-
-
Notifications
You must be signed in to change notification settings - Fork 114
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
feat(stdlib): Faster memory allocator #2124
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I only had one comment. The new implementation is very easy to follow but I'd still love @peblair to take a look!
// Leak all available memory | ||
// The first call to malloc ensures it has been initialized | ||
Malloc.malloc(8n) | ||
Malloc.leakAll() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like leakAll being a public API on malloc
@peblair any chance you can peruse this? |
This PR is awesome! I believe the new malloc implementation makes sense to me, but I have one question: why is there no |
Small allocations just return a block from the small block list, and if none are available the code path is exactly the same as the one for large blocks, i.e. take the next large block, take a chunk off off of it, return that. If there are no blocks left in the large list then it will call morecore. |
Small blocks and large blocks have the same memory layout. The only difference is which free list they live in, which is mostly the job of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I said this during our community meeting, but I'll put it here too: FWIW, we originally exported @peblair I'll update the contributor docs. |
Thanks to @mortax for feedback on the current allocator.
The current allocator implements
malloc
andfree
in O(n) to the size of the free list. This new implementation maintains separate free lists for small allocations and large allocations, and implementsmalloc
andfree
in O(1) for small allocations and O(n) malloc to the size of the large free list and O(1) free.The cost of this extra performance is a small amount of extra memory use and additional bookkeeping. For example, a List cons cell currently uses 40 bytes of memory, but now uses 64 bytes, as all small allocations are 64 bytes. For implementation details, see the large comment in
malloc.gr
.The majority of allocations are very small, so this PR greatly improves performance of the allocator. Small allocations include:
Closes #545