From: naruse@... Date: 2016-01-27T14:07:11+00:00 Subject: [ruby-core:73537] [Ruby trunk - Feature #12024] Add String.buffer, for creating strings with large capacities Issue #12024 has been updated by Yui NARUSE. More effective example. ``` % cat test.rb; ./ruby test.rb # frozen_string_literal: true require 'benchmark' M = 100 N = 25 Benchmark.bm 5 do |r| r.report "buffer" do j = 0 while j < M i = 0 buf = String.buffer(2**N) buf << "a" while i < N buf << buf i += 1 end j += 1 end end r.report "normal" do j = 0 while j < M i = 0 buf = String.new buf << "a" while i < N buf << buf i += 1 end j += 1 end end end user system total real buffer 0.976562 1.070312 2.046875 ( 2.048577) normal 2.085938 2.867188 4.953125 ( 4.954006) ``` ---------------------------------------- Feature #12024: Add String.buffer, for creating strings with large capacities https://bugs.ruby-lang.org/issues/12024#change-56750 * Author: Jeremy Evans * Status: Open * Priority: Normal * Assignee: ---------------------------------------- If you know you are going to need to create a large string, it's better to create it with a large capacity. Otherwise, ruby will need to continuously resize the string as it grows. For example, if you will be producing a string that is 100000 bytes, String.buffer(100000) will avoid 10 separate resizes compared to using String.new. Performance-wise, String.new is 1.33x slower than String.buffer(100000) if appending in 1000 byte chunks, and 1.64x slower than String.buffer(1000) if appending in 100 byte chunks. To make sure this works correctly with String subclasses, a static rb_str_buf_new_with_class function is added, which both String.buffer and rb_str_buf_new now call. ---Files-------------------------------- 0001-Add-String.buffer-for-creating-strings-with-large-ca.patch (3.53 KB) -- https://bugs.ruby-lang.org/ Unsubscribe: