Skip to content

Conversation

@kenhys
Copy link
Contributor

@kenhys kenhys commented Apr 27, 2025

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/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

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

Docs Changes:

N/A

Release Note:

N/A

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 <[email protected]>
@kenhys kenhys force-pushed the rubocop-string-include branch from 14fde9a to 70d654d Compare April 27, 2025 08:04
@kenhys
Copy link
Contributor Author

kenhys commented Apr 27, 2025

NOTE:

rubocop detects the following code, but it was a false positive.

Ofenses:

lib/fluent/command/plugin_config_formatter.rb:64:17: C: [Correctable] Performance/StringInclude: Use String#include? instead of a regex match with literal-only pattern.
        next if /::PluginHelper::/.match?(plugin_class.name)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ruby 3.2.8 (2025-03-26 revision 13f495dc2c) +YJIT [x86_64-linux]
Warming up --------------------------------------
check method with match?
                         1.291M i/100ms
check method with include?
                       744.908k i/100ms
Calculating -------------------------------------
check method with match?
                         13.453M (± 0.7%) i/s   (74.33 ns/i) -     68.399M in   5.084622s
check method with include?
                          7.220M (± 1.0%) i/s  (138.50 ns/i) -     36.500M in   5.055801s

Comparison:
check method with match?: 13452792.6 i/s
check method with include?:  7220336.0 i/s - 1.86x  slower

@kenhys kenhys requested review from Watson1978 and daipom April 28, 2025 04:03
@kenhys kenhys merged commit 08f74d4 into fluent:master Apr 28, 2025
10 checks passed
@kenhys kenhys deleted the rubocop-string-include branch April 28, 2025 04:58
@daipom daipom added this to the v1.19.0 milestone Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants