Repository navigation
Conversation
A block of statistics (`mi_stats_t`) has about 180 counts: one per statistic and one per size
bin for blocks, pages and chunks. `mi_stat_count_add_mt` added every count of the source into
the destination with an atomic add, also when the source count was all zero. That is one locked
instruction per count.
`mi_heap_delete` and `mi_heap_destroy` merge three times (theap into heap twice, heap into the
main heap), and almost all counts of a short-lived heap are zero. Since the sync with upstream
v3.5.2 the statistics are always on (`MI_STATS=1`) and a merge also walks the 74 `malloc_bins`:
it went from about 100 to about 180 locked adds.
A merge now returns early for a count whose total, peak and current value are all zero in the
source. A count with any value in it is merged as before.
`mi_heap_new` + n x `mi_heap_malloc` + `mi_heap_destroy` on one thread, x64 Linux, clang -O3,
per cycle (instructions counted by single-stepping, time is the minimum of 9 runs):
instructions locked instructions ns
n=0 before 5499 181 959
after 4508 11 314
n=1 (64 B) before 19332 591 3407
after 16471 93 1464
One difference: for an all-zero source the old code still raised the destination's peak to its
current value if the peak was lower. `mi_stat_update_mt` and `mi_stat_adjust_mt` return early
for a zero amount in the same way.
`test-stats-merge`: a merge adds up, a merge of an empty block does not write to a read-only
destination (this fails without the change), and the counts of destroyed heaps arrive in
`mi_stats_get`.
This was referenced Oct 4, 2026
Collaborator
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
mi_heap_new+mi_heap_destroytakes about 55% longer since the sync with upstream v3.5.2 (Sync upstream dev3 (2026-09-12, 636510a3, v3.5.2) #37): 620 ns before, 959 ns now for a heap that allocates nothing, and 2231 ns before, 3407 ns now with one block (x64 Linux, bun's compile flags). bun pays this on each call of an API that parses in an arena of its own. It is the step between bun 1.4.2 and main forBun.color("red", "number")(813 to 1157 ns) andtransformSync("")(2648 to 3865 ns).mi_stat_count_add_mt(src/stats.c:99) adds each count of the source with an atomic add, also a count that is all zero. Ami_stats_thas about 180 counts, so a merge is about 180 locked instructions.mi_heap_deleteandmi_heap_destroymerge three times (src/heap.c:198,:218,:252).malloc_binsout of a merge (#if MI_STAT>1). The statistics are always on now (MI_STATS=1), so a merge went from about 100 to about 180 locked adds.Fix
mi_stat_count_add_mtreturns early when the total, the peak and the current value of the source count are all zero. A count with a value in it is merged with the same operations as before.collect_merges_stats),mi_stats_merge,mi_stats_get.mi_stat_update_mtandmi_stat_adjust_mtreturn early for a zero amount in the same way.Numbers
mi_heap_new, n xmi_heap_malloc,mi_heap_destroyon one thread. x64 Linux, clang 23, bun's flags forstatic.c. Instructions are counted by single-stepping the thread. Time is the minimum of 9 interleaved processes.6a64e1ba(before #37)eab090156a64e1baeab09015The pins between #37 and
eab09015(#40 to #52) do not change these counts: 181 locked instructions per cycle atab135013,69050cf3andeab09015.bun, release build of oven-sh/bun
bb35d1b8, only the mimalloc commit differs. ns per call, minimum of 42 runs of 100000 calls in 6 interleaved processes:eab09015)Bun.color([255, 0, 0], "hex")(no heap)Bun.color("red", "number")Bun.color("#ff8800", "css")transformSync("")transformSync("export const a = 1;\n")Cost for a merge that the skip cannot help: a block in which every count has a value takes 4243 instructions in place of 3740 (one more branch per count), with the same 535 locked instructions.
Not changed here: a destroy still reads the counts three times and clears the source three times. With no merge at all a cycle takes 244 ns (n=0) and 1080 ns (8 blocks of 256 bytes), against 314 and 1438 with this PR.
Test
test/test-stats-merge.c(not on Windows, it usesmprotectandsigaction):mi_stat_count_add_mt, and the source is empty afterwards;FAILED a merge of an empty block of statistics wrote to the destination;mi_stats_get.It passes here in release and debug (
MI_DEBUG=FULL) builds as C and as C++ (MI_USE_CXX=ON), and withMI_SECURE=ON. The branch starts ateab09015, the commit that bun's main pins, so that bun can pin this commit without #34.