Skip to content
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

[mimalloc] Halve the page size #23037

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kleisauke
Copy link
Collaborator

No description provided.

@sbc100 sbc100 requested a review from kripken December 3, 2024 02:23
@sbc100 sbc100 changed the title [mimalloc] halve the page size [mimalloc] Halve the page size Dec 3, 2024
@kripken
Copy link
Member

kripken commented Dec 3, 2024

What is the benefit here? Did you measure this @kleisauke ?

@kleisauke
Copy link
Collaborator Author

The advantage is that mimalloc uses 2 MiB alignments instead of 4 MiB, which causes emmalloc (and sbrk()) to allocate memory in smaller segments (i.e. 4 MiB instead of 8 MiB).

@@ -1,12 +1,12 @@
 initialize_emmalloc_heap()
 claim_more_memory(numBytes=48)
-claim_more_memory: claimed 0x0050ed80 - 0x0050edb0 (48 bytes) via sbrk()
+claim_more_memory: claimed 0x0050edc0 - 0x0050edf0 (48 bytes) via sbrk()
 claim_more_memory: sbrk() returned a disjoint region to last root region, some external code must have sbrk()ed outside emmalloc(). Creating a new root region
-allocate_memory(align=4194304, size=4194304 bytes)
-claim_more_memory(numBytes=8388656)
-claim_more_memory: claimed 0x0050edb0 - 0x00d0ede0 (8388656 bytes) via sbrk()
+allocate_memory(align=2097152, size=2097152 bytes)
+claim_more_memory(numBytes=4194352)
+claim_more_memory: claimed 0x0050edf0 - 0x0090ee20 (4194352 bytes) via sbrk()
 claim_more_memory: sbrk() returned a region contiguous to last root region, expanding the existing root region
-allocate_memory(align=4194304, size=4194304 bytes)
-attempt_allocate(freeRegion=0x0050ed90,alignment=4194304,size=4194304)
-attempt_allocate - succeeded allocating memory, region ptr=0x007ffffc, align=4194304, payload size=4194304 bytes)
+allocate_memory(align=2097152, size=2097152 bytes)
+attempt_allocate(freeRegion=0x0050edd0,alignment=2097152,size=2097152)
+attempt_allocate - succeeded allocating memory, region ptr=0x005ffffc, align=2097152, payload size=2097152 bytes)

(before commit 3cd6861 this issue did not occur because an internal alignment of 1 MiB in mimalloc was always enforced)

This was only verified on wasm-vips' test suite, which somehow consistently exceeds the -sINITIAL_MEMORY=1GB limit after the mimalloc v2.1.7 update.

I should also verify this on Emscripten's benchmarks. They need to be redone anyway, the previous benchmarks was done with assertions enabled in emmalloc (i.e. before commit d0a2dce), which may have affect the results.

def test_malloc_multithreading(self):
# Multithreaded malloc test. For emcc we use mimalloc here.
src = read_file(test_file('other/test_malloc_multithreading.cpp'))
# TODO measure with different numbers of cores and not fixed 4
self.do_benchmark('malloc_multithreading', src, 'Done.', shared_args=['-DWORKERS=4', '-pthread'], emcc_args=['-sEXIT_RUNTIME', '-sMALLOC=mimalloc'])

@kripken
Copy link
Member

kripken commented Dec 4, 2024

Sounds good. Yeah, if this does not regress that benchmark then it seems reasonable to me.

And meanwhile I see that this is recommended for "small systems", which probably describes most wasm usage: microsoft/mimalloc#647 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants