From: "headius (Charles Nutter)" Date: 2012-11-01T05:06:01+09:00 Subject: [ruby-core:48679] [ruby-trunk - Feature #905] Add String.new(fixnum) to preallocate large buffer Issue #905 has been updated by headius (Charles Nutter). So we have something like this: Platforms known to not support any sort of O(1) realloc: JVM Platforms that may not support O(1) realloc: OS X, others? Platforms that do (should?) support O(1) realloc: Linux In any case, I still see that there's value in this feature: * It would help JRuby and all runtimes that run on non-efficient-realloc platforms. * It does no harm and matches Array.new behavior. * For folks doing crypto stuff that want to know exactly how big the buffer is right away, this provides a way to do so. I won't try to argue whether realloc is consistently efficient across platforms or not. It seems like it's not guaranteed to be on any platform. It's also such a tiny addition...why not? ---------------------------------------- Feature #905: Add String.new(fixnum) to preallocate large buffer https://bugs.ruby-lang.org/issues/905#change-32121 Author: headius (Charles Nutter) Status: Feedback Priority: Normal Assignee: matz (Yukihiro Matsumoto) Category: Target version: next minor =begin Because Strings are used in ruby as arbitrary byte buffers, and because the cost of growing a String increases as it gets larger (especially when it starts small), String.new should support a form that takes a fixnum and ensures the backing store will have at least that much room. This is analogous to Array.new(fixnum) which does the same thing. The simple implementation of this would just add a Fixnum check to the String.new method, and the result would be an empty string with that size buffer. This would allow heavy string-appending algorithms and libraries (like ERb) to avoid doing so many memory copies while they run. =end -- http://bugs.ruby-lang.org/