rubocop: reduce a regex match for performance#4933
Merged
kenhys merged 1 commit intofluent:masterfrom Apr 28, 2025
Merged
Conversation
This is cosmetic change, it does not change behavior at all.
The following rubocop configuration detects it.
```
Performance/StringInclude:
Enable: true
```
Benchmark result:
```
ruby 3.2.8 (2025-03-26 revision 13f495dc2c) +YJIT [x86_64-linux]
Warming up --------------------------------------
check method with ===
411.815k i/100ms
check method with include?
1.180M i/100ms
Calculating -------------------------------------
check method with ===
3.797M (± 5.3%) i/s (263.38 ns/i) - 18.943M in 5.003267s
check method with include?
12.067M (± 3.5%) i/s (82.87 ns/i) - 61.348M in 5.090688s
Comparison:
check method with include?: 12066561.6 i/s
check method with ===: 3796826.8 i/s - 3.18x slower
ruby 3.2.8 (2025-03-26 revision 13f495dc2c) +YJIT [x86_64-linux]
Warming up --------------------------------------
check method with =~ 353.806k i/100ms
check method with include?
1.170M i/100ms
Calculating -------------------------------------
check method with =~ 3.371M (± 5.1%) i/s (296.66 ns/i) - 16.983M in 5.051124s
check method with include?
12.155M (± 1.0%) i/s (82.27 ns/i) - 60.849M in 5.006417s
Comparison:
check method with include?: 12155448.7 i/s
check method with =~: 3370848.9 i/s - 3.61x slower
```
Appendix: benchmark script
```ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'benchmark-ips'
gem 'benchmark-memory'
end
Benchmark.ips do |x|
name = "x86_64-linux"
x.report("check method with ===") {
/linux/ === name
}
x.report("check method with include?") {
name.include?("linux")
}
x.compare!
end
Benchmark.ips do |x|
triplet = "powerpc64-apple-darwin"
x.report("check method with =~") {
/darwin/ =~ triplet
}
x.report("check method with include?") {
triplet.include?("darwin")
}
x.compare!
end
```
Signed-off-by: Kentaro Hayashi <hayashi@clear-code.com>
14fde9a to
70d654d
Compare
Contributor
Author
|
NOTE: rubocop detects the following code, but it was a false positive. |
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:
Appendix: benchmark script
Docs Changes:
N/A
Release Note:
N/A