Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buffer growth strategy

Tags:

c

buffer

I have a generic growing buffer indended to accumulate "random" string pieces and then fetch the result. Code to handle that buffer is written in plain C.

Pseudocode API:

void write(buffer_t * buf, const unsigned char * bytes, size_t len);/* appends */
const unsigned char * buffer(buffer_t * buf);/* returns accumulated data */

I'm thinking about the growth strategy I should pick for that buffer.

I do not know if my users would prefer memory or speed — or what would be the nature of user's data.

I've seen two strategies in the wild: grow buffer by fixed size increments (that is what I've currently implemented) or grow data exponentially. (There is also a strategy to allocate the exact amount of memory needed — but this is not that interesting in my case.)

Perhaps I should let user to pick the strategy... But that would make code a bit more complex...

Once upon a time, Herb Sutter wrote (referencing Andrew Koenig) that the best strategy is, probably, exponential growth with factor 1.5 (search for "Growth Strategy"). Is this still the best choice?

Any advice? What does your experience say?

like image 759
Alexander Gladysh Avatar asked Feb 15 '10 21:02

Alexander Gladysh


People also ask

What is Buffer Strategy?

Buffer strategies are critical linkages in maintaining smooth flows to customers. These buffers may be in the form of inventory, capacity, or time. Organizations primarily utilize inventory as a buffer, because it is by far the easiest to quantify, track, and control.

What is a buffer in investment?

Buffer ETFs are funds that seek to provide investors with the upside of an asset's returns (generally up to a capped percentage) while also providing downside protection on the first predetermined percentage of losses (for example, on the first 10% or 15%).


2 Answers

Unless you have a good reason to do otherwise, exponential growth is probably the best choice. Using 1.5 for the exponent isn't really magical, and in fact that's not what Andrew Koenig originally said. What he originally said was that the growth factor should be less than (1+sqrt(5))/2 (~1.6).

Pete Becker says when he was at Dinkumware P.J. Plauger, owner of Dinkumware, says they did some testing and found that 1.5 worked well. When you allocate a block of memory, the allocator will usually allocate a block that's at least slightly larger than you requested to give it room for a little book-keeping information. My guess (though unconfirmed by any testing) is that reducing the factor a little lets the real block size still fit within the limit.

References: I believe Andrew originally published this in a magazine (the Journal of Object Oriented Programming, IIRC) which hasn't been published in years now, so getting a re-print would probably be quite difficult.

Andrew Koenig's Usenet post, and P.J. Plauger's Usenet post.

like image 199
Jerry Coffin Avatar answered Sep 20 '22 00:09

Jerry Coffin


The exponential growth strategy is used throughout STL and it seems to work fine. I'd say stick with that at least until you find a definite case where it won't work.

like image 28
Blindy Avatar answered Sep 23 '22 00:09

Blindy