in reply to Growing strings, avoiding copying and fragmentation?

As the data is compressed, you don't know the final size, so you can't preallocate the entire buffer. However, you probably do know the compressed size, and it is unlikely to shrink, so why not preallocate the buffer to the compressed size and then expand from there?

You might even add a scaling factor to the compressed size. If the algoritm used averages 50% compression on the type of data your handling, the double the compressed size and allocate. If this turns out to be an over estimate and your end up shrinking, the cost is likely to be much less than rallocing the buffer many times.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.

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