wasi-libc icon indicating copy to clipboard operation
wasi-libc copied to clipboard

Investigate mimalloc

Open sunfishcode opened this issue 5 years ago • 9 comments

It'd be nice to provide alternative malloc implementations, with different code size vs performance tradeoffs.

See the comment here about mimalloc.

sunfishcode avatar Aug 29 '19 13:08 sunfishcode

Hi!

I am interested in layout of Wasm linear memory on execution environment. As far as I know, with wasm-ld it should look as follows:

[ |0 ... data ... | ... stack ... | (__heap_base) ... heap ... (initial) ... (maximum)| ],

where __heap_base is a place where heap could be started and initial usually equals ceil(__heap_base) to 2**16, so initial >= __heap_base (or even >>).

Currently, wasi-libc uses dlmalloc with disabled mmap. And this dlmalloc starts with extending its top chunk with memory_grow. Execution environments execute this call as a memory "allocating" from initial to maximum. So it turns out that if allocator isn't explicitly support __heap_base space between __heap_base and initial isn't used. And this is up to 2**16 bytes - quite a lot. And current wasi-libc allocator doesn't support __heap_base. Moreover, wasm-ld in clang 9.0.0 doesn't export __heap_base and __data_end by default. Maybe I am wrong somewhere in this.

What do you think about it, does it make sense for WASI? Shall the old or a new allocator support __heap_base?

mikevoronov avatar Oct 07 '19 14:10 mikevoronov

Yes, it does make sense to save this memory. I expect this could be done within the existing dlmalloc allocator, though I haven't looked at specifics. It would be useful for alternate allocators to do too.

wasm-ld still does support __heap_base and __data_end; it just doesn't emit them if they're not used.

sunfishcode avatar Oct 07 '19 17:10 sunfishcode

If you don't mind, I can prepare a PR for dlmalloc this week.

mikevoronov avatar Oct 07 '19 17:10 mikevoronov

@sunfishcode, I have made a fix, but It is inside dlmalloc's sys_alloc and I wanted to test it before making a PR somehow. Does wasi-libc have a test suite?

mikevoronov avatar Oct 14 '19 21:10 mikevoronov

Great! For a testsuite, I have started working on setting up libc-test here -- for licensing issues we need to keep it in a separate repo. It's admittedly not a lot yet, but it's a start.

sunfishcode avatar Oct 16 '19 18:10 sunfishcode

@sunfishcode As I understand you correctly, mimalloc shouldn't replace existing dlmalloc, but be an alternative (enable with some #define) allocator?

mikevoronov avatar Oct 20 '19 16:10 mikevoronov

I imagine it would be a link-time alternative enabled via some kind of link flag.

sbc100 avatar Oct 20 '19 18:10 sbc100

Agreed; a link-time alternative is a good way to go here. So mimalloc would be built into its own .a file, named something like libc-mimalloc.a, installed alongside libc.a, so users can link with -lc-mimalloc or so.

Of course, if mimalloc works well enough, it may make sense to make it the default, but we can figure that out once we have something we can experiment with :-).

sunfishcode avatar Oct 21 '19 12:10 sunfishcode

It sounds good, I will try to introduce __heap_base to mimalloc first and then add it to WASI.

mikevoronov avatar Oct 21 '19 20:10 mikevoronov