emmalloc - a smaller option for malloc

1,235 views
Skip to first unread message

Alon Zakai

unread,
Mar 7, 2018, 8:08:48 PM3/7/18
to emscripten-discuss
The incoming branch now has a MALLOC option which can be either dlmalloc (the standard allocator from before) or emmalloc, which is a new allocator written to be small and simple. It's about a third of the code size of dlmalloc, which can help on small programs that use malloc/free, where sometimes malloc/free is most of the binary.

To try it, build with something like  -s 'MALLOC="emmalloc"'  . The only observable difference should be smaller code size (around 7K or so), but in some cases you may also see a performance difference - emmalloc is much simpler than dlmalloc,which usually doesn't matter but on benchmarks that stress huge amounts of tiny allocations it can be slower. For that reason dlmalloc is still the default.


- Alon

Floh

unread,
Mar 9, 2018, 7:31:03 PM3/9/18
to emscripten-discuss
Awesome, I just tested with the sokol-samples, which are quite small to begin with, and for asm.js I'm getting 10..11KByte reduction, which for most samples results in around 10%, and for wasm 4..7 KByte (15% smaller, even >30% for the smallest sample which drops from 14KByte to 10KByte). All sizes are uncompressed. I have attached 2 screenshots with the before- and after-sizes.

Really nice work, Alon :)
wasm-before-after.png
asmjs-before-after.png

Soeren Balko

unread,
Mar 4, 2019, 3:18:25 AM3/4/19
to emscripten-discuss
Just reading this now - great addition! 

On the subject of using different allocators: how easy would it be to add other allocators (jemalloc & friends)? Or put differently: how much do more complex features (multithreading, split memory etc.) depend on the allocator being dlmalloc? The motivation is that dlmalloc may not be the best allocator to handle memory fragmentation. We would hence like to play around with some other allocators to understand the cost/benefits of using them over dlmalloc.

- Soeren

Alon Zakai

unread,
Mar 4, 2019, 6:39:40 PM3/4/19
to emscripte...@googlegroups.com
It's pretty easy - you just need the allocator to use sbrk (instead of mmap or other things not currently possible in asm.js or wasm).

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages