rubocop: reduce every regex evaluation for performance#4934
Merged
daipom merged 1 commit intofluent:masterfrom Apr 28, 2025
Merged
rubocop: reduce every regex evaluation for performance#4934daipom merged 1 commit intofluent:masterfrom
daipom merged 1 commit intofluent:masterfrom
Conversation
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
```
Signed-off-by: Kentaro Hayashi <hayashi@clear-code.com>
Watson1978
approved these changes
Apr 28, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
Benchmark result:
Regex should not be evaluated every time, use /o to evaluate only once because it expands constant in /#{...}/ expression.
Appendix: benchmark script
Docs Changes:
N/A
Release Note:
N/A