10x faster python test iteration via fork(2)
Benchling cut Python test iteration from 10 seconds to about 1 second by preloading slow-to-import dependencies and SQLAlchemy models in a parent process, then fork()ing a child that imports the changed app and test code, in the style of Rails' zeus. importlib.reload() fails because reverse dependencies keep stale bindings. Modules are split into tiers by dependency position and edit frequency, so a file change discards as little import work as possible. Calling gc.freeze() before fork avoids copy-on-write page copies caused by refcount and GC header writes, saving 160 MiB of unique set size, while calling gc.collect() first is counterproductive because freed holes get refilled in copied pages.
Related posts
- How posix_spawn & close_range “fixed” cold starts
Neon mediumAdept
Neon's Pageserver regressed cold-start and first-query-after-idle latency to seconds after it began shutting down idle per-tenant walredo sidecar processes. Spawning them was slow because a pre_exec hook (used to mark inherited file descriptors close-on-exec) forced Rust's std::process::Command to fall back from posix_spawn to fork+exec. The real cost turned out to be the close_fds crate's fallback: CLOSE_RANGE_CLOEXEC isn't supported on Linux 5.10, so it issued one fcntl per open FD, roughly 2s across ~200k descriptors. Moving a plain close_range call into the walredo process itself cut basebackup latency from ~2s mean and ~10s p99 to the low hundreds of milliseconds.
- Introducing Immortal Objects for Python
Meta Engineering mediumAdeptInstagram's pre-fork Django servers were losing shared memory to copy-on-write because CPython mutates reference counts and GC headers even on effectively read-only objects. PEP-683 Immortal Objects marks a special refcount value so the runtime never modifies an object's refcount or GC header, which keeps those pages shared and lets objects be shared across threads without the GIL. Upstreaming required preserving backward compatibility across platforms, compilers, word sizes and endianness, and careful register allocation held the refcount-path check to about a 2% regression. The change is a building block for PEP-684 (per-interpreter GIL) and PEP-703 (optional GIL).
- Lazy is the new fast: How Lazy Imports and Cinder accelerate machine learning at Meta
Meta Engineering easyNoviceMeta rolled out the Cinder Python runtime with Lazy Imports, which transparently defers all imports until first use, across ML frameworks and Jupyter kernels. The change cut time to first batch (TTFB) by up to 40 percent and Jupyter kernel startup by 20 percent. Unlike importlib's LazyLoader, it needs no hand-picked lazy imports and removes the need for TYPE_CHECKING guards. Libraries such as PyTorch, NumPy, SciPy and Numba broke because they depend on import side effects and ordering, so Meta enables lazy imports only during startup and preparation and turns them off before the first batch.
- Meta contributes new features to Python 3.12
Meta Engineering mediumAdeptMeta upstreamed several features into CPython 3.12, many derived from its Cinder runtime: immortal objects (PEP 683) that skip reference counting to cut copy-on-write memory in forked Instagram workers, the @typing.override decorator (PEP 698), and inlined comprehensions (PEP 709) running up to 2x faster. Other changes include opt-in eager asyncio tasks that resolve immediately-available results without event-loop scheduling, a LOAD_SUPER_ATTR opcode that avoids allocating super objects, and new async_tree and comprehension benchmarks in pyperformance. New runtime hooks, including vectorcall entrypoints, dict/type/function/code watchers, and a thread-safe perf-map API, let third-party JITs like CinderX plug into CPython.
- Zero-Downtime Patching Part 1: Prewarming
Neon mediumAdept
Neon and Databricks Lakebase avoid the post-restart performance hit of cold Postgres caches by prewarming a replacement compute before planned maintenance. Shortly before the scheduled restart, the control plane starts a hidden new compute, sends it the list of pages in the primary's cache, and loads them from shared storage. It then tails WAL from Safekeepers, applying only records that touch cached pages, and finally promotes it with pg_promote without restarting the server. In 10 GB pgbench tests, throughput recovers almost immediately with prewarming, compared with a roughly 70% TPS drop and slow recovery on a cold restart, with the largest gain on read-only workloads.