Skip to content

Commit 1b3c003

Browse files
committed
Convert YieldingCoreMethodNode and GetConstantNode to stateless
1 parent 1c7e476 commit 1b3c003

27 files changed

+270
-172
lines changed

src/main/java/org/truffleruby/builtins/YieldingCoreMethodNode.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414

1515
public abstract class YieldingCoreMethodNode extends CoreMethodArrayArgumentsNode {
1616

17-
@Child private CallBlockNode yieldNode = CallBlockNode.create();
18-
1917
// Not called yield() because that warns in Java 13+
20-
public Object callBlock(RubyProc block, Object... arguments) {
18+
public static Object callBlock(CallBlockNode yieldNode, RubyProc block, Object... arguments) {
2119
return yieldNode.yield(block, arguments);
2220
}
2321

src/main/java/org/truffleruby/cext/CExtNodes.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
import org.truffleruby.language.objects.MetaClassNode;
119119
import org.truffleruby.language.objects.WriteObjectFieldNode;
120120
import org.truffleruby.language.supercall.CallSuperMethodNode;
121+
import org.truffleruby.language.yield.CallBlockNode;
121122
import org.truffleruby.parser.IdentifierType;
122123

123124
import com.oracle.truffle.api.CompilerDirectives;
@@ -975,17 +976,17 @@ protected ImmutableRubyString rbStrUnlockTmpImmutable(ImmutableRubyString string
975976
public abstract static class RbConstGetNode extends CoreMethodNode {
976977

977978
@Child private LookupConstantNode lookupConstantNode = LookupConstantNode.create(true, true);
978-
@Child private GetConstantNode getConstantNode = GetConstantNode.create();
979979

980980
@CreateCast("name")
981981
protected RubyNode coerceToString(RubyNode name) {
982982
return ToJavaStringNode.create(name);
983983
}
984984

985985
@Specialization
986-
protected Object rbConstGet(RubyModule module, String name) {
986+
protected Object rbConstGet(RubyModule module, String name,
987+
@Cached GetConstantNode getConstantNode) {
987988
return getConstantNode
988-
.lookupAndResolveConstant(LexicalScope.IGNORE, module, name, false, lookupConstantNode);
989+
.lookupAndResolveConstant(LexicalScope.IGNORE, module, name, false, lookupConstantNode, true);
989990
}
990991

991992
}
@@ -996,17 +997,17 @@ protected Object rbConstGet(RubyModule module, String name) {
996997
public abstract static class RbConstGetFromNode extends CoreMethodNode {
997998

998999
@Child private LookupConstantNode lookupConstantNode = LookupConstantNode.create(true, false);
999-
@Child private GetConstantNode getConstantNode = GetConstantNode.create();
10001000

10011001
@CreateCast("name")
10021002
protected RubyNode coerceToString(RubyNode name) {
10031003
return ToJavaStringNode.create(name);
10041004
}
10051005

10061006
@Specialization
1007-
protected Object rbConstGetFrom(RubyModule module, String name) {
1007+
protected Object rbConstGetFrom(RubyModule module, String name,
1008+
@Cached GetConstantNode getConstantNode) {
10081009
return getConstantNode
1009-
.lookupAndResolveConstant(LexicalScope.IGNORE, module, name, false, lookupConstantNode);
1010+
.lookupAndResolveConstant(LexicalScope.IGNORE, module, name, false, lookupConstantNode, true);
10101011
}
10111012

10121013
}
@@ -1461,9 +1462,10 @@ public abstract static class CaptureExceptionNode extends YieldingCoreMethodNode
14611462
@Specialization
14621463
protected Object captureException(RubyProc block,
14631464
@Cached InlinedBranchProfile exceptionProfile,
1464-
@Cached InlinedBranchProfile noExceptionProfile) {
1465+
@Cached InlinedBranchProfile noExceptionProfile,
1466+
@Cached CallBlockNode yieldNode) {
14651467
try {
1466-
callBlock(block);
1468+
callBlock(yieldNode, block);
14671469
noExceptionProfile.enter(this);
14681470
return nil;
14691471
} catch (Throwable e) {

src/main/java/org/truffleruby/core/array/ArrayEachIteratorNode.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424
import com.oracle.truffle.api.nodes.NodeInterface;
2525
import com.oracle.truffle.api.profiles.ConditionProfile;
2626
import com.oracle.truffle.api.profiles.LoopConditionProfile;
27+
import org.truffleruby.language.yield.CallBlockNode;
2728

2829
@ImportStatic(ArrayGuards.class)
2930
@ReportPolymorphism
3031
public abstract class ArrayEachIteratorNode extends RubyBaseNode {
3132

3233
public interface ArrayElementConsumerNode extends NodeInterface {
33-
void accept(RubyArray array, Object state, Object element, int index);
34+
void accept(CallBlockNode yieldNode, RubyArray array, Object state, Object element, int index);
3435
}
3536

3637
@Child private ArrayEachIteratorNode recurseNode;
@@ -50,13 +51,14 @@ protected RubyArray iterateMany(RubyArray array, Object state, int startAt, Arra
5051
// Checkstyle: resume
5152
@Cached LoopConditionProfile loopProfile,
5253
@Cached IntValueProfile arraySizeProfile,
53-
@Cached ConditionProfile strategyMatchProfile) {
54+
@Cached ConditionProfile strategyMatchProfile,
55+
@Cached CallBlockNode yieldNode) {
5456
int i = startAt;
5557
try {
5658
for (; loopProfile.inject(i < arraySizeProfile.profile(array.size)); i++) {
5759
Object store = array.getStore();
5860
if (strategyMatchProfile.profile(stores.accepts(store))) {
59-
consumerNode.accept(array, state, stores.read(store, i), i);
61+
consumerNode.accept(yieldNode, array, state, stores.read(store, i), i);
6062
} else {
6163
return getRecurseNode().execute(array, state, i, consumerNode);
6264
}

src/main/java/org/truffleruby/core/array/ArrayNodes.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -673,9 +673,11 @@ protected Object delete(RubyArray array, Object value, Object maybeBlock,
673673
@Bind("array.getStore()") Object store,
674674
@CachedLibrary("store") ArrayStoreLibrary stores,
675675
@Cached @Shared IntValueProfile arraySizeProfile,
676-
@Cached @Shared LoopConditionProfile loopProfile) {
676+
@Cached @Shared LoopConditionProfile loopProfile,
677+
@Cached @Shared CallBlockNode yieldNode) {
677678

678-
return delete(array, value, maybeBlock, true, store, store, stores, stores, arraySizeProfile, loopProfile);
679+
return delete(array, value, maybeBlock, true, store, store, stores, stores, arraySizeProfile, loopProfile,
680+
yieldNode);
679681
}
680682

681683
@Specialization(
@@ -686,11 +688,12 @@ protected Object delete(RubyArray array, Object value, Object maybeBlock,
686688
@CachedLibrary("store") ArrayStoreLibrary stores,
687689
@CachedLibrary(limit = "1") ArrayStoreLibrary newStores,
688690
@Cached @Shared IntValueProfile arraySizeProfile,
689-
@Cached @Shared LoopConditionProfile loopProfile) {
691+
@Cached @Shared LoopConditionProfile loopProfile,
692+
@Cached @Shared CallBlockNode yieldNode) {
690693

691694
final Object newStore = stores.allocator(store).allocate(arraySizeProfile.profile(array.size));
692695
return delete(array, value, maybeBlock, false, store, newStore, stores, newStores, arraySizeProfile,
693-
loopProfile);
696+
loopProfile, yieldNode);
694697
}
695698

696699
private Object delete(RubyArray array, Object value, Object maybeBlock,
@@ -700,7 +703,8 @@ private Object delete(RubyArray array, Object value, Object maybeBlock,
700703
ArrayStoreLibrary oldStores,
701704
ArrayStoreLibrary newStores,
702705
IntValueProfile arraySizeProfile,
703-
LoopConditionProfile loopProfile) {
706+
LoopConditionProfile loopProfile,
707+
CallBlockNode yieldNode) {
704708

705709
assert !sameStores || (oldStore == newStore && oldStores == newStores);
706710

@@ -739,7 +743,7 @@ private Object delete(RubyArray array, Object value, Object maybeBlock,
739743
if (maybeBlock == nil) {
740744
return nil;
741745
} else {
742-
return callBlock((RubyProc) maybeBlock, value);
746+
return callBlock(yieldNode, (RubyProc) maybeBlock, value);
743747
}
744748
}
745749
}
@@ -830,9 +834,9 @@ protected Object each(RubyArray array, RubyProc block,
830834
}
831835

832836
@Override
833-
public void accept(RubyArray array, Object state, Object element, int index) {
837+
public void accept(CallBlockNode yieldNode, RubyArray array, Object state, Object element, int index) {
834838
RubyProc block = (RubyProc) state;
835-
callBlock(block, element);
839+
callBlock(yieldNode, block, element);
836840
}
837841

838842
}
@@ -842,16 +846,14 @@ public void accept(RubyArray array, Object state, Object element, int index) {
842846
public abstract static class EachWithIndexNode extends PrimitiveArrayArgumentsNode
843847
implements ArrayElementConsumerNode {
844848

845-
@Child private CallBlockNode yieldNode = CallBlockNode.create();
846-
847849
@Specialization
848850
protected Object eachOther(RubyArray array, RubyProc block,
849851
@Cached ArrayEachIteratorNode iteratorNode) {
850852
return iteratorNode.execute(array, block, 0, this);
851853
}
852854

853855
@Override
854-
public void accept(RubyArray array, Object state, Object element, int index) {
856+
public void accept(CallBlockNode yieldNode, RubyArray array, Object state, Object element, int index) {
855857
RubyProc block = (RubyProc) state;
856858
yieldNode.yield(block, element, index);
857859
}
@@ -1235,13 +1237,14 @@ protected Object initializeBlock(RubyArray array, int size, Object unusedFilling
12351237
@CachedLibrary(limit = "2") @Exclusive ArrayStoreLibrary stores,
12361238
@Cached @Shared IsSharedNode isSharedNode,
12371239
@Cached @Shared ConditionProfile sharedProfile,
1238-
@Cached @Exclusive LoopConditionProfile loopProfile) {
1240+
@Cached @Exclusive LoopConditionProfile loopProfile,
1241+
@Cached CallBlockNode yieldNode) {
12391242
BuilderState state = arrayBuilder.start(size);
12401243

12411244
int n = 0;
12421245
try {
12431246
for (; loopProfile.inject(n < size); n++) {
1244-
final Object value = callBlock(block, n);
1247+
final Object value = callBlock(yieldNode, block, n);
12451248
arrayBuilder.appendValue(state, n, value);
12461249
}
12471250
} finally {
@@ -1347,7 +1350,6 @@ private static class State {
13471350
}
13481351

13491352
@Child private DispatchNode dispatch = DispatchNode.create(PUBLIC);
1350-
@Child private CallBlockNode yieldNode = CallBlockNode.create();
13511353

13521354
// Uses block and no Symbol
13531355

@@ -1390,10 +1392,9 @@ private Object injectBlockHelper(RubyArray array,
13901392
}
13911393

13921394
@Override
1393-
public void accept(RubyArray array, Object stateObject, Object element, int index) {
1395+
public void accept(CallBlockNode yieldNode, RubyArray array, Object stateObject, Object element, int index) {
13941396
final State state = (State) stateObject;
1395-
final Object accumulator = yieldNode.yield(state.block, state.accumulator, element);
1396-
state.accumulator = accumulator;
1397+
state.accumulator = yieldNode.yield(state.block, state.accumulator, element);
13971398
}
13981399

13991400
// Uses Symbol and no block
@@ -1506,10 +1507,10 @@ protected Object map(RubyArray array, RubyProc block,
15061507
}
15071508

15081509
@Override
1509-
public void accept(RubyArray array, Object stateObject, Object element, int index) {
1510+
public void accept(CallBlockNode yieldNode, RubyArray array, Object stateObject, Object element, int index) {
15101511
final State state = (State) stateObject;
15111512

1512-
Object value = callBlock(state.block, element);
1513+
Object value = callBlock(yieldNode, state.block, element);
15131514
arrayBuilder.appendValue(state.builderState, index, value);
15141515
}
15151516

@@ -1528,9 +1529,9 @@ protected Object map(RubyArray array, RubyProc block,
15281529
}
15291530

15301531
@Override
1531-
public void accept(RubyArray array, Object state, Object element, int index) {
1532+
public void accept(CallBlockNode yieldNode, RubyArray array, Object state, Object element, int index) {
15321533
RubyProc block = (RubyProc) state;
1533-
writeNode.executeWrite(array, index, callBlock(block, element));
1534+
writeNode.executeWrite(array, index, callBlock(yieldNode, block, element));
15341535
}
15351536

15361537
}
@@ -1793,10 +1794,10 @@ protected Object reject(RubyArray array, RubyProc block,
17931794
}
17941795

17951796
@Override
1796-
public void accept(RubyArray array, Object stateObject, Object element, int index) {
1797+
public void accept(CallBlockNode yieldNode, RubyArray array, Object stateObject, Object element, int index) {
17971798
final State state = (State) stateObject;
17981799

1799-
if (!booleanCastNode.execute(callBlock(state.block, element))) {
1800+
if (!booleanCastNode.execute(callBlock(yieldNode, state.block, element))) {
18001801
arrayBuilder.appendValue(state.builderState, state.newArraySize, element);
18011802
state.newArraySize++;
18021803
}
@@ -2006,10 +2007,10 @@ protected Object select(RubyArray array, RubyProc block,
20062007
}
20072008

20082009
@Override
2009-
public void accept(RubyArray array, Object stateObject, Object element, int index) {
2010+
public void accept(CallBlockNode yieldNode, RubyArray array, Object stateObject, Object element, int index) {
20102011
final State state = (State) stateObject;
20112012

2012-
if (booleanCastNode.execute(callBlock(state.block, element))) {
2013+
if (booleanCastNode.execute(callBlock(yieldNode, state.block, element))) {
20132014
arrayBuilder.appendValue(state.builderState, state.selectedSize, element);
20142015
state.selectedSize++;
20152016
}

src/main/java/org/truffleruby/core/inlined/AlwaysInlinedMethodNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private static RaiseException buildException(Node node, RootCallTarget target) {
6969
private static RaiseException buildException(Node node, String method) {
7070
return new RaiseException(getContext(node), coreExceptions(node).runtimeError(
7171
method + " needs the caller frame but it was not passed (cannot be called directly from a foreign language)",
72-
node));
72+
getNode(node)));
7373
}
7474

7575
public static boolean isBlockProvided(Object[] rubyArgs) {

src/main/java/org/truffleruby/core/module/ModuleNodes.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,17 +1131,17 @@ private Object getConstant(RubyModule module, String name, boolean checkName, bo
11311131
if (lookInObject) {
11321132
return getConstantNode
11331133
.lookupAndResolveConstant(LexicalScope.IGNORE, module, name, checkName,
1134-
lookupConstantLookInObjectNode);
1134+
lookupConstantLookInObjectNode, true);
11351135
} else {
11361136
return getConstantNode
11371137
.lookupAndResolveConstant(LexicalScope.IGNORE, module, name, checkName,
1138-
lookupConstantNode);
1138+
lookupConstantNode, true);
11391139
}
11401140
}
11411141

11421142
private Object getConstantNoInherit(RubyModule module, String name, boolean checkName) {
11431143
final LookupConstantInterface lookup = this::lookupConstantNoInherit;
1144-
return getConstantNode.lookupAndResolveConstant(LexicalScope.IGNORE, module, name, checkName, lookup);
1144+
return getConstantNode.lookupAndResolveConstant(LexicalScope.IGNORE, module, name, checkName, lookup, true);
11451145
}
11461146

11471147
@TruffleBoundary

src/main/java/org/truffleruby/core/mutex/MutexNodes.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.truffleruby.annotations.Visibility;
2727
import org.truffleruby.language.control.RaiseException;
2828
import org.truffleruby.language.objects.AllocationTracing;
29+
import org.truffleruby.language.yield.CallBlockNode;
2930

3031
import java.util.concurrent.locks.ReentrantLock;
3132

@@ -120,7 +121,8 @@ public abstract static class SynchronizeNode extends YieldingCoreMethodNode {
120121

121122
@Specialization
122123
protected Object synchronize(RubyMutex mutex, RubyProc block,
123-
@Cached InlinedBranchProfile errorProfile) {
124+
@Cached InlinedBranchProfile errorProfile,
125+
@Cached CallBlockNode yieldNode) {
124126
final ReentrantLock lock = mutex.lock;
125127
final RubyThread thread = getLanguage().getCurrentThread();
126128

@@ -134,7 +136,7 @@ protected Object synchronize(RubyMutex mutex, RubyProc block,
134136
* locks list to be in consistent state at the end. */
135137
MutexOperations.lock(getContext(), lock, thread, this);
136138
try {
137-
return callBlock(block);
139+
return callBlock(yieldNode, block);
138140
} finally {
139141
MutexOperations.checkOwnedMutex(getContext(), lock, this, errorProfile);
140142
MutexOperations.unlock(lock, thread);

0 commit comments

Comments
 (0)