Marvin Humphrey schrieb: >> * Why the "+10"? Why have a extra minimum at all? Why "10" instead >> of another number? Given your examples, for a large string the 10 is >> a trivial fraction of the total and for a small string, the efficiency >> either doesn't matter or the string quickly grows into a large string >> anyway. > > That's similar to my analysis. I think it's more useful to round up to a > multiple of the pointer size for small values. I would rather align it to the pointer size, and keep an eye on the pagesize. I did similar tricks in Tie::CArray to get decent realloc (array grow) performance. http://cpansearch.perl.org/src/RURBAN/Tie-CArray-0.15/CArray.xs #define PAGEBITS 11 /* we can also use 10 or 12, most system malloc to 12 ie 4096 */ #define MY_PAGESIZE (1 << PAGEBITS) /* 2048 byte is the size of a fresh carray */ /* This is the to-tune part: * The overall size should fit into a page or other malloc chunks. * Leave room for "some" more items, but align it to the page size. * Should small arrays (<100) be aligned at 2048 or smaller bounds? * 10 => 2048-10, 2000 => 2048-2000, 200.000 => 2048 * len is the actual length of the array, size the itemsize in bytes. */ int freesize (int len, int size) { len *= size; return max(MY_PAGESIZE-len, len - ((len >> PAGEBITS) << PAGEBITS)) / size; } freesize is the slackspace to allocate additionally, and can be inlined. grow by n: ptr = saferealloc (ptr, len + freesize(len+n, itemsize)); -- Reini Urban http://phpwiki.org/ http://murbreak.at/Thread Previous | Thread Next