Skip to content

Commit 8e663ee

Browse files
committed
Use inlining cache in QuoteNode
1 parent f63bed0 commit 8e663ee

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/main/java/org/truffleruby/core/regexp/RegexpNodes.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.oracle.truffle.api.strings.TruffleString;
2020
import org.graalvm.shadowed.org.joni.NameEntry;
2121
import org.truffleruby.annotations.CoreMethod;
22+
import org.truffleruby.annotations.Split;
2223
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
2324
import org.truffleruby.annotations.CoreModule;
2425
import org.truffleruby.annotations.Primitive;
@@ -43,7 +44,6 @@
4344
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4445
import com.oracle.truffle.api.dsl.Cached;
4546
import com.oracle.truffle.api.dsl.Cached.Shared;
46-
import com.oracle.truffle.api.dsl.Fallback;
4747
import com.oracle.truffle.api.dsl.Specialization;
4848

4949
@CoreModule(value = "Regexp", isClass = true)
@@ -58,7 +58,7 @@ int hash(RubyRegexp regexp) {
5858
}
5959
}
6060

61-
@CoreMethod(names = { "quote", "escape" }, onSingleton = true, required = 1)
61+
@CoreMethod(names = { "quote", "escape" }, onSingleton = true, required = 1, split = Split.ALWAYS)
6262
public abstract static class QuoteNode extends CoreMethodArrayArgumentsNode {
6363

6464
public abstract RubyString execute(Object raw);
@@ -68,25 +68,45 @@ public static QuoteNode create() {
6868
return RegexpNodesFactory.QuoteNodeFactory.create(null);
6969
}
7070

71-
@Specialization(guards = "libRaw.isRubyString(raw)", limit = "1")
71+
@Specialization(
72+
guards = {
73+
"libRaw.isRubyString(raw)",
74+
"rawEqualNode.execute(node, libRaw, raw, cachedRaw, cachedRawEnc)" },
75+
limit = "getDefaultCacheLimit()")
76+
static RubyString quoteStringCached(Object raw,
77+
@Cached @Shared RubyStringLibrary libRaw,
78+
@Cached("asTruffleStringUncached(raw)") TruffleString cachedRaw,
79+
@Cached("libRaw.getEncoding(raw)") RubyEncoding cachedRawEnc,
80+
@Cached StringHelperNodes.EqualSameEncodingNode rawEqualNode,
81+
@Bind("this") Node node,
82+
@Cached("quote(libRaw, raw)") RubyString quotedString) {
83+
return quotedString;
84+
}
85+
86+
@Specialization(replaces = "quoteStringCached", guards = "libRaw.isRubyString(raw)")
7287
RubyString quoteString(Object raw,
73-
@Cached RubyStringLibrary libRaw) {
74-
return createString(ClassicRegexp.quote19(new ATStringWithEncoding(libRaw, raw)));
88+
@Cached @Shared RubyStringLibrary libRaw) {
89+
return quote(libRaw, raw);
7590
}
7691

7792
@Specialization
7893
RubyString quoteSymbol(RubySymbol raw) {
7994
return createString(ClassicRegexp.quote19(new ATStringWithEncoding(raw.tstring, raw.encoding)));
8095
}
8196

82-
@Fallback
83-
static RubyString quote(Object raw,
97+
@Specialization(guards = { "!libRaw.isRubyString(raw)", "!isRubySymbol(raw)" })
98+
static RubyString quoteGeneric(Object raw,
99+
@Cached @Shared RubyStringLibrary libRaw,
84100
@Cached ToStrNode toStrNode,
85101
@Cached QuoteNode recursive,
86102
@Bind("this") Node node) {
87103
return recursive.execute(toStrNode.execute(node, raw));
88104
}
89105

106+
RubyString quote(RubyStringLibrary strings, Object string) {
107+
return createString(ClassicRegexp.quote19(new ATStringWithEncoding(strings, string)));
108+
}
109+
90110
}
91111

92112
@CoreMethod(names = "source")

0 commit comments

Comments
 (0)