Repository navigation
Conversation
|
Preview build of cc0d0fd: |
robobun
force-pushed
the
robobun/768fe9fd/collect-async-if-allocated
branch
2 times, most recently
from
November 21, 2026 05:27
db66f73 to
fbd9946
Compare
robobun
force-pushed
the
robobun/768fe9fd/collect-async-if-allocated
branch
from
November 21, 2026 12:09
fbd9946 to
bf97c96
Compare
robobun
force-pushed
the
robobun/768fe9fd/collect-async-if-allocated
branch
2 times, most recently
from
November 21, 2026 13:48
3ba7fe9 to
1a8b7a6
Compare
robobun
force-pushed
the
robobun/768fe9fd/collect-async-if-allocated
branch
from
November 21, 2026 20:13
1a8b7a6 to
80e1b16
Compare
robobun
added a commit
to oven-sh/bun
that referenced
this pull request
Nov 21, 2026
The preview adds Heap::collectAsyncIfAllocated() to 499846f18f75. The pin moves to the merged commit when that pull request lands.
Bun's GC timer asks for a collection once a second, and once every 30 s
in a program that has gone quiet, in case the program has stopped
allocating. It asks with Heap::collectAsync(), which runs every request.
A program that allocates nothing gets one Eden collection per request:
it frees nothing and wakes the marking threads.
Heap::collectAsyncIfAllocated() is collectAsync() for such a hint. It
asks for the collection only if the heap has something to do:
- A cell was allocated since the last collection ended. The heap already
keeps that: a block that an allocator takes has its eden bit set, a
precise allocation is appended to the nursery, and
prepareForAllocation() resets both at the end of a collection. An Eden
collection sweeps exactly those blocks and that part of the list. So
MarkedSpace::isEdenEmpty() reads them, and no allocation path changes.
- Or a Full collection is owed. After an Eden collection that left
little room the heap makes the next collection a Full one
(m_shouldDoFullCollection), and over the critical memory threshold
every collection that has no scope is a Full one. Only the next
request runs it, also with nothing allocated. After a Full collection
none is owed.
Any other request would run as an Eden collection with nothing to sweep.
It returns whether it asked.
A byte count cannot say that no cell was allocated: a cell is counted
when its allocator has used up a free list, and a small object can own
memory that no count has.
Heap::collect() and the code of every other request do not change.
The jsc shell gets collectAsyncIfAllocated() for the tests.
Tests: JSTests/stress/bun-collect-async-if-allocated.js
JSTests/stress/bun-collect-async-if-allocated-full-only.js
robobun
force-pushed
the
robobun/768fe9fd/collect-async-if-allocated
branch
from
November 21, 2026 23:41
80e1b16 to
cc0d0fd
Compare
robobun
added a commit
to oven-sh/bun
that referenced
this pull request
Nov 22, 2026
The preview adds Heap::collectAsyncIfAllocated() to 499846f18f75. This commit is here so that CI can run. When that pull request has merged, the pin comes from main and this commit goes away.
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
Heap::collectAsync()every second, and the heap runs every request.setTimeout(() => {}, 12000)gets 10 Eden collections that free nothing.m_shouldDoFullCollection).Fix
Heap::collectAsyncIfAllocated()callscollectAsync()only if a cell was allocated since the last collection ended, or if a Full collection is owed. It returns whether it asked.MarkedSpace::isEdenEmpty()reads the eden bits of the block directories and the nursery of the precise allocations. No allocation path changes.Background
criticalGCMemoryThreshold.m_preciseAllocationsNurseryOffset.prepareForAllocation()resets both at the end of every collection.Downsides
Notes
Heap::collect(), the allocators and the code of every other request are as before. The change inheap/is 44 added lines: the entry point,MarkedSpace::isEdenEmpty(), andHeap::wantsFullCollection(), whichCollector::decideCollectionScope()now shares with the hint.BlockDirectory::snapshotUnsweptForEdenCollection()adds the eden bits to the unswept bits, and the precise allocations are swept fromm_preciseAllocationsNurseryOffsetForSweep.MarkedBlock::Handle::specializedSweep()sets a block's eden bit when it sweeps to a free list, which is the only way an allocator gets a block.BlockDirectory::prepareForAllocation()clears the bits.BlockDirectory::assertMayAccessBitsUnlocked()). The hint runs on the mutator thread.node:http,node:crypto,node:zlib,bun:sqliteand a server were loaded. 17 ns each when a collection is already requested. With only this change the binary's text has the same size as before (155,785,633 bytes):Heap::didAllocate()andLocalAllocator::allocateSlowCase()are byte for byte as long as they were.isEdenEmpty()isEdenEmpty()!lastWasFullJSTests/stress/bun-collect-async-if-allocated.jshas three runs. Two use--minEdenToOldGenerationRatio=2, so that every Eden collection leaves a Full one owing: one with the concurrent collector and one without. The third is over the critical memory threshold (--criticalGCMemoryThreshold=0.000001). The precise allocation in it is the butterfly of an array that grows to 50,000 elements. Nothing else is allocated then.JSTests/stress/bun-collect-async-if-allocated-full-only.jsruns without the generational collector and in a mini VM. A heap that collects only Full owes no collection for that alone. It also has the cell whose slow path runs the collection that the hint queued: the cell is allocated after that collection, so the next hint is a yes.JSTests/BUN-TEST-DIFFERENCES.md.collectAsyncIfAllocated()for the tests, next toidleFullGC().m_hasAllocatedSinceLastCollection. Outside compiled images it is set again after every collection, and in compiled images it also turns on the shortcuts of an Eden collection with an empty Eden.m_shouldDoFullCollection, and the hint runs that collection too. [JSC] The marking helper threads join a collection once it has shown them enough work #800 and [JSC] A marking thread pays for the wake-up of another one with marking, and an idle helper leaves the collection #838 make a collection wake fewer helper threads. They lower the cost of each collection, and this PR removes the collections that have nothing to do.Heap::collect(), gave the hint a byte floor, and kept a flag that a collection cleared as it began. A pass over the free lists kept that flag exact while a collection ran. The second version moved the pass toMarkedSpace::stopAllocating(). With the first version one test of Bun failed on Windows x64 only (bun-serve-html-build-holds-server.test.ts, 5 of 5 runs). With the byte floor, Bun made no collection when that test's server started to listen. The cause on Windows is not known, and Bun asks at listen as before since the second version.