Skip to content

Commit 02ef8d9

Browse files
committed
lazily initialize node in ToStringOrSymbolNode
1 parent c5200a2 commit 02ef8d9

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/main/java/org/truffleruby/core/cast/ToStringOrSymbolNode.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package org.truffleruby.core.cast;
1111

12+
import com.oracle.truffle.api.CompilerDirectives;
1213
import com.oracle.truffle.api.dsl.Cached;
1314
import com.oracle.truffle.api.dsl.NodeChild;
1415
import com.oracle.truffle.api.dsl.Specialization;
@@ -27,7 +28,7 @@
2728
@NodeChild(value = "child", type = RubyNode.class)
2829
public abstract class ToStringOrSymbolNode extends RubyNode {
2930

30-
@Child private CallDispatchHeadNode toStr = CallDispatchHeadNode.createPrivate();
31+
@Child private CallDispatchHeadNode toStr;
3132

3233
@Specialization(guards = "isRubySymbol(symbol)")
3334
public DynamicObject coerceRubySymbol(DynamicObject symbol) {
@@ -44,7 +45,7 @@ public DynamicObject coerceObject(VirtualFrame frame, Object object,
4445
@Cached("create()") BranchProfile errorProfile) {
4546
final Object coerced;
4647
try {
47-
coerced = toStr.call(object, "to_str");
48+
coerced = callToStr(object);
4849
} catch (RaiseException e) {
4950
errorProfile.enter();
5051
if (Layouts.BASIC_OBJECT.getLogicalClass(e.getException()) == coreLibrary().getNoMethodErrorClass()) {
@@ -61,4 +62,12 @@ public DynamicObject coerceObject(VirtualFrame frame, Object object,
6162
throw new RaiseException(getContext(), coreExceptions().typeErrorBadCoercion(object, "String", "to_str", coerced, this));
6263
}
6364
}
65+
66+
private Object callToStr(Object object) {
67+
if (toStr == null) {
68+
CompilerDirectives.transferToInterpreterAndInvalidate();
69+
toStr = insert(CallDispatchHeadNode.createPrivate());
70+
}
71+
return toStr.call(object, "to_str");
72+
}
6473
}

0 commit comments

Comments
 (0)