Skip to content

[JSC] Startup: size the atom table for a bun VM, read cgroup limits once, hand the structure arena over as zeroed - #554

Merged
Jarred-Sumner merged 3 commits into
mainfrom
robobun/9dfcc910/startup-atom-table
Sep 3, 2026
Merged

Jarred-Sumner merged 3 commits into
mainfrom
robobun/9dfcc910/startup-atom-table

Conversation

@robobun

@robobun robobun commented Sep 3, 2026 •

Copy link
Copy Markdown
Collaborator

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

  • 318618@main reserves the atom table for 2048 keys, which HashTable sizes at 8192 slots. A bun VM holds about 1250 atoms after startup, and HashTable shrinks a table that is less than a sixth full. So the first atom that a parser frees halves the table again, on every start: a rehash of every atom (about 70 us in a profile of bun -e 1) and a 48 KB zeroed table used for nothing.
  • With /proc/self/cgroup = 0::/ (a container), the cgroup v2 walk starts at /sys/fs/cgroup/. The trailing slash fails the root check, so memory.max, memory.high and cpu.max are read twice.
  • StructureMemoryManager hands its fresh reservation to mimalloc with is_zero=false, so mimalloc zeroes the bookkeeping of all 65536 slices of the 4 GB region up front (about 40 KB of pages).

Fix

  • Under 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.
  • Build the root cgroup path without the trailing slash, in both the memory and the CPU walk.
  • Pass is_zero=true. An anonymous mapping (Linux, macOS) and a MEM_RESERVE region (Windows) read as zero once committed. The flag also tells mimalloc that a slice it has not handed out yet is zero (slices_dirty starts clear), so a zeroed allocation from a fresh slice skips its memset. That holds for the same reason.
  • Verified: each file compiles against the current headers. The atom table numbers come from gdb on a release build: the table metadata at the shrink read 1244 keys in 8192 slots. bun will run its suites against the preview build of this PR.

Background

  • The atom table is the per-thread set of unique strings. Every identifier that JSC parses goes through it.
  • In bun, JSC links bun's mimalloc. The structure heap is an exclusive mimalloc arena inside the reservation.

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.
@coderabbitai

coderabbitai Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Essentials

Run ID: 062e75d9-bee8-4dde-bb24-4a9bc13f2fa4

📥 Commits

Reviewing files that changed from the base of the PR and between 72e75e4 and 7da3f7e.

📒 Files selected for processing (3)
  • Source/JavaScriptCore/heap/StructureAlignedMemoryAllocator.cpp
  • Source/JavaScriptCore/runtime/VM.cpp
  • Source/WTF/wtf/uv_get_constrained_memory.cpp

Included review availability: Your plan provides up to 5 included reviews per hour; 1 remains after this review.


Walkthrough

The 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.

Changes

Structure-heap initialization

Layer / File(s) Summary
Structure-heap zeroing
Source/JavaScriptCore/heap/StructureAlignedMemoryAllocator.cpp
MIMALLOC treats freshly reserved structure-heap memory as already zeroed.

Atom-string reservation

Layer / File(s) Summary
VM atom-string capacity
Source/JavaScriptCore/runtime/VM.cpp
Bun builds reserve 1024 atom-string keys. Other builds continue to reserve 2048 keys.

Cgroup root paths

Layer / File(s) Summary
Cgroup v2 path handling
Source/WTF/wtf/uv_get_constrained_memory.cpp
Memory and CPU limit lookups resolve empty cgroup paths to /sys/fs/cgroup without a trailing slash. Non-empty paths retain their existing suffix.】【。

Merge Risk: ⚪ Minimal · up to 7da3f

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)

Check name Status Explanation Resolution
Description check ⚠️ Warning 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-fi… Add the required bug title and Bugzilla link, include the reviewed-by line, and list the changed paths and relevant functions in the template format.
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the three startup optimizations: atom-table sizing, cgroup limit reads, and zeroed structure-arena handoff. It is specific and related to the changes.
Full details: Description check

Explanation

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.

  • Fix all pre-merge checks with AI

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 path_filters to narrow the review scope.


Comment @coderabbitai help to get the list of available commands.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_ex signature vs. call site — 5th arg is is_zero; region is a fresh tryReserveUncommittedAligned reservation, so zero-on-commit holds on Linux/macOS/Windows.
  • cgroup v2 walk in both memory and CPU helpers — n == 0 branch produces /sys/fs/cgroup, matching the existing strcmp break 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.

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown

Preview Builds

Commit Release Date
7da3f7ee autobuild-preview-pr-554-7da3f7ee 2026-09-03 03:18:42 UTC

Jarred-Sumner pushed a commit to oven-sh/bun that referenced this pull request Sep 3, 2026
### 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>
Jarred-Sumner pushed a commit to oven-sh/bun that referenced this pull request Sep 3, 2026
#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 -->
@Jarred-Sumner
Jarred-Sumner merged commit 64f49fc into main Sep 3, 2026
48 checks passed
dylan-conway added a commit to oven-sh/bun that referenced this pull request Sep 4, 2026
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
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