From: Caleb Clausen Date: 2010-03-06T01:25:21+09:00 Subject: [ruby-core:28513] Re: [Feature #905] Add String.new(fixnum) to preallocate large buffer On 3/5/10, Yusuke ENDOH wrote: > 2010/3/5 Kornelius Kalnbach : >> How big would the buffer size have to be for this template? >> >>

<%= link_to @record.name, @record %>

> > Yes, it is generally difficult to determine the size. > > We may be able to estimate it by using domain knowledge in some cases. > (e.g., certain page size is empirically known as about 10KB, etc.) > But if the expectation is disappointed, it will cause wasteful memory > allocation or no speed up. Generally, a given template should expand to about the same size every time. You can't easily tell ahead of time how big a template will get, but it's almost never the case that a template is expanded once and then never again. So, maybe remember what the average expanded size is, add say a 10% fudge factor, and use that as the initial buffer size for subsequent expansions. You're not guaranteed that the expansion will never get bigger than the alloc'd size, but hopefully that will be rare. It should be a speedup in the average case, and no slower than the status quo if the guessed size is too small. The current algorithm wastes up to 49.9% of the allocated string buffer. On average it might be 25%. This strategy would reduce average memory wastage to under 10%. A reasonable _initial_ guess for the expanded size of a template that's never been expanded before would be the size of the unexpanded template.