-
Notifications
You must be signed in to change notification settings - Fork 79
-
Notifications
You must be signed in to change notification settings - Fork 79
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
Overriding the memory allocator is needlessly complex #285
Comments
It tries to compute memory usage as precisely as possible. MALLOC_OVERHEAD is libc's per-allocation overhead. Bookkeeping, basically. Having said that, I'm not sure if MALLOC_OVERHEAD is actually accurate for glibc or musl. |
Is there a way to find out, at runtime? |
I don't think so. I suppose MALLOC_OVERHEAD=8 is okay as a best guess, even though for musl it can be anywhere from 0 to a few dozen bytes, depending. I'm not even going to attempt to explain what glibc does; it's an eldritch horror. :-) |
😅 alright then! Now back to my proposal, WDYT? |
I don't have a strong opinion but seems fine to me. |
@chqrlie any thoughts? |
This is an interesting proposal. Here are some small problems:
A separate path should be explored too: using memory pools for fixed size objects and favoring a set of fixed sizes for builtin objects that use flexible arrays such as |
Alternative allocators such as mimalloc do provide one, so in my example above I was overriding that too.
I will do that indeed. Right now I wanted to get a pulse on the approach. If it turns out to perform poorly we can ditch it no problem. |
I took a closer look at this and well... it's not that simple. The allocators just get the state, not the mf struct so it looks like an annoying change to make for little gain. Dropping this for now. |
Rather than having the user take care of JSMallocState, take care of the bookkeeping internally (and make JSMallocState non-public since it's no longer necessary) and keep the allocation functions to the bare minimum. This has the advantage that using a different allocator is just a few lines of code, and there is no need to copy the default implementation just to moficy the call to the allocation function. Fixes: #285
Rather than having the user take care of JSMallocState, take care of the bookkeeping internally (and make JSMallocState non-public since it's no longer necessary) and keep the allocation functions to the bare minimum. This has the advantage that using a different allocator is just a few lines of code, and there is no need to copy the default implementation just to moficy the call to the allocation function. Fixes: #285
Rather than having the user take care of JSMallocState, take care of the bookkeeping internally (and make JSMallocState non-public since it's no longer necessary) and keep the allocation functions to the bare minimum. This has the advantage that using a different allocator is just a few lines of code, and there is no need to copy the default implementation just to moficy the call to the allocation function. Fixes: #285
Rather than having the user take care of JSMallocState, take care of the bookkeeping internally (and make JSMallocState non-public since it's no longer necessary) and keep the allocation functions to the bare minimum. This has the advantage that using a different allocator is just a few lines of code, and there is no need to copy the default implementation just to moficy the call to the allocation function. Fixes: #285
Rather than having the user take care of JSMallocState, take care of the bookkeeping internally (and make JSMallocState non-public since it's no longer necessary) and keep the allocation functions to the bare minimum. This has the advantage that using a different allocator is just a few lines of code, and there is no need to copy the default implementation just to moficy the call to the allocation function. Fixes: #285
I'm experimenting with using mimalloc on txiki.js and when it came to overriding the allocator in QuickJS it seems like a lot of bolierplate is needed:
This is because the allocator customization works as follows:
So if someone just wants to use
mi_malloc
and friends they need to copy the whole code to change a couple of lines.Here is a proposal: alter
JSMallocFunctions
to take the system allocator into account:This way, if all you want is to modify the system allocator to use mimalloc (for example), you'd do:
Internally QuickJS would use the default functions and delegate to the overriden system allocator.
Thoughts?
PS: Why is QuickJS doing that extra MALLOC_OVERHEAD thing?
The text was updated successfully, but these errors were encountered: