Commit 54c9864
authored
robocop: reduce string to symbol conversion for performance (#4929)
**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 --------------------------------------
get instance variable with string notation
918.201k i/100ms
get instance variable with symbol notation
2.186M i/100ms
Calculating -------------------------------------
get instance variable with string notation
10.480M (± 1.5%) i/s (95.42 ns/i) - 53.256M in 5.082903s
get instance variable with symbol notation
27.349M (± 1.3%) i/s (36.56 ns/i) - 137.703M in 5.035946s
Comparison:
get instance variable with symbol notation: 27349072.3 i/s
get instance variable with string notation: 10479751.9 i/s - 2.61x slower
ruby 3.2.8 (2025-03-26 revision 13f495dc2c) +YJIT [x86_64-linux]
Warming up --------------------------------------
defined? instance variable with string notation
919.834k i/100ms
defined? instance variable with symbol notation
2.333M i/100ms
Calculating -------------------------------------
defined? instance variable with string notation
9.011M (± 3.6%) i/s (110.98 ns/i) - 45.072M in 5.008998s
defined? instance variable with symbol notation
27.433M (± 2.2%) i/s (36.45 ns/i) - 137.670M in 5.021011s
Comparison:
defined? instance variable with symbol notation: 27432875.4 i/s
defined? instance variable with string notation: 9010878.0 i/s - 3.04x slower
```
Appendix: benchmark ruby script
```ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'benchmark-ips'
gem 'benchmark-memory'
end
class Dummy
def initialize
@running = nil
end
end
Benchmark.ips do |x|
@dummy = Dummy.new
x.report("get instance variable with string notation") {
@dummy.instance_variable_get("@running")
}
x.report("get instance variable with symbol notation") {
@dummy.instance_variable_get(:@running)
}
x.compare!
end
Benchmark.ips do |x|
@dummy = Dummy.new
x.report("defined? instance variable with string notation") {
@dummy.instance_variable_defined?("@running")
}
x.report("defined? instance variable with symbol notation") {
@dummy.instance_variable_defined?(:@running)
}
x.compare!
end
```
**Docs Changes**:
N/A
**Release Note**:
N/A
Signed-off-by: Kentaro Hayashi <hayashi@clear-code.com>1 parent 98a4e32 commit 54c9864
File tree
6 files changed
+10
-10
lines changed- lib/fluent
- compat
- plugin
- test
- compat
6 files changed
+10
-10
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
132 | | - | |
| 132 | + | |
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
| 29 | + | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
| 60 | + | |
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
66 | | - | |
| 66 | + | |
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
186 | 186 | | |
187 | 187 | | |
188 | 188 | | |
189 | | - | |
| 189 | + | |
190 | 190 | | |
191 | 191 | | |
192 | 192 | | |
193 | 193 | | |
194 | 194 | | |
195 | | - | |
| 195 | + | |
196 | 196 | | |
197 | 197 | | |
198 | 198 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | | - | |
| 89 | + | |
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
580 | 580 | | |
581 | 581 | | |
582 | 582 | | |
583 | | - | |
| 583 | + | |
584 | 584 | | |
585 | 585 | | |
586 | 586 | | |
| |||
728 | 728 | | |
729 | 729 | | |
730 | 730 | | |
731 | | - | |
| 731 | + | |
732 | 732 | | |
733 | 733 | | |
734 | 734 | | |
| |||
0 commit comments