Skip to content

perf(wsl): return orcad's startup compile memory with a malloc mmap threshold - #34187

Merged
nwparker merged 1 commit into
mainfrom
nwparker/wsl-orcad-malloc-mmap-threshold
Oct 27, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/wsl-orcad-malloc-mmap-threshold

Conversation

@nwparker

@nwparker nwparker commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 5 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​151 $\color{#cf222e}{\Huge{\mathbf{−}}}$​2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​149
Prod 9 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​78 $\color{#cf222e}{\Huge{\mathbf{−}}}$​3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​75

ELI5

When Orca's server inside WSL starts up, it uses about 70 MB of temporary memory to prepare its code. It then frees that memory, but Linux's memory allocator keeps hold of it. Starting the server with one allocator setting makes that memory go back to the system. Users' own shells and tools never see the setting.

What Changed

  • Launch: when the desktop launches its own build of orcad in a WSL distro, it adds two variables:

    • GLIBC_TUNABLES=glibc.malloc.mmap_threshold=16384;
    • a marker, ORCA_HOST_MALLOC_TUNABLES, carrying the same value.

    This goes through the existing WSL-only orcadLaunchEnv hook, which now receives the launched slot's version. withOwnBuildLaunchEnv adds the variables only when that version equals this desktop's own orcad build. An older slot, which wouldn't know to remove them, never gets them.

  • Claim at startup: orcad's and the daemon's entries import a claim module before anything else. If the marker is present and GLIBC_TUNABLES still equals it, both are removed from process.env. glibc reads them only at exec, so removing them doesn't undo the effect.

  • Daemon: orcad hands the claimed value explicitly to the daemon it spawns, on both the systemd-scope and the direct path. The daemon claims it the same way.

  • What children see: PTY shells, agents, git and anything else either process starts inherit nothing. A GLIBC_TUNABLES the user set themselves is never touched.

Why

I profiled orcad and its daemon on Linux (linux-arm64 container, pinned Node 24.22.1, managed launch with a warm compile cache; 2 shells and an idle agent-like process; 60 s of idle).

  • The cause: V8's malloc'd memory peaks at about 101 MB while it compiles the 11 MB orcad bundle, and only about 2 MB of that stays live. glibc keeps the freed space stranded in its main arena. A forced malloc_trim(0) via gdb confirmed it: 279 → 214 MB without the compile cache, 233 → 217 MB with it.
  • The fix: serving allocations of 16 KB and up from mmap returns the memory when it's freed.

Results on this branch's build, idle RSS in MB:

orcad daemon sum
without the launch env (×3: main ×1, this branch ×2) 232–233 79 311
with the launch env (×3) 193 71 264

Flood A/B (300 MB of output through a terminal, two runs each, combined with #2's semi-space change):

wall time orcad CPU daemon CPU
default 4.1 s / 5.4 s 6.1 s / 6.5 s 4.1 s / 4.8 s
tuned 4.6 s / 4.1 s 6.5 s / 6.0 s 4.5 s / 4.1 s

The differences are within noise. 60 s after the flood, orcad settles at 300–312 MB against 343 MB by default.

Alternatives I measured and didn't pick:

setting orcad idle RSS (MB)
mmap_threshold=65536 195
trim_threshold=0 199
arena_max=1 214
MALLOC_ARENA_MAX=2 (awin) no effect

Node has no API to trim malloc, so setting the variable at launch is the only route.

User-risk: low. Only the orcad and daemon processes of this build's own WSL slot run with a different malloc threshold. User processes keep their environment: on this branch's Linux build, every shell and the agent process showed no GLIBC_TUNABLES in /proc/<pid>/environ. On musl distros glibc tunables don't apply, so nothing changes there.

Flag-off: inert outside WSL. SSH launches never call the WSL hook, the desktop's own daemon never sees the marker, and an older slot isn't given it. Reverting this PR restores today's launch exactly.

Linked Issue

Internal WSL host memory work (D3 bar).

Visual Proof

N/A: no UI change. This changes the server's memory use only.

Testing

  • host-malloc-tunables.test.ts:

    • the claim removes what the launch set and keeps it only for the daemon;
    • a user-set GLIBC_TUNABLES is untouched, including one that replaced the launch's value;
    • nothing is handed on when the launch set nothing;
    • orcad's and the daemon's entries import the claim first.
  • wsl-orcad-own-build-launch-env.test.ts: this build's slot gets it, an older slot doesn't, and nothing is added before the build version is known.

  • daemon-launched-child-spawn.test.ts: the daemon gets the claimed value on both launch paths, and nothing when unset.

  • pty-subprocess-env-inheritance.test.ts: after the claim, a PTY shell's env has neither variable.

  • End to end: this branch's orcad, built for Linux and launched with the env. Shells and the agent process are clean in /proc/<pid>/environ; the daemon received it; RSS is in the table above.

  • Under the load rule: pnpm tc, pnpm run check:code-quality:changed, and the touched suites (WSL deploy, transport, launch, daemon spawn, PTY env, preflight order) pass. CI runs the full suite.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • SSH: unchanged. Whether to offer this to SSH hosts is a separate decision.
  • Desktop: unchanged.
  • Wire compatibility: none needed. Old orcad never receives the variables; new orcad launched by an older desktop simply runs without them.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

…hreshold

V8 frees ~70 MB after compiling orcad's bundle, but glibc keeps it stranded
in the main arena. WSL launches of this build's own slot now set
GLIBC_TUNABLES=glibc.malloc.mmap_threshold=16384; orcad and its daemon
remove it at startup and orcad hands it only to the daemon, so shells,
agents and git keep the user's malloc. Older slots never get it.
@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Walkthrough

The change adds helpers to set and claim host malloc tunables, then passes claimed values into daemon environments. The transport launch callback now receives the launched slot’s full version. A wrapper adds tunables to the launch environment only when that version matches the current build version. Tests cover tunable claiming, daemon and PTY environment behavior, and version matching.

Priority: Low

Merge Risk: Moderate · up to 5eb68

On WSL, a user's existing GLIBC_TUNABLES can be silently replaced and then removed for orcad and the shells and agents it spawns. This affects only users who set that variable in the guest environment. Apply the threshold only when the variable is unset before merging.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 44.44% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surv… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly identifies the WSL performance change and the malloc mmap threshold used to return orcad startup memory.
Description check Passed The description follows the required template and provides clear ELI5, change, rationale, testing, compatibility, and checklist details. The Linked Issue section names internal work but does not provi…
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.
Full details: Description check

Explanation

The description follows the required template and provides clear ELI5, change, rationale, testing, compatibility, and checklist details. The Linked Issue section names internal work but does not provide an actual issue reference, and the manual-test and full-command checklist items remain unchecked.

Full details: Docstring Coverage

Explanation

Docstring coverage is 44.44% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 9 functions across 14 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2


Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8e3ee2d0-f8c0-4f3a-91db-d49668638413
Commits

Reviewing files that changed from the base of the PR and between 57aa892 and 5eb6892.

Files selected for processing: 14
  • src/main/daemon/daemon-entry.ts
  • src/main/daemon/daemon-launched-child-spawn.test.ts
  • src/main/daemon/daemon-launched-child-spawn.ts
  • src/main/daemon/pty-subprocess-env-inheritance.test.ts
  • src/main/managed-server/install/orcad-remote-runtime-control.ts
  • src/main/memory/host-malloc-tunables-claim.ts
  • src/main/memory/host-malloc-tunables.test.ts
  • src/main/memory/host-malloc-tunables.ts
  • src/main/orcad/main.ts
  • src/main/ssh/orcad-host-transport.ts
  • src/main/wsl/wsl-orcad-deploy.ts
  • src/main/wsl/wsl-orcad-host-transport.test.ts
  • src/main/wsl/wsl-orcad-own-build-launch-env.test.ts
  • src/main/wsl/wsl-orcad-own-build-launch-env.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.

Comment on lines +326 to +329
for (const [name] of hostMallocTunablesLaunchEnv()) {
delete process.env[name]
}
resetHostMallocTunablesForTests()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor · Maintainability & Code Quality · Quick win

Supported by static analysis

Script executed:

sed -n 300,345p src/main/daemon/pty-subprocess-env-inheritance.test.ts

Repository: stablyai/orca

Length of output: 1726


Restore pre-existing malloc environment values.

If the test process already has GLIBC_TUNABLES or the marker variable, the finally block deletes that value and leaks a changed environment to later tests. Save each original value before assigning the launch environment, then restore it explicitly in finally.

...conn,
orcadLaunchEnv: async (launch) => [
...((await launchEnv?.(launch)) ?? []),
...(launch.fullVersion === ownBuildVersion() ? hostMallocTunablesLaunchEnv() : [])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Major · Data Integrity & Integration · Heavy lift

Supported by static analysis

Script executed:

sed -n 1,200p src/main/managed-server/install/orcad-remote-launch.ts

Repository: stablyai/orca

Length of output: 8678


Script executed:

cat src/main/memory/host-malloc-tunables.ts src/main/wsl/wsl-orcad-own-build-launch-env.ts

Repository: stablyai/orca

Length of output: 2367


Script executed:

set -o pipefail
printf '%s\n' '--- launch-env bindings ---'
rg -n -F --glob '*.ts' -e 'withOwnBuildLaunchEnv' -e 'orcadLaunchEnv' -e 'claimHostMallocTunables' -e 'hostMallocTunablesDaemonEnv' src/main src/shared || test "$?" -eq 1
printf '%s\n' '--- WSL-related definitions and callers ---'
rg -n -F --glob '*.ts' -e 'wsl.exe' -e 'WSLENV' -e 'ORCA_WSL_HOST_ENV' -e 'login' -e 'profile' src/main/wsl src/main/managed-server src/main/ssh || test "$?" -eq 1
printf '%s\n' '--- relevant source blocks ---'
sed -n '1,220p' src/main/wsl/wsl-orcad-own-build-launch-env.ts
sed -n '1,180p' src/main/memory/host-malloc-tunables.ts
sed -n '1,180p' src/main/ssh/orcad-host-transport.ts
sed -n '1,260p' src/main/wsl/wsl-orcad-launch.ts 2>/dev/null || true

Repository: stablyai/orca

Length of output: 42289


Script executed:

set -o pipefail
printf '%s\n' '--- WSL deployment and transport ---'
sed -n '1,190p' src/main/wsl/wsl-orcad-deploy.ts
sed -n '1,180p' src/main/wsl/wsl-orcad-host-transport.ts
printf '%s\n' '--- runtime control launch binding ---'
sed -n '90,160p' src/main/managed-server/install/orcad-remote-runtime-control.ts
printf '%s\n' '--- WSL command runner ---'
sed -n '1,130p' src/main/wsl/wsl-command-loop.ts
rg -n -F --glob '*.ts' -e 'loginPath:' -e 'runWsl' -e 'wslCommand' -e 'execFile' src/main/wsl src/main/managed-server src/main/ssh || test "$?" -eq 1

Repository: stablyai/orca

Length of output: 37695


Preserve an inherited GLIBC_TUNABLES.

The WSL path starts sh -c directly, so this source does not establish that a login profile runs. However, any GLIBC_TUNABLES already present in the guest environment is still overwritten by the matching own-build launch assignment. claimHostMallocTunables then removes that value before child processes inherit the environment.

Apply the threshold only when the guest variable is unset, and add the claim marker only when this launch injected the value. Carrying the value through claimHostMallocTunables cannot recover it after the launch assignment overwrites it.

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.

1 participant