Skip to content

Commit 80f3a44

Browse files
authored
rubocop: reduce string to symbol conversion for performance (#4930)
**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. It was detected by the following rubocop configuration: ``` Performance/StringIdentifierArgument: Enabled: true ``` Apparently, string identifier argument should not be used. Benchmark result: ``` ruby 3.2.8 (2025-03-26 revision 13f495dc2c) +YJIT [x86_64-linux] Warming up -------------------------------------- check method with string notation 875.323k i/100ms check method with symbol notation 1.880M i/100ms Calculating ------------------------------------- check method with string notation 8.828M (± 1.4%) i/s (113.28 ns/i) - 44.641M in 5.058002s check method with symbol notation 19.766M (± 2.2%) i/s (50.59 ns/i) - 99.631M in 5.043071s Comparison: check method with symbol notation: 19765521.3 i/s check method with string notation: 8827598.1 i/s - 2.24x 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| x.report("check method with string notation") { "test".respond_to?("start_with?") } x.report("check method with symbol notation") { "test".respond_to?(:start_with?) } x.compare! end ``` **Docs Changes**: N/A **Release Note**: N/A Signed-off-by: Kentaro Hayashi <hayashi@clear-code.com>
1 parent 54c9864 commit 80f3a44

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

lib/fluent/config/types.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def self.bool_value(str, opts = {}, name = nil)
7171
else
7272
# Current parser passes comment without actual values, e.g. "param #foo".
7373
# parser should pass empty string in this case but changing behaviour may break existing environment so keep parser behaviour. Just ignore comment value in boolean handling for now.
74-
if str.respond_to?('start_with?') && str.start_with?('#')
74+
if str.respond_to?(:start_with?) && str.start_with?('#')
7575
true
7676
elsif opts[:strict]
7777
raise Fluent::ConfigError, "#{name}: invalid bool value: #{str}"

lib/fluent/plugin.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def self.new_impl(kind, registry, type, parent=nil)
175175
else
176176
raise Fluent::ConfigError, "#{kind} plugin '#{type}' is not a Class nor callable (without arguments)."
177177
end
178-
if parent && impl.respond_to?("owner=")
178+
if parent && impl.respond_to?(:owner=)
179179
impl.owner = parent
180180
end
181181
impl.extend FeatureAvailabilityChecker

0 commit comments

Comments
 (0)