Skip to content

Commit 14fde9a

Browse files
committed
rubocop: reduce a regex match for performance
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 ```
1 parent 30ae991 commit 14fde9a

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

lib/fluent/env.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ def self.windows?
3535
end
3636

3737
def self.linux?
38-
/linux/ === RUBY_PLATFORM
38+
RUBY_PLATFORM.include?("linux")
3939
end
4040

4141
def self.macos?
42-
/darwin/ =~ RUBY_PLATFORM
42+
RUBY_PLATFORM.include?("darwin")
4343
end
4444
end

lib/fluent/plugin/in_tail.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def configure(conf)
184184
configure_tag
185185
configure_encoding
186186

187-
@multiline_mode = parser_config["@type"] =~ /multiline/
187+
@multiline_mode = parser_config["@type"].include?("multiline")
188188
@receive_handler = if @multiline_mode
189189
method(:parse_multilines)
190190
else

0 commit comments

Comments
 (0)