Skip to content

Commit c2b09d4

Browse files
authored
Use #match? method instead of #match (#4769)
**Which issue(s) this PR fixes**: Fixes # **What this PR does / why we need it**: #match method returns MatchData, it has some cost to create the MatchData object. If it does not use MatchData object, it is better to use #match? method instead. * verify ```ruby require 'bundler/inline' gemfile do source 'https://rubygems.org' gem 'benchmark-ips' gem 'benchmark-memory' end Benchmark.ips do |x| pattern = /\{.*,.*\}/ path = 'path/to/file' x.report("match") { pattern.match(path) } x.report("match?") { pattern.match?(path) } x.compare! end ``` ``` ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux] Warming up -------------------------------------- match 1.082M i/100ms match? 1.683M i/100ms Calculating ------------------------------------- match 11.116M (± 0.8%) i/s (89.96 ns/i) - 56.266M in 5.062181s match? 16.779M (± 0.5%) i/s (59.60 ns/i) - 84.142M in 5.014786s Comparison: match?: 16779085.3 i/s match: 11115820.6 i/s - 1.51x slower ``` **Docs Changes**: **Release Note**: Signed-off-by: Shizuo Fujita <[email protected]>
1 parent 26812c8 commit c2b09d4

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

lib/fluent/plugin/in_tail.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def configure(conf)
147147
raise Fluent::ConfigError, "cannot use glob_policy as always with the default path_delimitor: `,\""
148148
end
149149

150-
if @glob_policy == :extended && /\{.*,.*\}/.match(@path) && extended_glob_pattern(@path)
150+
if @glob_policy == :extended && /\{.*,.*\}/.match?(@path) && extended_glob_pattern(@path)
151151
raise Fluent::ConfigError, "cannot include curly braces with glob patterns in `#{@path}\". Use glob_policy always instead."
152152
end
153153

@@ -300,7 +300,7 @@ def have_read_capability?
300300
end
301301

302302
def extended_glob_pattern(path)
303-
path.include?('*') || path.include?('?') || /\[.*\]/.match(path)
303+
path.include?('*') || path.include?('?') || /\[.*\]/.match?(path)
304304
end
305305

306306
# Curly braces is not supported with default path_delimiter
@@ -313,7 +313,7 @@ def use_glob?(path)
313313
# regular expressions as much as possible.
314314
# This is because not using `true' as a returning value
315315
# when choosing :always here.
316-
extended_glob_pattern(path) || /\{.*,.*\}/.match(path)
316+
extended_glob_pattern(path) || /\{.*,.*\}/.match?(path)
317317
elsif @glob_policy == :extended
318318
extended_glob_pattern(path)
319319
elsif @glob_policy == :backward_compatible

lib/fluent/plugin/out_secondary_file.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ def validate_compatible_with_primary_buffer!(path_without_suffix)
9898
raise Fluent::ConfigError, "out_secondary_file: basename or directory has an incompatible placeholder, remove time formats, like `%Y%m%d`, from basename or directory"
9999
end
100100

101-
if !@chunk_key_tag && (ph = placeholders.find { |placeholder| placeholder.match(/tag(\[\d+\])?/) })
101+
if !@chunk_key_tag && (ph = placeholders.find { |placeholder| placeholder.match?(/tag(\[\d+\])?/) })
102102
raise Fluent::ConfigError, "out_secondary_file: basename or directory has an incompatible placeholder #{ph}, remove tag placeholder, like `${tag}`, from basename or directory"
103103
end
104104

105-
vars = placeholders.reject { |placeholder| placeholder.match(/tag(\[\d+\])?/) || (placeholder == 'chunk_id') }
105+
vars = placeholders.reject { |placeholder| placeholder.match?(/tag(\[\d+\])?/) || (placeholder == 'chunk_id') }
106106

107107
if ph = vars.find { |v| !@chunk_keys.include?(v) }
108108
raise Fluent::ConfigError, "out_secondary_file: basename or directory has an incompatible placeholder #{ph}, remove variable placeholder, like `${varname}`, from basename or directory"

lib/fluent/plugin/parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def convert_values(time, record)
220220
end
221221

222222
def string_like_null(value, null_empty_string = @null_empty_string, null_value_regexp = @null_value_pattern)
223-
null_empty_string && value.empty? || null_value_regexp && string_safe_encoding(value){|s| null_value_regexp.match(s) }
223+
null_empty_string && value.empty? || null_value_regexp && string_safe_encoding(value){|s| null_value_regexp.match?(s) }
224224
end
225225

226226
TRUTHY_VALUES = ['true', 'yes', '1']

0 commit comments

Comments
 (0)