Skip to content

Commit d01e644

Browse files
committed
Reduce duplicated code in regexp instrumentation.
1 parent 6e41f81 commit d01e644

File tree

2 files changed

+22
-44
lines changed

2 files changed

+22
-44
lines changed

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

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -363,43 +363,29 @@ protected <T> RubyArray fillinInstrumentData(Map<T, AtomicInteger> map, ArrayBui
363363
}
364364
}
365365

366-
@CoreMethod(names = "literal_regexp_compilation_stats_array", onSingleton = true, required = 0)
367-
public abstract static class LiteralRegexpCompilationStatsArrayNode extends RegexpStatsNode {
366+
@CoreMethod(names = "regexp_compilation_stats_array", onSingleton = true, required = 1)
367+
public abstract static class RegexpCompilationStatsArrayNode extends RegexpStatsNode {
368368

369369
@Specialization
370-
protected Object buildStatsArray(
370+
protected Object buildStatsArray(boolean literalRegexps,
371371
@Cached ArrayBuilderNode arrayBuilderNode) {
372-
return fillinInstrumentData(COMPILED_REGEXPS_LITERAL, arrayBuilderNode, getContext());
373-
}
374-
}
375-
376-
@CoreMethod(names = "dynamic_regexp_compilation_stats_array", onSingleton = true, required = 0)
377-
public abstract static class DynamicRegexpCompilationStatsArrayNode extends RegexpStatsNode {
378-
379-
@Specialization
380-
protected Object buildStatsArray(
381-
@Cached ArrayBuilderNode arrayBuilderNode) {
382-
return fillinInstrumentData(COMPILED_REGEXPS_DYNAMIC, arrayBuilderNode, getContext());
383-
}
384-
}
385-
386-
@CoreMethod(names = "joni_match_stats_array", onSingleton = true, required = 0)
387-
public abstract static class JoniMatchStatsArrayNode extends RegexpStatsNode {
388-
389-
@Specialization
390-
protected Object buildStatsArray(
391-
@Cached ArrayBuilderNode arrayBuilderNode) {
392-
return fillinInstrumentData(MATCHED_REGEXPS_JONI, arrayBuilderNode, getContext());
372+
return fillinInstrumentData(
373+
literalRegexps ? COMPILED_REGEXPS_LITERAL : COMPILED_REGEXPS_DYNAMIC,
374+
arrayBuilderNode,
375+
getContext());
393376
}
394377
}
395378

396-
@CoreMethod(names = "tregex_match_stats_array", onSingleton = true, required = 0)
397-
public abstract static class TRegexMatchStatsArrayNode extends RegexpStatsNode {
379+
@CoreMethod(names = "match_stats_array", onSingleton = true, required = 1)
380+
public abstract static class MatchStatsArrayNode extends RegexpStatsNode {
398381

399382
@Specialization
400-
protected Object buildStatsArray(
383+
protected Object buildStatsArray(boolean joniMatches,
401384
@Cached ArrayBuilderNode arrayBuilderNode) {
402-
return fillinInstrumentData(MATCHED_REGEXPS_TREGEX, arrayBuilderNode, getContext());
385+
return fillinInstrumentData(
386+
joniMatches ? MATCHED_REGEXPS_JONI : MATCHED_REGEXPS_TREGEX,
387+
arrayBuilderNode,
388+
getContext());
403389
}
404390
}
405391

src/main/ruby/truffleruby/core/truffle/regexp_operations.rb

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,12 @@ def self.return_match_data(md)
154154
end
155155
end
156156

157-
def self.compilation_stats_literal_regexp
158-
Hash[*literal_regexp_compilation_stats_array]
157+
def self.compilation_stats_regexp(literal_regexps:)
158+
Hash[*regexp_compilation_stats_array(literal_regexps)]
159159
end
160160

161-
def self.compilation_stats_dynamic_regexp
162-
Hash[*dynamic_regexp_compilation_stats_array]
163-
end
164-
165-
def self.match_stats_joni
166-
Hash[*joni_match_stats_array]
167-
end
168-
169-
def self.match_stats_tregex
170-
Hash[*tregex_match_stats_array]
161+
def self.match_stats(joni_matches:)
162+
Hash[*match_stats_array(joni_matches)]
171163
end
172164

173165
def self.print_stats
@@ -180,19 +172,19 @@ def self.print_stats
180172

181173
if Truffle::Boot.get_option('regexp-instrument-creation')
182174
puts ' Compilation (Literal)'
183-
print_stats_table compilation_stats_literal_regexp
175+
print_stats_table compilation_stats_regexp(literal_regexps: true)
184176
puts ' --------------------'
185177
puts ' Compilation (Dynamic)'
186-
print_stats_table compilation_stats_dynamic_regexp
178+
print_stats_table compilation_stats_regexp(literal_regexps: false)
187179
puts ' --------------------'
188180
end
189181

190182
if Truffle::Boot.get_option('regexp-instrument-match')
191183
puts ' Matches (Joni)'
192-
print_stats_table match_stats_joni
184+
print_stats_table match_stats(joni_matches: true)
193185
puts ' --------------------'
194186
puts ' Matches (TRegex)'
195-
print_stats_table match_stats_tregex
187+
print_stats_table match_stats(joni_matches: false)
196188

197189
if Truffle::Boot.get_option('regexp-instrument-creation')
198190
puts ' --------------------'

0 commit comments

Comments
 (0)