-
Notifications
You must be signed in to change notification settings - Fork 945
Add an Emscripten/wasm implementation of primitives #822
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
Conversation
@microsoft-github-policy-service agree [company="google"] |
@microsoft-github-policy-service agree company="google" |
Ah this is awesome -- apologies for the later reaction! I love emscripten -- the Koka compiler uses it as well and it would be good to have a more native implementation. |
I merged your PR manually into the |
Great, thanks for merging! And sorry I didn't know the PR should target I'll close this PR then. Btw, recently I saw a 5x speedup using mimalloc over dlmalloc and we have other positive reports coming in from other Emscripten users. Thanks again for mimalloc! |
// we may want to improve emmalloc to support such alignment. See also | ||
// https://github.com/emscripten-core/emscripten/issues/20645 | ||
#define MIN_EMMALLOC_ALIGN 8 | ||
#define MAX_EMMALLOC_ALIGN (1024*1024) |
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.
Coming back to this: is it possible to make the MAX_EMMALLOC_ALIGN
equal to MI_SEGMENT_ALIGN
(=4MiB) ? As it is now, it will never use the right alignment, and mimalloc will retry with size + 4MiB
and align within that area itself. (see os.c:mi_os_prim_alloc_aligned
)
Thanks!
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.
Actually I see we fixed this in emmalloc in emscripten-core/emscripten#20704 😄
So, yes, the MAX_EMMALLOC_ALIGN
can be removed.
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 opened emscripten-core/emscripten#21905 on Emscripten to verify that all tests pass with that, and after that will open a PR here.
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.
fyi, we posted about the benefits of mimalloc in Emscripten here: https://web.dev/articles/scaling-multithreaded-webassembly-applications?hl=en#mimalloc Thanks again for the great allocator! |
This PR adds a
prim
implementation for the Emscripten wasm toolchain. There is already a WASI wasm prim in mimalloc, which this is separate from. WASI tends to be used on the server, and Emscripten on the browser, and there are API differences that led to a different design here. Another difference is that Emscripten has stable support for multithreading, and so that part is fully implemented here (it was the main reason for the port, actually - to get a malloc that scales well with multiple cores).The overall idea in this implementation is to build on top of
emmalloc
as the "system allocator". emmalloc is a minimal allocator in Emscripten, so it can be used to provide a VirtualAlloc-like API. This avoids using sbrk directly, which could lead to problems with returning memory to the OS (fragmentation and leaks).There is a PR up on the Emscripten repo with all the integration into our toolchain so that people can test it out there. Also some charts with numbers, which look great!
Thank you for the mimalloc project ❤️ it was very easy to build support for, and as the numbers there show now we have an option for a malloc implementation that looks like it scales really well, in fact just as good as native builds.