From: merch-redmine@... Date: 2016-01-27T17:17:13+00:00 Subject: [ruby-core:73545] [Ruby trunk - Feature #12024] Add String.buffer, for creating strings with large capacities Issue #12024 has been updated by Jeremy Evans. Here's the benchmark I wrote for this: ~~~ require 'benchmark/ips' eval("def a; b = ' ' * 1000; String.new " + "<< b " * 100 + "end") eval("def c; b = ' ' * 1000; String.buffer(100000) " + "<< b " * 100 + "end") Benchmark.ips do |x| x.report("String.new - 100000/1000"){a} x.report("String.buffer - 100000/1000"){c} x.compare! end eval("def a; b = ' ' * 100; String.new " + "<< b " * 10 + "end") eval("def c; b = ' ' * 100; String.buffer(1000) " + "<< b " * 10 + "end") Benchmark.ips do |x| x.report("String.new - 1000/100"){a} x.report("String.buffer - 1000/100"){c} x.compare! end ~~~ The problem with the benchmark in note 7 is the bottleneck is high ruby execution overhead, due to individual character appends. The problem with the benchmark in note 9 is that is attempts to specifically exploit realloc issues. This benchmark is more realistic, I think. As to why `String.buffer(10000)` instead of `String.new(:buffersize=>10000)`, see https://bugs.ruby-lang.org/issues/905#note-2 ---------------------------------------- Feature #12024: Add String.buffer, for creating strings with large capacities https://bugs.ruby-lang.org/issues/12024#change-56757 * 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: