Skip to content

Commit 8a4959f

Browse files
authored
rubocop: reduce every regex evaluation for performance (#4934)
**Which issue(s) this PR fixes**: Fixes # **What this PR does / why we need it**: This is cosmetic change, it does not change behavior at all. The following rubocop configuration detects it. ``` Performance/ConstantRegexp: Enable: true ``` Benchmark result: ``` ruby 3.2.8 (2025-03-26 revision 13f495dc2c) +YJIT [x86_64-linux] Warming up -------------------------------------- interpret everytime 26.127k i/100ms interpret once 3.172M i/100ms Calculating ------------------------------------- interpret everytime 254.171k (± 2.6%) i/s (3.93 μs/i) - 1.280M in 5.040357s interpret once 33.755M (± 1.7%) i/s (29.62 ns/i) - 171.289M in 5.075867s Comparison: interpret once: 33755320.3 i/s interpret everytime: 254171.5 i/s - 132.81x slower ``` Regex should not be evaluated every time, use /o to evaluate only once because it expands constant in /#{...}/ expression. Appendix: benchmark script ```ruby require 'bundler/inline' gemfile do source 'https://rubygems.org' gem 'benchmark-ips' gem 'benchmark-memory' end Benchmark.ips do |x| ZERO_OR_MORE_SPACING = /(?:[ \t\r\n]|\z|\#.*?(?:\z|[\r\n]))*/ x.report("interpret everytime") { /(?:#{ZERO_OR_MORE_SPACING}\>)/ } x.report("interpret once") { /(?:#{ZERO_OR_MORE_SPACING}\>)/o } x.compare! end ``` **Docs Changes**: N/A **Release Note**: N/A Signed-off-by: Kentaro Hayashi <hayashi@clear-code.com>
1 parent 53bcd3c commit 8a4959f

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

lib/fluent/config/literal_parser.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ def scan_double_quoted_string
9898
else
9999
return string.join
100100
end
101-
elsif check(/[^"]#{LINE_END_WITHOUT_SPACING_AND_COMMENT}/)
102-
if s = check(/[^\\]#{LINE_END_WITHOUT_SPACING_AND_COMMENT}/)
101+
elsif check(/[^"]#{LINE_END_WITHOUT_SPACING_AND_COMMENT}/o)
102+
if s = check(/[^\\]#{LINE_END_WITHOUT_SPACING_AND_COMMENT}/o)
103103
string << s
104104
end
105-
skip(/[^"]#{LINE_END_WITHOUT_SPACING_AND_COMMENT}/)
105+
skip(/[^"]#{LINE_END_WITHOUT_SPACING_AND_COMMENT}/o)
106106
elsif s = scan(/\\./)
107107
string << eval_escape_char(s[1,1])
108108
elsif skip(/\#\{/)

lib/fluent/config/v1_parser.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def parse_element(root_element, elem_name, attrs = {}, elems = [])
8383
elsif skip(/\</)
8484
e_name = scan(ELEMENT_NAME)
8585
spacing
86-
e_arg = scan_string(/(?:#{ZERO_OR_MORE_SPACING}\>)/)
86+
e_arg = scan_string(/(?:#{ZERO_OR_MORE_SPACING}\>)/o)
8787
spacing
8888
unless skip(/\>/)
8989
parse_error! "expected '>'"
@@ -98,7 +98,7 @@ def parse_element(root_element, elem_name, attrs = {}, elems = [])
9898
new_e.v1_config = true
9999
elems << new_e
100100

101-
elsif root_element && skip(/(\@include|include)#{SPACING}/)
101+
elsif root_element && skip(/(\@include|include)#{SPACING}/o)
102102
if !prev_match.start_with?('@')
103103
@logger.warn "'include' is deprecated. Use '@include' instead" if @logger
104104
end

0 commit comments

Comments
 (0)