From 80c50308f9db813e999367ec5d116e2d2be9f840 Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 21 Oct 2017 23:21:05 +0000 Subject: Improve performance of string interpolation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch will add pre-allocation in string interpolation. By this, unecessary 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 ```ruby 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 ``` [Fix GH-1626] From: Nao Minami git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60320 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_string.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index b6c6214364..ed95d8013d 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -620,6 +620,11 @@ CODE assert_raise(RuntimeError) { 'foo'.freeze.concat('bar') } end + def test_concat_literals + s="." * 50 + assert_equal(Encoding::UTF_8, "#{s}x".encoding) + end + def test_count a = S("hello world") assert_equal(5, a.count(S("lo"))) -- cgit v1.2.3