sds icon indicating copy to clipboard operation
sds copied to clipboard

malloc/realloc only powers of two?

Open ericcurtin opened this issue 5 years ago • 1 comments

It might be worth considering only allocating strings of a capacity that is a power of two (including header) to reduce memory fragmentations:

A simple code snippet to round up to the next power of two:

unsigned int v; // compute the next highest power of 2 of 32-bit v

--v;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
++v;

I can make the code contribution if you'd like.

ericcurtin avatar Dec 04 '19 13:12 ericcurtin

How much does this really help with fragmentation? It would also be more efficient to go for an approach using CLZ to calculate the next power of two, if supported.

geniiii avatar Feb 15 '20 01:02 geniiii