Skip to content

Commit 7fe36be

Browse files
eregonandrykonchin
authored andcommitted
Add spec for Regexp inline caching
1 parent 7b937ba commit 7fe36be

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# truffleruby_primitives: true
2+
3+
# Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. This
4+
# code is released under a tri EPL/GPL/LGPL license. You can use it,
5+
# redistribute it and/or modify it under the terms of the:
6+
#
7+
# Eclipse Public License version 2.0, or
8+
# GNU General Public License version 2, or
9+
# GNU Lesser General Public License version 2.1.
10+
11+
require_relative '../../ruby/spec_helper'
12+
13+
guard -> { TruffleRuby.jit? } do # this test needs splitting
14+
describe "Inline caching for dynamically-created Regexp works for" do
15+
before :each do
16+
@performance_warnings, Warning[:performance] = Warning[:performance], true
17+
end
18+
19+
after :each do
20+
Warning[:performance] = @performance_warnings
21+
end
22+
23+
it "Regexp.union with 1 argument" do
24+
# Check that separate call sites with fixed input does not warn
25+
-> {
26+
Regexp.union("a")
27+
Regexp.union("b")
28+
Regexp.union("c")
29+
Regexp.union("d")
30+
Regexp.union("e")
31+
Regexp.union("f")
32+
Regexp.union("g")
33+
Regexp.union("h")
34+
Regexp.union("i")
35+
Regexp.union("j")
36+
}.should_not complain
37+
38+
# Check that calling it with many different inputs has the warning.
39+
-> {
40+
("a".."z").each do |pattern|
41+
Regexp.union(pattern)
42+
end
43+
}.should complain(/unbounded creation of regexps/)
44+
end
45+
46+
it "Regexp.union with multiple arguments" do
47+
# Check that separate call sites with fixed input does not warn
48+
-> {
49+
Regexp.union("h", "a")
50+
Regexp.union("h", "b")
51+
Regexp.union("h", "c")
52+
Regexp.union("h", "d")
53+
Regexp.union("h", "e")
54+
Regexp.union("h", "f")
55+
Regexp.union("h", "g")
56+
Regexp.union("h", "h")
57+
Regexp.union("h", "i")
58+
Regexp.union("h", "j")
59+
}.should_not complain
60+
61+
# Check that calling it with many different inputs has the warning.
62+
-> {
63+
("a".."z").each do |pattern|
64+
Regexp.union("h", pattern)
65+
end
66+
}.should complain(/unbounded creation of regexps/)
67+
end
68+
69+
it "String#scan" do
70+
# Check that separate call sites with fixed input does not warn
71+
-> {
72+
"zzz".scan("a")
73+
"zzz".scan("b")
74+
"zzz".scan("c")
75+
"zzz".scan("d")
76+
"zzz".scan("e")
77+
"zzz".scan("f")
78+
"zzz".scan("g")
79+
"zzz".scan("h")
80+
"zzz".scan("i")
81+
"zzz".scan("j")
82+
}.should_not complain
83+
84+
# Check that calling it with many different inputs has the warning.
85+
-> {
86+
# "a".."z" and not just "a".."j" because there can be some late heuristic megamorphic splitting by TRegex (ExecCompiledRegexNode)
87+
("a".."z").each do |pattern|
88+
"zzz".scan(pattern)
89+
end
90+
}.should complain(/unbounded creation of regexps/)
91+
end
92+
end
93+
end

0 commit comments

Comments
 (0)