Add embedder defined allocator support (mimalloc example) #655
Add embedder defined allocator support (mimalloc example) #655jschwe wants to merge 1 commit intoservo:mainfrom
Conversation
ad7c039 to
80f19f3
Compare
eca2ec9 to
7dc5bc5
Compare
Signed-off-by: Jonathan Schwender <[email protected]>
7dc5bc5 to
6c67dd8
Compare
Indeed. I think JS_USE_CUSTOM_ALLOCATOR should guard the code in this block: https://searchfox.org/firefox-main/rev/af3c77e22f88ca06e1b4ac822c1d056f263a64fe/js/public/Utility.h#355 not the thread code too.
Sounds like we could upstream some fixes.
I understand the need for this (we cannot inline without LTO - maybe we should investigate thinlto support in mozjs artifacts), but this design is complex (hate environment variables), I am wondering how we could make use of this in servo (for linux where we use jemalloc). One thing that I loved about webeef solution is that it's easy to pass overrides, and we could possibly do this without changing artifacts (always use prefixed alloc, but provide "own" in mozjs as default). Does inlining really makes difference in practice? |
Yes - the issue is that the happy path of allocators is highly optimized to be just a few instructions. Adding a function call is measurable overhead (since it saves and restores registers on the stack). How measurable depends of course on the frequency of allocations, but it really adds up. (This is based on discussions with a colleague who is responsible for our in-house custom allocator - I didn't measure myself).
I have a half-baked PR for servo, which uses this feature. The main issue is that it adds another thing to mach (due to the environment variable). Otherwise, nothing much is really required on the servo side.
This is mainly a tradeoff between baking in the function calls statically (via
I'm not sure if they would appreciate it. The intention seems to be that if the arena allocator is OOM, then they fall back to |
|
If we published I haven't thought this compleltely through, so perhaps something prevents that solution. It would make the publish pipeline for alloc related changes quite ugly though. |
This is a PR to explore adding support for using an allocator provided by the servo / the embedder.
An example is provided (
examples/embedder_allocator), which usesmimallocto show how an embedder is expected to provide a custom allocator for Spidermonkey to use. To summarize:This design ensures that the allocator functions can be inlined, and minimize boilerplate for allocators which provide prefixed versions of standard allocator APIs.
This PR does not eliminate all usages of standard malloc / free, but covers the most important ones. This is not a major issue, as long as the remaining usages are isolated in the respective third-party library.
Most significantly,
icuis not covered, however that is exposed viaJS_SetICUMemoryFunctions, so we could initialize that from the embedder / servo side.Notes:
-DJS_USE_CUSTOM_ALLOCATORdoesn't seem to be actually useful / maintained, since any custom implementation still need to copy lots of code fromUtility.hmoz_arena_allocated pointers can be freed / re-alloced by unprefixedfree(), this lead to a patch to StringBuffer.h