J
Jef Driesen
Hi,
I'm implementing a buffer that grows automatically when appending or
prepending data. To improve performance, I'm looking into a buffer
growing strategy to double the memory (or more generally multiply with a
FACTOR > 1). It seems there are a number of variations in use:
1. Try to increase the current size once, and use the requested size 'n'
if that is not enough.
newsize = oldsize * FACTOR;
if (n > newsize)
newsize = n;
2. Increase the current size until the new size is large enough. (I'm
aware that I have to take extra care in case oldsize is zero, or with
multiplications with a non-integer FACTOR.)
newsize = oldsize;
while (n > newsize)
newsize *= FACTOR;
3. Similar to #2, but always using a fixed blocksize instead of the
current size. (Typically BLOCKSIZE=1 and FACTOR=2, such that the
allocated buffer is always a power of two.)
newsize = BLOCKSIZE;
while (n > newsize)
newsize *= FACTOR;
Which of these methods is preferred, and for what reason? Or is that
just a matter of personal preference?
Thanks,
Jef
I'm implementing a buffer that grows automatically when appending or
prepending data. To improve performance, I'm looking into a buffer
growing strategy to double the memory (or more generally multiply with a
FACTOR > 1). It seems there are a number of variations in use:
1. Try to increase the current size once, and use the requested size 'n'
if that is not enough.
newsize = oldsize * FACTOR;
if (n > newsize)
newsize = n;
2. Increase the current size until the new size is large enough. (I'm
aware that I have to take extra care in case oldsize is zero, or with
multiplications with a non-integer FACTOR.)
newsize = oldsize;
while (n > newsize)
newsize *= FACTOR;
3. Similar to #2, but always using a fixed blocksize instead of the
current size. (Typically BLOCKSIZE=1 and FACTOR=2, such that the
allocated buffer is always a power of two.)
newsize = BLOCKSIZE;
while (n > newsize)
newsize *= FACTOR;
Which of these methods is preferred, and for what reason? Or is that
just a matter of personal preference?
Thanks,
Jef