Skip to content

Commit 13bde08

Browse files
committed
Cache quoteSymbol in QuoteNode as well
1 parent 231bb14 commit 13bde08

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

spec/truffle/regexp/inline_caching_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
# Check that calling it with many different inputs has the warning
9797
-> {
9898
("a".."z").each do |pattern|
99-
/ #{pattern} /
99+
/#{pattern}/
100100
end
101101
}.should complain(/unstable interpolated regexps/)
102102
end
@@ -126,6 +126,8 @@
126126
end
127127

128128
it "String#sub" do
129+
# Don't use String explicitly to trigger Truffle::Type.coerce_to_regexp. String argument is handled with
130+
# Primitive.matchdata_create_single_group and isn't converted to Regexp immediately.
129131
pattern = Class.new do
130132
def initialize(string) = @string = string
131133
def to_str = @string
@@ -154,6 +156,8 @@ def to_str = @string
154156
end
155157

156158
it "String#sub!" do
159+
# Don't use String explicitly to trigger Truffle::Type.coerce_to_regexp. String argument is handled with
160+
# Primitive.matchdata_create_single_group and isn't converted to Regexp immediately.
157161
pattern = Class.new do
158162
def initialize(string) = @string = string
159163
def to_str = @string
@@ -182,6 +186,8 @@ def to_str = @string
182186
end
183187

184188
it "String#gsub" do
189+
# Don't use String explicitly to trigger Truffle::Type.coerce_to_regexp. String argument is handled with
190+
# Primitive.matchdata_create_single_group and isn't converted to Regexp immediately.
185191
pattern = Class.new do
186192
def initialize(string) = @string = string
187193
def to_str = @string
@@ -210,6 +216,8 @@ def to_str = @string
210216
end
211217

212218
it "String#gsub!" do
219+
# Don't use String explicitly to trigger Truffle::Type.coerce_to_regexp. String argument is handled with
220+
# Primitive.matchdata_create_single_group and isn't converted to Regexp immediately.
213221
pattern = Class.new do
214222
def initialize(string) = @string = string
215223
def to_str = @string

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

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,43 +68,55 @@ public static QuoteNode create() {
6868
return RegexpNodesFactory.QuoteNodeFactory.create(null);
6969
}
7070

71+
@SuppressWarnings("truffle-static-method")
7172
@Specialization(
7273
guards = {
73-
"libRaw.isRubyString(raw)",
74-
"rawEqualNode.execute(node, libRaw, raw, cachedRaw, cachedRawEnc)" },
74+
"libString.isRubyString(raw)",
75+
"equalNode.execute(node, libString, raw, cachedString, cachedEnc)" },
7576
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,
77+
RubyString quoteStringCached(Object raw,
78+
@Cached @Shared RubyStringLibrary libString,
79+
@Cached("asTruffleStringUncached(raw)") TruffleString cachedString,
80+
@Cached("libString.getEncoding(raw)") RubyEncoding cachedEnc,
81+
@Cached StringHelperNodes.EqualSameEncodingNode equalNode,
8182
@Bind("this") Node node,
82-
@Cached("quote(libRaw, raw)") RubyString quotedString) {
83-
return quotedString;
83+
@Cached("quote(libString, raw)") TStringWithEncoding quotedString) {
84+
return createString(quotedString);
8485
}
8586

86-
@Specialization(replaces = "quoteStringCached", guards = "libRaw.isRubyString(raw)")
87+
@Specialization(replaces = "quoteStringCached", guards = "libString.isRubyString(raw)")
8788
RubyString quoteString(Object raw,
88-
@Cached @Shared RubyStringLibrary libRaw) {
89-
return quote(libRaw, raw);
89+
@Cached @Shared RubyStringLibrary libString) {
90+
return createString(quote(libString, raw));
9091
}
9192

92-
@Specialization
93+
@Specialization(guards = "raw == cachedSymbol", limit = "getDefaultCacheLimit()")
94+
RubyString quoteSymbolCached(RubySymbol raw,
95+
@Cached("raw") RubySymbol cachedSymbol,
96+
@Cached("quote(cachedSymbol)") TStringWithEncoding quotedString) {
97+
return createString(quotedString);
98+
}
99+
100+
@Specialization(replaces = "quoteSymbolCached")
93101
RubyString quoteSymbol(RubySymbol raw) {
94-
return createString(ClassicRegexp.quote19(new ATStringWithEncoding(raw.tstring, raw.encoding)));
102+
return createString(quote(raw));
95103
}
96104

97-
@Specialization(guards = { "!libRaw.isRubyString(raw)", "!isRubySymbol(raw)" })
105+
@Specialization(guards = { "!libString.isRubyString(raw)", "!isRubySymbol(raw)" })
98106
static RubyString quoteGeneric(Object raw,
99-
@Cached @Shared RubyStringLibrary libRaw,
107+
@Cached @Shared RubyStringLibrary libString,
100108
@Cached ToStrNode toStrNode,
101109
@Cached QuoteNode recursive,
102110
@Bind("this") Node node) {
103111
return recursive.execute(toStrNode.execute(node, raw));
104112
}
105113

106-
RubyString quote(RubyStringLibrary strings, Object string) {
107-
return createString(ClassicRegexp.quote19(new ATStringWithEncoding(strings, string)));
114+
TStringWithEncoding quote(RubyStringLibrary strings, Object string) {
115+
return ClassicRegexp.quote19(new ATStringWithEncoding(strings, string));
116+
}
117+
118+
TStringWithEncoding quote(RubySymbol symbol) {
119+
return ClassicRegexp.quote19(new ATStringWithEncoding(symbol.tstring, symbol.encoding));
108120
}
109121

110122
}

0 commit comments

Comments
 (0)