in reply to Growing strings, avoiding copying and fragmentation?

You are (probably) the only one who can answer that question. How big does your buffer get on average? How concerned are you about wasting memory? If you're not too concerned then setting SZSIZE to a really large value will probably give you the best performance. If you can accept a moderate ammount of waste then setting it to the average buffer size is probably good.

While writing HTML::Template::JIT I faced a similar problem. HTML::Template::JIT needs to accumulate generated HTML in a single buffer, but if I guess too low then the buffer might get copied around in memory during a realloc(). The solution I chose was to guess based on the size of the template and how many variables and loops were present how big the result would likely be.

Of course, whenever you're optimizing the first question should always be "does it really matter?". Try setting SZSIZE to 1 and do a benchmark run. Then set it to 8129 or some other highish value. If the difference is tiny stop immediately and do some profiling to find out where your problem really is! (Note that if I followed this advice HTML::Tempate::JIT wouldn't even exist...)

-sam

  • Comment on Re: Growing strings, avoiding copying and fragmentation?