-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Update mimalloc to 3.3.0 #26696
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
Open
kleisauke
wants to merge
16
commits into
emscripten-core:main
Choose a base branch
from
kleisauke:mimalloc-update-3.3.0
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Update mimalloc to 3.3.0 #26696
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ee253d1
Update mimalloc to 3.3.0 (microsoft/mimalloc@0ddf397)
kleisauke 8f4d480
mimalloc: add Emscripten-specific changes
kleisauke 57a6623
mimalloc: enable use of `__builtin_thread_pointer()`
kleisauke a4e07d4
mimalloc: remove weak decls
kleisauke 0719860
mimalloc: disable large pages by default
kleisauke 6b83c90
mimalloc: halve the page size
kleisauke a8cb678
mimalloc: ensure proper alignment
kleisauke 9e64b24
mimalloc: fix single-threaded build
kleisauke ddd3840
Rebaseline `other.test_malloc_size_mimalloc*` codesize expectations
kleisauke 02d918b
Revert "mimalloc: remove weak decls"
kleisauke 23f8935
Fix `test_wrap_malloc_mimalloc`
kleisauke 0158c62
Update ChangeLog.md
kleisauke 9607089
Fix `wasm64_4gb.test_wrap_malloc_mimalloc`
kleisauke 094d31e
Revise commit 23f8935 and 9607089
kleisauke 4207b4f
Merge branch 'main' into mimalloc-update-3.3.0
kleisauke dcf3cfd
Add TODO comment
kleisauke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| /* ---------------------------------------------------------------------------- | ||
| Copyright (c) 2024-2025, Microsoft Research, Daan Leijen | ||
| This is free software; you can redistribute it and/or modify it under the | ||
| terms of the MIT license. A copy of the license can be found in the file | ||
| "LICENSE" at the root of this distribution. | ||
| -----------------------------------------------------------------------------*/ | ||
| #pragma once | ||
| #ifndef MIMALLOC_STATS_H | ||
| #define MIMALLOC_STATS_H | ||
|
|
||
| #include <mimalloc.h> | ||
| #include <stdint.h> | ||
|
|
||
| #define MI_STAT_VERSION 5 // increased on every backward incompatible change | ||
|
|
||
| // alignment for atomic fields | ||
| #if defined(_MSC_VER) | ||
| #define mi_decl_align(a) __declspec(align(a)) | ||
| #elif defined(__GNUC__) | ||
| #define mi_decl_align(a) __attribute__((aligned(a))) | ||
| #elif __cplusplus >= 201103L | ||
| #define mi_decl_align(a) alignas(a) | ||
| #else | ||
| #define mi_decl_align(a) | ||
| #endif | ||
|
|
||
|
|
||
| // count allocation over time | ||
| typedef struct mi_stat_count_s { | ||
| int64_t total; // total allocated | ||
| int64_t peak; // peak allocation | ||
| int64_t current; // current allocation | ||
| } mi_stat_count_t; | ||
|
|
||
| // counters only increase | ||
| typedef struct mi_stat_counter_s { | ||
| int64_t total; // total count | ||
| } mi_stat_counter_t; | ||
|
|
||
| #define MI_STAT_FIELDS() \ | ||
| MI_STAT_COUNT(pages) /* count of mimalloc pages */ \ | ||
| MI_STAT_COUNT(reserved) /* reserved memory bytes */ \ | ||
| MI_STAT_COUNT(committed) /* committed bytes */ \ | ||
| MI_STAT_COUNTER(reset) /* reset bytes */ \ | ||
| MI_STAT_COUNTER(purged) /* purged bytes */ \ | ||
| MI_STAT_COUNT(page_committed) /* committed memory inside pages */ \ | ||
| MI_STAT_COUNT(pages_abandoned) /* abandonded pages count */ \ | ||
| MI_STAT_COUNT(threads) /* number of threads */ \ | ||
| MI_STAT_COUNT(malloc_normal) /* allocated bytes <= MI_LARGE_OBJ_SIZE_MAX */ \ | ||
| MI_STAT_COUNT(malloc_huge) /* allocated bytes in huge pages */ \ | ||
| MI_STAT_COUNT(malloc_requested) /* malloc requested bytes */ \ | ||
| \ | ||
| MI_STAT_COUNTER(mmap_calls) \ | ||
| MI_STAT_COUNTER(commit_calls) \ | ||
| MI_STAT_COUNTER(reset_calls) \ | ||
| MI_STAT_COUNTER(purge_calls) \ | ||
| MI_STAT_COUNTER(arena_count) /* number of memory arena's */ \ | ||
| MI_STAT_COUNTER(malloc_normal_count) /* number of blocks <= MI_LARGE_OBJ_SIZE_MAX */ \ | ||
| MI_STAT_COUNTER(malloc_huge_count) /* number of huge bloks */ \ | ||
| MI_STAT_COUNTER(malloc_guarded_count) /* number of allocations with guard pages */ \ | ||
| \ | ||
| /* internal statistics */ \ | ||
| MI_STAT_COUNTER(arena_rollback_count) \ | ||
| MI_STAT_COUNTER(arena_purges) \ | ||
| MI_STAT_COUNTER(pages_extended) /* number of page extensions */ \ | ||
| MI_STAT_COUNTER(pages_retire) /* number of pages that are retired */ \ | ||
| MI_STAT_COUNTER(page_searches) /* total pages searched for a fresh page */ \ | ||
| MI_STAT_COUNTER(page_searches_count) /* searched count for a fresh page */ \ | ||
| /* only on v1 and v2 */ \ | ||
| MI_STAT_COUNT(segments) \ | ||
| MI_STAT_COUNT(segments_abandoned) \ | ||
| MI_STAT_COUNT(segments_cache) \ | ||
| MI_STAT_COUNT(_segments_reserved) \ | ||
| /* only on v3 */ \ | ||
| MI_STAT_COUNT(heaps) \ | ||
| MI_STAT_COUNT(theaps) \ | ||
| MI_STAT_COUNTER(pages_reclaim_on_alloc) \ | ||
| MI_STAT_COUNTER(pages_reclaim_on_free) \ | ||
| MI_STAT_COUNTER(pages_reabandon_full) \ | ||
| MI_STAT_COUNTER(pages_unabandon_busy_wait) \ | ||
| MI_STAT_COUNTER(heaps_delete_wait) | ||
|
|
||
| // Size bins for chunks | ||
| typedef enum mi_chunkbin_e { | ||
| MI_CBIN_SMALL, // slice_count == 1 | ||
| MI_CBIN_OTHER, // slice_count: any other from the other bins, and 1 <= slice_count <= MI_BCHUNK_BITS | ||
| MI_CBIN_MEDIUM, // slice_count == 8 | ||
| MI_CBIN_LARGE, // slice_count == MI_SIZE_BITS (only used if MI_ENABLE_LARGE_PAGES is 1) | ||
| MI_CBIN_HUGE, // slice_count > MI_BCHUNK_BITS | ||
| MI_CBIN_NONE, // no bin assigned yet (the chunk is completely free) | ||
| MI_CBIN_COUNT | ||
| } mi_chunkbin_t; | ||
|
|
||
|
|
||
| // Define the statistics structure | ||
| #define MI_BIN_HUGE (73U) // see types.h | ||
| #define MI_STAT_COUNT(stat) mi_stat_count_t stat; | ||
| #define MI_STAT_COUNTER(stat) mi_stat_counter_t stat; | ||
|
|
||
| typedef struct mi_stats_s | ||
| { | ||
| size_t size; // size of the mi_stats_t structure | ||
| size_t version; | ||
|
|
||
| mi_decl_align(8) MI_STAT_FIELDS() | ||
|
|
||
| // future extension | ||
| mi_stat_count_t _stat_reserved[4]; | ||
| mi_stat_counter_t _stat_counter_reserved[4]; | ||
|
|
||
| // size segregated statistics | ||
| mi_stat_count_t malloc_bins[MI_BIN_HUGE+1]; // allocation per size bin | ||
| mi_stat_count_t page_bins[MI_BIN_HUGE+1]; // pages allocated per size bin | ||
| mi_stat_count_t chunk_bins[MI_CBIN_COUNT]; // chunks per page sizes | ||
| } mi_stats_t; | ||
|
|
||
| #undef MI_STAT_COUNT | ||
| #undef MI_STAT_COUNTER | ||
|
|
||
| // helper | ||
| #if __cplusplus | ||
| #define MI_STATS_ZERO_INIT { } /* empty initializer to prevent running the constructor (with msvc) */ | ||
| #else | ||
| #define MI_STATS_ZERO_INIT { 0 } /* C zero initialize */ | ||
| #endif | ||
|
|
||
| #define mi_stats_t_decl(name) mi_stats_t name = MI_STATS_ZERO_INIT; name.size = sizeof(mi_stats_t); name.version = MI_STAT_VERSION; | ||
|
|
||
| // Exported definitions | ||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| // stats from a heap | ||
| mi_decl_export bool mi_heap_stats_get(mi_heap_t* heap, mi_stats_t* stats) mi_attr_noexcept; | ||
| mi_decl_export char* mi_heap_stats_get_json(mi_heap_t* heap, size_t buf_size, char* buf) mi_attr_noexcept; // use mi_free to free the result if the input buf == NULL | ||
| mi_decl_export void mi_heap_stats_print_out(mi_heap_t* heap, mi_output_fun* out, void* arg) mi_attr_noexcept; | ||
|
|
||
| // stats from a subprocess and its heaps aggregated | ||
| mi_decl_export bool mi_subproc_stats_get(mi_subproc_id_t subproc_id, mi_stats_t* stats) mi_attr_noexcept; | ||
| mi_decl_export char* mi_subproc_stats_get_json(mi_subproc_id_t subproc_id, size_t buf_size, char* buf) mi_attr_noexcept; // use mi_free to free the result if the input buf == NULL | ||
| mi_decl_export void mi_subproc_stats_print_out(mi_subproc_id_t subproc_id, mi_output_fun* out, void* arg) mi_attr_noexcept; | ||
| // print subprocess and all its heap stats segregated | ||
| mi_decl_export void mi_subproc_heap_stats_print_out(mi_subproc_id_t subproc_id, mi_output_fun* out, void* arg) mi_attr_noexcept; | ||
|
|
||
| // stats aggregated for the current subprocess and all its heaps. | ||
| mi_decl_export bool mi_stats_get(mi_stats_t* stats) mi_attr_noexcept; | ||
| mi_decl_export char* mi_stats_get_json(size_t buf_size, char* buf) mi_attr_noexcept; // use mi_free to free the result if the input buf == NULL | ||
| mi_decl_export void mi_stats_print_out(mi_output_fun* out, void* arg) mi_attr_noexcept; | ||
|
|
||
| // add the stats of the heap to the subprocess and clear the heap stats | ||
| mi_decl_export void mi_heap_stats_merge_to_subproc(mi_heap_t* heap); | ||
|
|
||
| // stats from the subprocess without aggregating its heaps | ||
| mi_decl_export bool mi_subproc_stats_get_exclusive(mi_subproc_id_t subproc_id, mi_stats_t* stats) mi_attr_noexcept; | ||
|
|
||
| mi_decl_export char* mi_stats_as_json(mi_stats_t* stats, size_t buf_size, char* buf) mi_attr_noexcept; // use mi_free to free the result if the input buf == NULL | ||
| mi_decl_export size_t mi_stats_get_bin_size(size_t bin) mi_attr_noexcept; | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif // MIMALLOC_STATS_H |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Are we tracking these emscripten-specific changes?
Was it easy enough for you to port them to the new version?
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.
Emscripten-specific changes are tracked in commit 8f4d480, which was created via:
system/lib/mimalloc/with original mimalloc v2.1.7.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.
Nice. I wonder if its worth forking mimalloc in the emscripten-core org like we do for llvm and musl in order to keep track of these changes.
Perhaps not worth it as the changes are so small and we have the option to upstream most of them (I think?)
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.
It's probably not worth creating a fork for these small changes. The changes to
src/prim/emscripten/prim.ccould likely be upstreamed, but I'm less certain about those insrc/alloc-override.c(although commit microsoft/mimalloc@a9b895e introduced a similar Emscripten-specific change to that file). I attempted to revert those weak decls with commit a4e07d4 but it was needed fortest_wrap_malloc_mimalloc.Commit 9e64b24 may also qualify as an Emscripten-specific change, but I'm not sure whether it should be documented (I've opened PR microsoft/mimalloc#1264 for this).