Issue #13587 has been reported by south37 (Nao Minami).

----------------------------------------
Feature #13587: Improve performance of string interpolation
https://bugs.ruby-lang.org/issues/13587

* Author: south37 (Nao Minami)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
This patch will add pre-allocation in string interpolation.
By this, unnecessary capacity resizing is avoided.

For small strings, optimized `rb_str_resurrect` operation is faster, so pre-allocation is done only when concatenated strings are large.
`MIN_PRE_ALLOC_SIZE` was decided by experimenting with local machine (x86_64-apple-darwin 16.5.0, Apple LLVM version 8.1.0 (clang - 802.0.42)).

String interpolation will be faster around 72% when large string is created.

**Before**

```
Calculating -------------------------------------
Large string interpolation
                          1.276M ( 5.9%) i/s -      6.358M in   5.002022s
Small string interpolation
                          5.156M ( 5.5%) i/s -     25.728M in   5.005731s
```

**After**

```
Calculating -------------------------------------
Large string interpolation
                          2.201M ( 5.8%) i/s -     11.063M in   5.043724s
Small string interpolation
                          5.192M ( 5.7%) i/s -     25.971M in   5.020516s
```

**Test code**

```
require 'benchmark/ips'

Benchmark.ips do |x|
  x.report "Large string interpolation" do |t|
    a = "Hellooooooooooooooooooooooooooooooooooooooooooooooooooo"
    b = "Wooooooooooooooooooooooooooooooooooooooooooooooooooorld"

    t.times do
      "#{a}, #{b}!"
    end
  end

  x.report "Small string interpolation" do |t|
    a = "Hello"
    b = "World"

    t.times do
      "#{a}, #{b}!"
    end
  end
end
```

**Patch**
https://github.com/ruby/ruby/pull/1626



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>