Issue #905 has been updated by headius (Charles Nutter).


I do not believe for a moment that realloc or mremap can in all cases perform the operation in O(1) time, and the docs seem to agree with me...first based on the doc above for realloc, and then for this doc on mremap:

       MREMAP_MAYMOVE
              By default, if there is not sufficient space to expand a mapping at its current location, then mremap() fails.  If this  flag
              is specified, then the kernel is permitted to relocate the mapping to a new virtual address, if necessary.  If the mapping is
              relocated, then absolute pointers into the old mapping location become invalid (offsets relative to the starting  address  of
              the mapping should be employed).

It seems to me that preallocation is most definitely useful, even in the presence of realloc and mremap. I would like to see it added.
----------------------------------------
Feature #905: Add String.new(fixnum) to preallocate large buffer
https://bugs.ruby-lang.org/issues/905#change-30669

Author: headius (Charles Nutter)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 2.0.0


=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/