Codex made process spawning 800x faster by ditching fork+exec for posix_spawn

charliermarsh · x · 2026-11-25

Charlie Marsh (Ruff/Codex) details how Codex optimized process creation, which runs millions of times per week for shells, hooks, and MCP servers. Even with copy-on-write, fork still copies page tables scaling with parent memory. Switching to posixspawn cut per-launch time from 200ms to 0.25ms on macOS with 20 GB resident memory — 800x faster. Key gotchas: Rust's stdlib falls back to fork when preexec callbacks are used, and macOS historically forces fork for relative executables with currentdir; the team wrote a custom Command abstraction calling posixspawn directly, uses POSIXSPAWNSETSID instead of a setsid callback, and on Linux spawns itself first to do unsupported setup before exec'ing the target.

Related event: Codex speeds up process spawning 800x by replacing fork with posix_spawn(14 posts)→

Original post →

More from coding & agent

coding & agent channel →