Repository navigation
[JSC] Startup: size the atom table for a bun VM, read cgroup limits once, hand the structure arena over as zeroed - #554
Conversation
318618@main reserves the current thread's atom table for 2048 keys when a VM is created, which HashTable sizes at 8192 slots. A bun VM holds about 1250 atoms once it has started, and HashTable shrinks a table that is less than a sixth full (1365 keys at 8192 slots). So the first atom a parser freed (the parse of bun's entry wrapper, on every start) halved the table again: a rehash of every atom, about 70 us, and a 48 KB zeroed table that was used for nothing. Under BUN_JSC_ADDITIONS, reserve for 1024 keys. That gives 4096 slots, which hold 1250 atoms with no shrink and grow at 2048 as before.
…root cgroup With /proc/self/cgroup = "0::/" (the usual case in a container) the walk started at "/sys/fs/cgroup/". The trailing slash failed the "reached the root" check, so the loop stripped it and read memory.max, memory.high and cpu.max again from "/sys/fs/cgroup". Build the root path without the slash. Three fewer file reads (nine syscalls) at VM creation.
The region is a fresh reservation (an anonymous mapping, or MEM_RESERVE on Windows), so it reads as zero once committed. Passing is_zero=false made mimalloc zero the arena's bookkeeping for every slice of the region up front: about 40 KB of pages touched for the 4 GB default before the first Structure is allocated, and every slice marked dirty.
|
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 (3)
Included review availability: Your plan provides up to 5 included reviews per hour; 1 remains after this review. WalkthroughThe changes adjust MIMALLOC structure-heap initialization, configure Bun’s atom-string table reservation, and correct Linux cgroup v2 root paths for memory and CPU limit lookups. ChangesStructure-heap initialization
Atom-string reservation
Cgroup root paths
Merge Risk: ⚪ Minimal · up to This change reduces startup overhead by right-sizing Bun atom-table reservation, avoiding duplicate root cgroup limit reads, and skipping unnecessary initialization work for fresh allocator memory. The supplied evidence shows no remaining merge-blocking risk. 🚥 Pre-merge checks | ✅ 3 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (3 passed)
Full details: Description checkExplanation The description clearly explains the problems, fixes, background, and verification, but it does not follow the repository template. It omits the bug title and Bugzilla URL, review line, and changed-file/function list.
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 |
There was a problem hiding this comment.
I reviewed this PR and didn't find any bugs. The atom-table sizing and cgroup-path changes are straightforward, but the is_zero=true handoff to mimalloc for the structure heap arena also flips the slices_dirty bookkeeping (arena.c:1472-1477), not just the 40 KB memset skip — a quick human look at that is worthwhile given the structure heap's role.
What was reviewed:
mi_manage_os_memory_exsignature vs. call site — 5th arg isis_zero; region is a freshtryReserveUncommittedAlignedreservation, so zero-on-commit holds on Linux/macOS/Windows.- cgroup v2 walk in both memory and CPU helpers —
n == 0branch produces/sys/fs/cgroup, matching the existingstrcmpbreak condition; non-root paths unchanged. - Atom-table reservation is a Bun-guarded tuning constant; HashTable growth/shrink semantics unchanged.
Extended reasoning...
Overview
Three independent startup micro-optimizations. Source/JavaScriptCore/runtime/VM.cpp lowers the initial AtomStringTable reservation from 2048 to 1024 keys under USE(BUN_JSC_ADDITIONS) to avoid an immediate shrink/rehash after Bun's ~1250 startup atoms. Source/WTF/wtf/uv_get_constrained_memory.cpp avoids a trailing slash when /proc/self/cgroup is 0::/, so the ancestor walk reads /sys/fs/cgroup/{memory.max,memory.high,cpu.max} once instead of twice. Source/JavaScriptCore/heap/StructureAlignedMemoryAllocator.cpp flips the is_zero argument to mi_manage_os_memory_ex from false to true for the freshly reserved structure-heap region.
Security risks
The atom-table and cgroup changes carry no security weight — one is a capacity hint, the other rewrites a local path string with the same components. The mimalloc change is the one that touches a security-relevant subsystem: the structure heap backs all Structure objects and underlies StructureID encoding. Reading mi_manage_os_memory_ex2 in Source/bmalloc/mimalloc/mimalloc/src/arena.c, is_zero=true (a) skips the _mi_memzero of the info slices and (b) leaves slices_dirty clear for the non-info slices instead of marking the whole arena dirty. Both are sound because the region comes from OSAllocator::tryReserveUncommittedAligned and is_committed=false is still passed, so every slice is committed fresh by mimalloc and reads zero on all supported OSes. JSC also does not rely on mimalloc-provided zeroing for MarkedBlock payloads. I see no exposure, but the second effect is broader than the "skip a 40 KB memset" framing in the PR description.
Level of scrutiny
Low for VM.cpp and the cgroup helper — mechanical, well-commented, and behavior-preserving (the cgroup change only removes a redundant read of the same files). Medium for the mimalloc flag: it is a one-token flip with a correct rationale, but it changes allocator metadata semantics in a heap that JSC treats as security-sensitive, and .github/CODEOWNERS routes Source/JavaScriptCore to a reviewer group. That combination argues for a human sign-off rather than auto-approval.
Other factors
No findings from the bug hunt (exit: dry_streak). No prior review activity on the timeline. Each change is confined to Bun-specific code paths (USE(BUN_JSC_ADDITIONS) guard, the USE(MIMALLOC) branch that only Bun builds, and a Bun-added WTF file), so upstream JSC behavior is untouched. The PR notes Bun's test suite will run against the preview build, which is the right validation for the arena change.
Preview Builds
|
### 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 -->
Updates `WEBKIT_VERSION` from `40e43a82a755` to `6119947592b6`. Picks up: - oven-sh/WebKit#554 — [JSC] Startup: size the atom table for a bun VM, read cgroup limits once, hand the structure arena over as zeroed - oven-sh/WebKit#559 — Keep the cycle guard on Array#join, Array#toString and Array#toLocaleString
Three startup costs that bun measured on x64 Linux. Each is its own commit. They were pushed to #553 after it had merged, so they are here.
Problem
bun -e 1) and a 48 KB zeroed table used for nothing./proc/self/cgroup=0::/(a container), the cgroup v2 walk starts at/sys/fs/cgroup/. The trailing slash fails the root check, somemory.max,memory.highandcpu.maxare read twice.StructureMemoryManagerhands its fresh reservation to mimalloc withis_zero=false, so mimalloc zeroes the bookkeeping of all 65536 slices of the 4 GB region up front (about 40 KB of pages).Fix
BUN_JSC_ADDITIONS, reserve the atom table for 1024 keys: 4096 slots, which hold 1250 atoms with no shrink and grow at 2048 as before. In bun the first module parsed is the entry wrapper, which adds about 20 atoms, so the 8192-slot table never survived. After the shrink it was at 4096 slots anyway.is_zero=true. An anonymous mapping (Linux, macOS) and aMEM_RESERVEregion (Windows) read as zero once committed. The flag also tells mimalloc that a slice it has not handed out yet is zero (slices_dirtystarts clear), so a zeroed allocation from a fresh slice skips its memset. That holds for the same reason.Background