Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Performance Tuning

Most of the cost of a short Sandbox::execute() is not running Python. It is creating the WebAssembly instance the code runs on and tearing it down again: mapping the pre-initialized heap image, building a function reference for every entry in the runtime’s ~6k-entry function table, and, afterwards, resetting the memory slot. Running pass on a fresh instance takes about 1.3 ms on a fast workstation; the Python part of that is roughly 0.2 ms.

Eryx hides that cost in two ways, both on by default. This page explains what they do and the settings that tune them.

Every sandbox in a process runs on one shared wasmtime engine and one shared pool of warm instances, created the first time the process needs them. Their settings are read once, at that point, so set them in the environment of the process that embeds eryx (for pyeryx, the Python process), or from code before the first sandbox is created. Like rayon’s build_global, this configures the process-wide instance; it cannot be changed afterwards:

eryx::configure_shared_engine(
    eryx::SharedEngineConfig::default()
        .with_warm_instances(4)
        .with_pool_instances(256),
)?;
eryx.configure_shared_engine(warm_instances=4, pool_instances=256)

Every variable below has a matching SharedEngineConfig::with_* method and configure_shared_engine keyword argument (ERYX_POOL_INSTANCES is with_pool_instances / pool_instances, and so on). An explicit setting takes precedence over its variable; anything left unset still falls back to the environment. configure_shared_engine fails if a setting is invalid, if it was already called, or once a sandbox has been created. Per-sandbox engines with their own settings are not supported yet; the “shared” naming leaves room for an explicit engine handle later.

Warm instances

After every stateless execution, a background task instantiates a replacement store for the same runtime and parks it in a process-wide pool. The next execute() takes it instead of instantiating, and the used store is dropped on the background task too. Every execution still runs on an instance that has never run user code, so isolation is unchanged; only the timing moves.

The pool is keyed by runtime component rather than by Sandbox, so the common pattern of building a short-lived sandbox per request (for example SandboxFactory.create_sandbox() from Python) benefits from it: the first request in a process pays for instantiation, later ones do not. Executors that load their own copy of a component (from_file, from_binary, with_precompiled_file) each get a separate entry, so the pool keeps warm instances for at most four components and evicts the least recently used. Loading a new copy of the component for every request (for example SandboxFactory(cache=False)) defeats the pool: each execution warms an instance for a component that is never used again. Load the component once and reuse it.

VariableDefaultEffect
ERYX_WARM_INSTANCES1Instances to keep ready per runtime. 0 disables the pool.

One ready instance is enough for a caller that executes serially. If several tasks execute concurrently, raise it towards that concurrency; each ready instance costs about the resident size of the pre-initialized heap (tens of megabytes, mostly shared copy-on-write pages) plus wasmtime’s per-instance metadata.

The pool is only used on a multi-threaded Tokio runtime, where the background work actually runs in parallel. On a current-thread runtime it would only add to the next request’s latency, so it stays off. Executor::warm_instances_ready() reports how many instances are waiting for that executor’s runtime.

Instance allocation

Wasmtime can allocate each instance’s linear memory, tables, and async stacks on demand (a fresh mmap per instance) or from a pool of pre-reserved slots. Eryx uses the pooling allocator (except on Windows, see below): a slot that has just been released is reused by the next instance, so its pages are still mapped, and on Linux only the pages the previous instance dirtied are reset.

VariableDefaultEffect
ERYX_ALLOCATORpooling (on-demand on Windows)pooling or on-demand.
ERYX_POOL_INSTANCES1000Maximum sandbox instances alive at once. Every running execute(), every live session and every warm instance holds one, and a finished execution keeps its slot until the background task drops it; instantiation fails once the pool is full. Allow at least concurrency + 2 × ERYX_WARM_INSTANCES per component. A shell’s external commands don’t count: they run on an on-demand engine of their own.
ERYX_POOL_KEEP_RESIDENT_MB64How much of each linear memory to keep mapped between uses.
ERYX_POOL_PAGEMAP_SCAN11: reset only dirty pages (Linux 6.7+, via PAGEMAP_SCAN). 0: memcpy the whole keep-resident budget instead, which trades a larger copy for fewer page faults on the next execution.
ERYX_POOL_RELEASE_INTERVAL_SECS30Every this many seconds, return the memory kept resident for unused slots to the OS. 0 keeps it resident indefinitely.

Keeping memory resident is what makes slot reuse cheap, but after a burst of concurrent executions every slot the burst used stays resident. The periodic release returns that memory. Live instances, including warm ones, are not affected; a released slot that is used again pays for its page faults once, so under steady traffic the cost is one slower instantiation per slot per interval.

On Windows the default is on-demand: Windows has no overcommit, and wasmtime commits the whole table pool when the engine is created, which at the default limits is gigabytes of commit charge per process.

The pool reserves virtual address space for every slot up front — several terabytes with the defaults, which is normal for wasmtime deployments but can be refused by a host with a low ulimit -v. If the pool cannot be created, eryx logs a warning and falls back to on-demand allocation; execution behaves identically either way.

If a process holds more than ERYX_POOL_INSTANCES sessions open at once, raise the limit or switch to on-demand. Note that the choice of allocator does not affect precompiled .cwasm artifacts; only compilation settings do.

Measuring

crates/eryx/examples/profile_stateless.rs times the stateless path and is a convenient target for perf or samply:

cargo build --example profile_stateless --features embedded --release
# 2000 executions of `pass`, with a 2 ms gap between them so the background
# replenishment gets the same chance it has between real requests
./target/release/examples/profile_stateless 2000 pass 2000
ERYX_WARM_INSTANCES=0 ./target/release/examples/profile_stateless 2000 pass 2000

The criterion benchmarks (cargo bench --package eryx --features embedded) cover the same paths with callbacks registered.