Skip to content

Commit ccb351d

Browse files
committed
[Truffle] Speculate on the name being the same String instance.
* String.equals() already check if it is, but would likely compile more code.
1 parent c281d17 commit ccb351d

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

truffle/src/main/java/org/jruby/truffle/nodes/constants/LookupConstantNode.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.oracle.truffle.api.dsl.Specialization;
2727
import com.oracle.truffle.api.frame.VirtualFrame;
2828
import com.oracle.truffle.api.source.SourceSection;
29+
import com.oracle.truffle.api.utilities.ConditionProfile;
2930

3031
@NodeChildren({ @NodeChild("module"), @NodeChild("name") })
3132
public abstract class LookupConstantNode extends RubyNode {
@@ -49,13 +50,14 @@ public LexicalScope getLexicalScope() {
4950

5051
@Specialization(guards = {
5152
"module == cachedModule",
52-
"name.equals(cachedName)"
53+
"guardName(name, cachedName, sameNameProfile)"
5354
}, assumptions = "cachedModule.getUnmodifiedAssumption()", limit = "getCacheLimit()")
5455
protected RubyConstant lookupConstant(VirtualFrame frame, RubyModule module, String name,
5556
@Cached("module") RubyModule cachedModule,
5657
@Cached("name") String cachedName,
5758
@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) {
5961
if (!isVisible) {
6062
CompilerDirectives.transferToInterpreter();
6163
throw new RaiseException(getContext().getCoreLibrary().nameErrorPrivateConstant(module, name, this));
@@ -82,6 +84,15 @@ protected RubyConstant lookupNotModule(Object module, String name) {
8284
throw new RaiseException(getContext().getCoreLibrary().typeErrorIsNotA(module.toString(), "class/module", this));
8385
}
8486

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+
8596
protected RubyConstant doLookup(RubyModule module, String name) {
8697
return ModuleOperations.lookupConstant(getContext(), lexicalScope, module, name);
8798
}

0 commit comments

Comments
 (0)