26
26
import com .oracle .truffle .api .dsl .Specialization ;
27
27
import com .oracle .truffle .api .frame .VirtualFrame ;
28
28
import com .oracle .truffle .api .source .SourceSection ;
29
+ import com .oracle .truffle .api .utilities .ConditionProfile ;
29
30
30
31
@ NodeChildren ({ @ NodeChild ("module" ), @ NodeChild ("name" ) })
31
32
public abstract class LookupConstantNode extends RubyNode {
@@ -49,13 +50,14 @@ public LexicalScope getLexicalScope() {
49
50
50
51
@ Specialization (guards = {
51
52
"module == cachedModule" ,
52
- "name.equals( cachedName)"
53
+ "guardName(name, cachedName, sameNameProfile )"
53
54
}, assumptions = "cachedModule.getUnmodifiedAssumption()" , limit = "getCacheLimit()" )
54
55
protected RubyConstant lookupConstant (VirtualFrame frame , RubyModule module , String name ,
55
56
@ Cached ("module" ) RubyModule cachedModule ,
56
57
@ Cached ("name" ) String cachedName ,
57
58
@ Cached ("doLookup(cachedModule, cachedName)" ) RubyConstant constant ,
58
- @ Cached ("isVisible(cachedModule, constant)" ) boolean isVisible ) {
59
+ @ Cached ("isVisible(cachedModule, constant)" ) boolean isVisible ,
60
+ @ Cached ("createBinaryProfile()" ) ConditionProfile sameNameProfile ) {
59
61
if (!isVisible ) {
60
62
CompilerDirectives .transferToInterpreter ();
61
63
throw new RaiseException (getContext ().getCoreLibrary ().nameErrorPrivateConstant (module , name , this ));
@@ -82,6 +84,15 @@ protected RubyConstant lookupNotModule(Object module, String name) {
82
84
throw new RaiseException (getContext ().getCoreLibrary ().typeErrorIsNotA (module .toString (), "class/module" , this ));
83
85
}
84
86
87
+ protected boolean guardName (String name , String cachedName , ConditionProfile sameNameProfile ) {
88
+ // This is likely as for literal constant lookup the name does not change and Symbols always return the same String.
89
+ if (sameNameProfile .profile (name == cachedName )) {
90
+ return true ;
91
+ } else {
92
+ return name .equals (cachedName );
93
+ }
94
+ }
95
+
85
96
protected RubyConstant doLookup (RubyModule module , String name ) {
86
97
return ModuleOperations .lookupConstant (getContext (), lexicalScope , module , name );
87
98
}
0 commit comments