Repository navigation
[JSC] WarmUpBlockProvider: start the helper thread after 64 block requests, not the first - #553
Conversation
…BlockStartAfterBlocks block requests Bringing up a VM takes a few dozen MarkedBlocks and a short program never allocates many more. For it the helper was a thread to create (with the allocator's scavenger thread behind it), a 512 KiB supply to fault in, and two more threads to tear down at exit: about 120 page faults and 0.2 ms of a `bun file.js` start. The provider now stays dormant until the process has requested 64 blocks (the new option); a ramping heap passes that within its first milliseconds of allocation and the helper then fills the supply as before.
…BlockStartAfterBlocks block requests Bringing up a VM takes a few dozen MarkedBlocks and a short program never allocates many more. For it the helper was a thread to create (with the allocator's scavenger thread behind it), a 512 KiB supply to fault in, and two more threads to tear down at exit: about 120 page faults and 0.2 ms of a `bun file.js` start. The provider now stays dormant until the process has requested 64 blocks (the new option); a ramping heap passes that within its first milliseconds of allocation and the helper then fills the supply as before.
There was a problem hiding this comment.
I reviewed this PR and didn't find any bugs. The change is small and the atomic threshold logic is correct, but since it sits on the MarkedBlock allocation hot path and the author noted the JSC test suite wasn't run, a human sanity-check of the default (64) and a test run would still be worthwhile.
What was reviewed:
isPastStartThreshold(): relaxed atomics are sound here (no cross-thread data dependency;singleton()'scall_onceprovides its own sync);fetch_add(1)+1correctly counts post-increment, and setting the option to 0 restores the old start-on-first-request behavior.- Function-local
static std::atomicinit is constant/thread-safe;requestsmay over-count briefly untilpastpropagates, which is harmless. - New
warmUpMarkedBlockStartAfterBlocksoption follows the exact pattern of the adjacentwarmUpMarkedBlock*entries in OptionsList.h. warmUpMarkedBlockStateForTesting()still callssingleton()directly, so the$vmtest hook can instantiate the provider regardless of the threshold.
Extended reasoning...
Overview
This PR adds a startup-delay threshold to WarmUpBlockProvider in Source/JavaScriptCore/heap/FastMallocAlignedMemoryAllocator.cpp so that the helper thread (which pre-faults MarkedBlock pages) is not spawned until the process has requested warmUpMarkedBlockStartAfterBlocks (default 64) blocks. It introduces one new static method with two function-local relaxed atomics (past flag and requests counter), adds the gate to the existing fast-path condition in tryAllocateAlignedMemory, and registers the new tunable in Source/JavaScriptCore/runtime/OptionsList.h alongside the other warmUpMarkedBlock* options.
Security risks
None. The change only delays instantiation of an internal helper thread and adds a numeric runtime option. There is no untrusted input, no auth/crypto/permissions surface, and no change to how memory is allocated or freed — before the threshold, allocation simply falls through to the pre-existing tryFastCompactAlignedMalloc path, exactly as it does today when the provider's supply is empty.
Level of scrutiny
Moderate. The diff is ~20 lines and mechanically simple, but it lives on the GC's block-allocation hot path, so correctness of the atomics and absence of unintended side effects matter. I verified: the relaxed ordering is appropriate because these atomics gate only whether to call singleton(), and std::call_once inside singleton() supplies the necessary happens-before for the provider's construction; the counter's post-threshold over-increment is bounded and unread; function-local static std::atomic initialization is constant-initialized and thread-safe; and isEnabled() short-circuits before isPastStartThreshold(), so disabled configurations never touch the counter. Setting the option to 0 or 1 makes the first request pass, preserving the previous behavior.
Other factors
The PR description states the JSC test suite could not be run from the Bun side, only that the file compiles. The existing stress test (warm-up-marked-blocks.js) allocates well past 64 blocks and the $vm testing hook bypasses the threshold via a direct singleton() call, so tests should be unaffected — but that is reasoning, not a run. .github/CODEOWNERS (inherited from upstream) tags Source/JavaScriptCore for WebKit/jsc-reviewers; while that team doesn't map to this fork, combined with the untested-suite caveat and the hot-path location, it tips the balance toward a quick human confirmation rather than auto-approval. The default of 64 is a judgment call (VM bring-up ≈30 blocks per the description) that a maintainer familiar with Bun's startup profile should ratify.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Essentials Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 5 included reviews per hour; 2 remain after this review. WalkthroughChangesThe allocator now delays warm-up until a configurable number of MarkedBlock requests occurs. The default threshold is 64. Eligible aligned allocations then use the existing warm-up path. MarkedBlock warm-up threshold
Merge Risk: ⚪ Minimal · up to The helper thread now starts after 64 block requests instead of the first request, reducing startup overhead while preserving warm-up for allocation-heavy workloads; no actionable merge-blocking risk remains beyond normal checks and review. 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Full details: Description checkExplanation The description is detailed and directly explains the problem, fix, implementation, testing, and measured impact. It does not include the Bugzilla URL, review marker, or the template-style changed-file list, but the core change information is complete. Warning Git: CodeRabbit could not clone the repository, so clone-backed analysis was skipped and this review may be incomplete. Verify repository clone access, such as SSH credentials, before requesting another full review. If clone access is intentionally unavailable, use Comment |
Preview Builds
|
oven-sh/WebKit#553 starts it from the allocation slow path once the heap has ramped instead of at VM bring-up; on Windows that hangs JSC's sampling profiler (test/integration/datadog-pprof times out at 120 s, 3/3 runs; the same tree pinned before #553 passes). Turn useWarmUpMarkedBlocks off there until the thread start is moved off the allocation path.
### Problem - `Bun__StackCheck__initialize` (`src/jsc/bindings/wtf-bindings.cpp:298`) computes the thread's stack bounds each time it is called. The main thread calls it twice: from `main`, and again from `VirtualMachine::init`. - For the main thread on Linux, `pthread_getattr_np` reads and parses all of `/proc/self/maps`. So each `bun file.js` and `bun -e` start parses it once for nothing. ### Fix - The function returns when this thread's bounds are already set. A thread's bounds are fixed. On Windows they are the reserved range of the stack, and the stack grows inside that range. - The call in `VirtualMachine::init` stays. It sets the bound for a thread that skipped it at start (`configure_thread_no_js`). Its old comment said JSC may change the stack size, which it does not. - Verified on release builds (numbers below). CI runs the stack tests: `test/js/bun/test/expect-stack-overflow-crash.test.ts`, `test/js/bun/toml/toml.test.ts`, `test/js/bun/yaml/yaml.test.ts`, and `test/regression/issue/26142.test.ts`. Release x64 Linux, against `main`, interleaved, 600 rounds, median: | | main | this PR | |---|---|---| | `/proc/self/maps` opens, `bun empty.js` | 3 | 2 | | syscalls, `bun empty.js` | 312 | 300 | | `bun empty.js` | 4.41 ms | 4.36 ms | ### Background - `StackCheck` is bun's recursion guard for its own parsers (JS, JSON, TOML, YAML). JSC's stack limit is separate. - WTF computes the main thread's bounds once more for itself, with the same `/proc/self/maps` parse. A change to `StackBounds.cpp` in oven-sh/WebKit can remove both remaining parses. That is a separate change. <details><summary>Notes</summary> - Windows: `WTF::StackBounds` calls `GetCurrentThreadStackLimits`. A probe linked with bun's `/STACK:0x1200000,0x200000` read the same range at start and 10 MB deep in a recursion. Only the committed part grew, from 2 MB to 10 MB. The output is in the conversation below. - WTF makes the same assumption: `Thread::initializeInThread` computes a thread's bounds once, and JSC's stack limit uses that value. - I found no thread that creates a VM and skips the bound at start. Each `ThreadPool` sets it (`needs_stack_bounds` is `true` in all pools), and the watcher thread runs no JS. So the call in `VirtualMachine::init` is one thread-local check today. - `bun --version` is unchanged: it never creates a VM, so it computes the bounds once. - A forked child inherits the thread-local bounds of the thread that forked, and that thread's stack is the child's stack. So the early return holds there too. - The syscall and `/proc/self/maps` counts are from `strace -f` on the two release builds. - My local debug build and test run of this change were cut off twice by container resets. An earlier version of this change, which moved the same bound computation, passed the same stack tests. - Other startup work, measured separately: #41233 (cpuid without the libgcc constructor), oven-sh/mimalloc#32, oven-sh/WebKit#553 (merged) and oven-sh/WebKit#554. </details>
#41233) ### Problem - `bun_cpu_features()` (`src/jsc/bindings/CPUFeatures.cpp`) uses `__builtin_cpu_supports` on Linux and macOS. That builtin links libgcc's constructor `__cpu_indicator_init`, which runs a dozen cpuid instructions before `main`. Under a hypervisor each one is a VM exit. - On Windows it uses `IsProcessorFeaturePresent`. On Windows Server 2019 that returns false for SSE4.2 and AVX on CPUs that have them. The crash report then has no `CPU:` line and says "CPU lacks AVX support". ### Fix - The function runs cpuid itself on every x86_64 OS, with the XCR0 checks of compiler-rt: bits 1 and 2 for AVX, bits 5 to 7 for AVX-512 (not on macOS). - Nothing else references `__cpu_model`. So the binary has one constructor less, and the vendored compiler-rt `cpu_model` sources for the macOS x64 cross link are removed. - Verified: `test/cli/run/run-crash-handler.test.ts` checks the `CPU:` line, now on Windows too. On Windows Server 2019 it fails on main and passes here. `test/js/bun/perf/static-initializers.test.ts` expects two constructors on macOS x64. Release x64 Linux, against main, interleaved, 1500 rounds, median: | | main | this PR | |---|---|---| | `.init_array` entries | 11 | 10 | | `bun --version` | 1.033 ms, 212 faults | 1.014 ms, 209 faults | ### Background - The crash report is the only reader: its `CPU:` line, its AVX message, and the `no_avx` counters. - XCR0 tells which register state the OS saves. A CPU can have AVX while the OS does not save the YMM registers. Darwin enables the AVX-512 state at a process's first AVX-512 instruction. <details><summary>Notes</summary> - Windows x64: on a Xeon Platinum 8573C with Windows Server 2019 (build 17763), `IsProcessorFeaturePresent` returned 0 for the SSE4.2, AVX, AVX2, and AVX-512 constants. cpuid reported all four, and XCR0 was 0xe7. The report now says `CPU: sse42 popcnt avx avx2 avx512`. - Windows arm64 keeps `IsProcessorFeaturePresent`. On Windows 11 arm64 (build 26100) the canary reports `CPU: neon fp aes crc32 atomics sve`, so the test passes there. - `<cpuid.h>` and the `xgetbv` inline asm compile with clang-cl 21 next to `<windows.h>` and `<intrin.h>`. - `CachedTypes.cpp` in WebKit already calls `__get_cpuid` to avoid the same constructor. - Searched for other users of `__cpu_model`: `src/`, `packages/`, `vendor/`, the fetched deps, the cargo registry, and the undefined symbols of the prebuilt JSC. None. On Linux, libgcc's `cpuinfo.o` leaves the link with this change. CI's macOS x64 cross link checks the rest. - `-e 1` shows no difference above the noise of this VM. - The other startup work: oven-sh/mimalloc#32 (per-bin abandoned bitmaps, about 250 faults per start), oven-sh/WebKit#553 (merged: the warm-up thread starts after 64 blocks), oven-sh/WebKit#554 (atom table reservation, cgroup files, structure arena), oven-sh/WebKit#556 (main thread stack bounds without `/proc/self/maps`), #41238. </details> <!-- robobun:evidence:begin --> --- **no test proof** · iteration 0 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/bun/perf/static-initializers.test.ts, test/cli/run/run-crash-handler.test.ts <!-- robobun:evidence:end -->
Problem
WarmUpBlockProvider(upstream 320112@main) starts its helper thread on the firstMarkedBlocka process allocates. Bringing up a VM takes about 30 blocks, and a short program never asks for many more. For it the helper is a thread to create (with mimalloc's scavenger thread started behind it), a 32-block supply to fault in, and two extra threads to tear down at exit.bun empty.js(release, x64 Linux): +123 minor page faults, +0.2 ms wall, +0.6 ms CPU per process, against a program that allocates nothing after startup. The same helper makes an allocation-heavy script 19% faster (401 ms against 495 ms for 3M retained objects), so turning it off is not an option.Fix
warmUpMarkedBlockStartAfterBlocks(default 64). The provider stays dormant until the process has requested that many blocks.isPastStartThresholdcounts requests with one relaxed atomic until the threshold, then a flag short-circuits the check.JSTests/stress/warm-up-marked-blocks.jsallocates 300k objects, far past the threshold, and the$vmstate machine test reaches the provider throughsingleton().Background
MarkedBlockis a 16 KiB GC heap block.FastMallocAlignedMemoryAllocator::tryAllocateAlignedMemoryasks the provider for a block whose pages a helper thread already touched, so the first-touch fault lands on the helper rather than the mutator.singleton()), so the request count is process-wide too.This PR merged as e9b43fb with the first commit only. Three commits that I pushed to the branch after the merge (cgroup limit files, the structure heap handed over as zeroed, and the atom table reservation) are in a separate PR.