formatter_out_file: Improve performance by using string interpolation (#5281)
**Which issue(s) this PR fixes**:
Fixes #
**What this PR does / why we need it**:
Replaced `String#<<` with string interpolation (`#{}`) when building the
output line.
This avoids unnecessary buffer expansions (reallocations) and
significantly improves performance, particularly on Windows.
## Micro benchmark
```ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'benchmark-ips'
end
Benchmark.ips do |x|
tag = "sample"
delimiter = "\t"
data = "x" * 256
x.report("interpolation") {
"#{tag}#{delimiter}#{data}"
}
x.report("concatenation") {
output = ''
output << tag << delimiter << data
output
}
x.compare!
end
```
* Result on Windows
```
ruby 4.0.1 (2026-01-13 revision e04267a14b) +PRISM [x64-mingw-ucrt]
Warming up --------------------------------------
interpolation 1.591M i/100ms
concatenation 453.294k i/100ms
Calculating -------------------------------------
interpolation 15.930M (± 1.4%) i/s (62.77 ns/i) - 81.148M in 5.094948s
concatenation 6.068M (±22.3%) i/s (164.79 ns/i) - 29.011M in 5.034606s
Comparison:
interpolation: 15930005.2 i/s
concatenation: 6068356.8 i/s - 2.63x slower
```
* Result on Linux
```
ruby 4.0.1 (2026-01-13 revision e04267a14b) +PRISM [x86_64-linux]
Warming up --------------------------------------
interpolation 1.831M i/100ms
concatenation 1.113M i/100ms
Calculating -------------------------------------
interpolation 18.224M (± 1.4%) i/s (54.87 ns/i) - 91.567M in 5.025463s
concatenation 10.988M (± 1.9%) i/s (91.01 ns/i) - 55.664M in 5.067939s
Comparison:
interpolation: 18224325.9 i/s
concatenation: 10987507.2 i/s - 1.66x slower
```
### Fluentd benchmark
Run `rake benchmark:run:in_tail` with 10 GB data on Windows.
**~7% faster** by this changing.
* Before
```
user system total real
38.438000 18.079000 56.517000 ( 48.443897)
```
* After
```
user system total real
35.078000 17.625000 52.703000 ( 45.104038)
```
**Docs Changes**:
N/A
**Release Note**:
* formatter_out_file: Improve performance by using string interpolation
Signed-off-by: Shizuo Fujita <fujita@clear-code.com> S
Shizuo Fujita committed
7c665e995910354e866a4bfbf7d566a58c3adde6
Parent: 4cc94dc
Committed by GitHub <noreply@github.com>
on 3/16/2026, 5:57:53 AM