Skip to content

stats: a merge skips a count that the source never touched - #58

Closed
robobun wants to merge 1 commit into
oven-sh:bun-dev3-v2from
robobun:robobun/06c2fb17/stats-merge-skips-zero-counts
Closed

robobun wants to merge 1 commit into
oven-sh:bun-dev3-v2from
robobun:robobun/06c2fb17/stats-merge-skips-zero-counts

Conversation

@robobun

@robobun robobun commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator

Problem

  • mi_heap_new + mi_heap_destroy takes 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 for Bun.color("red", "number") (813 to 1157 ns) and transformSync("") (2648 to 3865 ns).
  • The time goes to the statistics merge. 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. A mi_stats_t has about 180 counts, so a merge is about 180 locked instructions. mi_heap_delete and mi_heap_destroy merge three times (src/heap.c:198, :218, :252).
  • Before Sync upstream dev3 (2026-09-12, 636510a3, v3.5.2) #37 a release build left the 74 malloc_bins out 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_mt returns 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.
  • Every merge goes through this function: heap delete and destroy, thread exit, a collect (collect_merges_stats), mi_stats_merge, mi_stats_get.
  • One difference: for an all-zero source the old code still raised the destination's peak to its current value when the peak was lower. mi_stat_update_mt and mi_stat_adjust_mt return early for a zero amount in the same way.

Numbers

mi_heap_new, n x mi_heap_malloc, mi_heap_destroy on one thread. x64 Linux, clang 23, bun's flags for static.c. Instructions are counted by single-stepping the thread. Time is the minimum of 9 interleaved processes.

per cycle instructions locked instructions ns
n=0, 6a64e1ba (before #37) 4109 105 620
n=0, eab09015 5499 181 959
n=0, this PR 4508 11 314
n=1 (64 bytes), 6a64e1ba 14691 341 2231
n=1, eab09015 19332 591 3407
n=1, this PR 16471 93 1464

The pins between #37 and eab09015 (#40 to #52) do not change these counts: 181 locked instructions per cycle at ab135013, 69050cf3 and eab09015.

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:

bun 1.4.2 main (eab09015) main with this PR
Bun.color([255, 0, 0], "hex") (no heap) 133 130 130
Bun.color("red", "number") 813 1157 512
Bun.color("#ff8800", "css") 1576 2266 938
transformSync("") 2648 3865 1856
transformSync("export const a = 1;\n") 6754 8692 6933

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 uses mprotect and sigaction):

  1. a merge adds up: totals and current values are summed, the peak follows the rule of mi_stat_count_add_mt, and the source is empty afterwards;
  2. a merge of a block that counted nothing does not write to the destination. The destination is mapped read-only. Without the change this part fails: FAILED a merge of an empty block of statistics wrote to the destination;
  3. the counts of 64 heaps that are created, used and destroyed arrive in mi_stats_get.

It passes here in release and debug (MI_DEBUG=FULL) builds as C and as C++ (MI_USE_CXX=ON), and with MI_SECURE=ON. The branch starts at eab09015, the commit that bun's main pins, so that bun can pin this commit without #34.

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`.
@Jarred-Sumner

Copy link
Copy Markdown
Collaborator

Landed as part of #59, which includes this commit (924b5a0).

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