[ruby-cvs:67501] nobu:r60320 (trunk): Improve performance of string interpolation

nobu at ruby-lang.org nobu at ruby-lang.org
Sun Oct 22 08:21:05 JST 2017


nobu	2017-10-22 08:21:05 +0900 (Sun, 22 Oct 2017)

  New Revision: 60320

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60320

  Log:
    Improve performance of string interpolation
    
    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 (?\194?\177 5.9%) i/s -      6.358M in   5.002022s
      Small string interpolation
                                5.156M (?\194?\177 5.5%) i/s -     25.728M in   5.005731s
      ```
    
    * After
      ```
      Calculating -------------------------------------
      Large string interpolation
                                2.201M (?\194?\177 5.8%) i/s -     11.063M in   5.043724s
      Small string interpolation
                                5.192M (?\194?\177 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 <south37777 at gmail.com>

  Modified files:
    trunk/string.c
    trunk/test/ruby/test_string.rb


More information about the ruby-cvs mailing list