Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

malloc/realloc only powers of two? #117

Open
ericcurtin opened this issue Dec 4, 2019 · 1 comment
Open

malloc/realloc only powers of two? #117

ericcurtin opened this issue Dec 4, 2019 · 1 comment

Comments

@ericcurtin
Copy link

ericcurtin commented Dec 4, 2019

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.

@geniiii
Copy link

geniiii commented Feb 15, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants