Skip to content

Commit cd7e743

Browse files
committed
[GR-17457] More laziness in Nodes.
PullRequest: truffleruby/2809
2 parents b78b4a4 + db97613 commit cd7e743

File tree

5 files changed

+32
-35
lines changed

5 files changed

+32
-35
lines changed

src/main/java/org/truffleruby/core/MathNodes.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,6 @@ protected Object ldexp(Object a, Object b) {
400400
@CoreMethod(names = "lgamma", isModuleFunction = true, required = 1)
401401
public abstract static class LGammaNode extends CoreMethodArrayArgumentsNode {
402402

403-
@Child private IsANode isANode = IsANode.create();
404-
@Child private ToFNode toFNode = ToFNode.create();
405-
406403
private final BranchProfile exceptionProfile = BranchProfile.create();
407404

408405
@Specialization
@@ -433,7 +430,9 @@ protected RubyArray lgamma(double a) {
433430
}
434431

435432
@Fallback
436-
protected RubyArray lgamma(Object a) {
433+
protected RubyArray lgamma(Object a,
434+
@Cached IsANode isANode,
435+
@Cached ToFNode toFNode) {
437436
if (!isANode.executeIsA(a, coreLibrary().numericClass)) {
438437
exceptionProfile.enter();
439438
throw new RaiseException(getContext(), coreExceptions().typeErrorCantConvertInto(a, "Float", this));
@@ -469,6 +468,7 @@ protected double function(double a, NotProvided b) {
469468

470469
@Specialization(guards = { "!isRubyBignum(a)", "!isImplicitLongOrDouble(a)" })
471470
protected double function(Object a, NotProvided b,
471+
@Cached IsANode isANode,
472472
@Cached ToFNode toFNode) {
473473
if (!isANode.executeIsA(a, coreLibrary().numericClass)) {
474474
exceptionProfile.enter();
@@ -599,9 +599,6 @@ protected double doFunction(double a) {
599599

600600
protected abstract static class SimpleMonadicMathNode extends CoreMethodArrayArgumentsNode {
601601

602-
@Child private IsANode isANode = IsANode.create();
603-
@Child private ToFNode toFNode = ToFNode.create();
604-
605602
protected final BranchProfile exceptionProfile = BranchProfile.create();
606603

607604
// Must be implemented because we can't prevent Truffle from generating the useless SimpleMonadicClassGen.
@@ -630,7 +627,9 @@ protected double function(double a) {
630627
}
631628

632629
@Fallback
633-
protected double function(Object a) {
630+
protected double function(Object a,
631+
@Cached IsANode isANode,
632+
@Cached ToFNode toFNode) {
634633
if (!isANode.executeIsA(a, coreLibrary().numericClass)) {
635634
exceptionProfile.enter();
636635
throw new RaiseException(getContext(), coreExceptions().typeErrorCantConvertInto(a, "Float", this));
@@ -643,10 +642,6 @@ protected double function(Object a) {
643642

644643
protected abstract static class SimpleDyadicMathNode extends CoreMethodArrayArgumentsNode {
645644

646-
@Child protected IsANode isANode = IsANode.create();
647-
@Child protected ToFNode floatANode = ToFNode.create();
648-
@Child protected ToFNode floatBNode = ToFNode.create();
649-
650645
protected final BranchProfile exceptionProfile = BranchProfile.create();
651646

652647
// Must be implemented because we can't prevent Truffle from generating the useless SimpleDyadicClassGen.
@@ -735,7 +730,10 @@ protected double function(double a, double b) {
735730
}
736731

737732
@Fallback
738-
protected double function(Object a, Object b) {
733+
protected double function(Object a, Object b,
734+
@Cached IsANode isANode,
735+
@Cached ToFNode floatANode,
736+
@Cached ToFNode floatBNode) {
739737
if (!(isANode.executeIsA(a, coreLibrary().numericClass) &&
740738
isANode.executeIsA(b, coreLibrary().numericClass))) {
741739
exceptionProfile.enter();

src/main/java/org/truffleruby/core/format/convert/ToDoubleNode.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public abstract class ToDoubleNode extends FormatNode {
2020

2121
public abstract double executeToDouble(VirtualFrame frame, Object object);
2222

23+
public static ToDoubleNode create() {
24+
return ToDoubleNodeGen.create(null);
25+
}
26+
2327
@Specialization
2428
protected double toDouble(int value) {
2529
return value;

src/main/java/org/truffleruby/core/format/convert/ToIntegerNode.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,14 @@
1313
import org.truffleruby.core.numeric.RubyBignum;
1414
import org.truffleruby.language.dispatch.DispatchNode;
1515

16-
import com.oracle.truffle.api.CompilerDirectives;
16+
import com.oracle.truffle.api.dsl.Cached;
1717
import com.oracle.truffle.api.dsl.NodeChild;
1818
import com.oracle.truffle.api.dsl.Specialization;
1919
import com.oracle.truffle.api.frame.VirtualFrame;
2020

2121
@NodeChild("value")
2222
public abstract class ToIntegerNode extends FormatNode {
2323

24-
@Child private DispatchNode integerNode;
25-
2624
public abstract Object executeToInteger(VirtualFrame frame, Object object);
2725

2826
@Specialization
@@ -46,12 +44,8 @@ protected long toInteger(double value) {
4644
}
4745

4846
@Specialization(guards = "!isRubyNumber(value)")
49-
protected Object toInteger(VirtualFrame frame, Object value) {
50-
if (integerNode == null) {
51-
CompilerDirectives.transferToInterpreterAndInvalidate();
52-
integerNode = insert(DispatchNode.create());
53-
}
54-
47+
protected Object toInteger(VirtualFrame frame, Object value,
48+
@Cached DispatchNode integerNode) {
5549
return integerNode.call(getContext().getCoreLibrary().kernelModule, "Integer", value);
5650
}
5751

src/main/java/org/truffleruby/core/format/read/array/ReadDoubleNode.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
import org.truffleruby.core.array.library.ArrayStoreLibrary;
1414
import org.truffleruby.core.format.FormatNode;
1515
import org.truffleruby.core.format.convert.ToDoubleNode;
16-
import org.truffleruby.core.format.convert.ToDoubleNodeGen;
1716
import org.truffleruby.core.format.read.SourceNode;
1817

19-
import com.oracle.truffle.api.CompilerDirectives;
18+
import com.oracle.truffle.api.dsl.Cached;
2019
import com.oracle.truffle.api.dsl.ImportStatic;
2120
import com.oracle.truffle.api.dsl.NodeChild;
2221
import com.oracle.truffle.api.dsl.Specialization;
@@ -27,16 +26,10 @@
2726
@ImportStatic(ArrayGuards.class)
2827
public abstract class ReadDoubleNode extends FormatNode {
2928

30-
@Child private ToDoubleNode toDoubleNode;
31-
3229
@Specialization(limit = "storageStrategyLimit()")
3330
protected Object read(VirtualFrame frame, Object source,
34-
@CachedLibrary("source") ArrayStoreLibrary sources) {
35-
if (toDoubleNode == null) {
36-
CompilerDirectives.transferToInterpreterAndInvalidate();
37-
toDoubleNode = insert(ToDoubleNodeGen.create(null));
38-
}
39-
31+
@CachedLibrary("source") ArrayStoreLibrary sources,
32+
@Cached ToDoubleNode toDoubleNode) {
4033
return toDoubleNode.executeToDouble(frame, sources.read(source, advanceSourcePosition(frame)));
4134
}
4235

src/main/java/org/truffleruby/core/hash/HashNodes.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected RubyHash allocate(RubyClass rubyClass) {
6868
@ImportStatic(HashGuards.class)
6969
public abstract static class ConstructNode extends CoreMethodArrayArgumentsNode {
7070

71-
@Child private DispatchNode fallbackNode = DispatchNode.create();
71+
@Child private DispatchNode fallbackNode;
7272

7373
@ExplodeLoop(kind = LoopExplosionKind.FULL_UNROLL)
7474
@Specialization(guards = "isSmallArrayOfPairs(args, getLanguage())")
@@ -87,14 +87,14 @@ protected Object construct(RubyClass hashClass, Object[] args,
8787
final Object pair = store[n];
8888

8989
if (!RubyGuards.isRubyArray(pair)) {
90-
return fallbackNode.call(hashClass, "_constructor_fallback", args);
90+
return fallback(hashClass, args);
9191
}
9292

9393
final RubyArray pairArray = (RubyArray) pair;
9494
final Object pairStore = pairArray.store;
9595

9696
if (pairStore.getClass() != Object[].class || pairArray.size != 2) {
97-
return fallbackNode.call(hashClass, "_constructor_fallback", args);
97+
return fallback(hashClass, args);
9898
}
9999

100100
final Object[] pairObjectStore = (Object[]) pairStore;
@@ -114,6 +114,14 @@ protected Object construct(RubyClass hashClass, Object[] args,
114114

115115
@Specialization(guards = "!isSmallArrayOfPairs(args, getLanguage())")
116116
protected Object constructFallback(RubyClass hashClass, Object[] args) {
117+
return fallback(hashClass, args);
118+
}
119+
120+
private Object fallback(RubyClass hashClass, Object[] args) {
121+
if (fallbackNode == null) {
122+
CompilerDirectives.transferToInterpreterAndInvalidate();
123+
fallbackNode = insert(DispatchNode.create());
124+
}
117125
return fallbackNode.call(hashClass, "_constructor_fallback", args);
118126
}
119127

0 commit comments

Comments
 (0)