Performance
SWS optimizes for low per-request syscalls and allocations on the static-file path. Changes are justified by a benchmark or profile, not intuition.
When to load: editing the hot path (handler.rs, static_files/, fs/, compression*.rs, control_headers.rs, security_headers.rs, custom_headers.rs, logger/access/, logger/trace_context/body.rs, exts/), adding a bench, reading a CodSpeed result, or adding a dependency.
Workflow
- Reproduce and measure with a bench in
benches/or a load test against a release build - Profile to find where time goes
- Change one thing, re-measure, keep it only if the gain is measurable
- Add or update a bench so CodSpeed catches regressions
- Never trade correctness or security for speed. A cached security decision needs a bounded staleness window (containment cache)
What the Hot Path Already Does
Don't undo these:
- Startup pre-computation: root canonicalized once; glob/regex rules, Aho-Corasick replacers, and the access log format compiled in
settings/; staticHeaderValues viafrom_static - Containment cache:
canonicalize()was ~18% of CPU on the static path. Athread_local!cache of proven-safe paths with wholesale TTL eviction removes the syscall in steady state (static_files/security.rs) - Single open + fstat:
fs::meta::try_file_openreads metadata from the descriptor - Cheap checks first: method check and pre-processing return early; symlink walking runs only with
--follow-symlinksoff - Zero-alloc header lookups:
control_headerslowercases the extension into a 16-byte stack buffer andbinary_searches sorted arrays - Streaming: bodies streamed with
fs::stream::optimal_buf_size()(device block size, capped at file length); only small generated bodies are buffered - Pre-compressed first:
.br/.gz/.zstcost no CPU; dynamic compression skips encoded, incompressible, partial, bodiless, and <200-byte responses - Memory cache (
[advanced.memory-cache]):mini-moka,CompactStringkeys with the content coding, so a hit serves the right variant without disk access; files up to 8 MiB by default (max 32 MiB),ttl/tti - Access log: one line per request, written when the body is dropped; values escaped into a bounded buffer
- Allocator:
mimallocon 64-bit musl targets (src/bin/server.rs)
Hot-Path Rules
- No per-request
canonicalize, regex compilation, glob building, or config parsing - No
format!/Stringfor header values that can befrom_staticor written into a reused buffer - Borrow (
&str,&Path,Cow) over clone; justify eachclone()on the request path ok_or_else/unwrap_or_elsewhen the fallback allocates#[inline]on small per-request functions when a bench shows it helps;#[cold]on error paths- Per-request work proportional to the request, not the config (indexed lookups over iterating all rules)
- Don't add a syscall to make a cache more precise without measuring
Threads and Runtime
- Workers = CPUs ×
--threads-multiplier(default1;0and1both mean CPU count) --max-blocking-threads(default512) bounds tokio's blocking pool used by file I/O- Raising the multiplier helps when dynamic compression and many clients interleave; for pure static serving the CPU count is usually optimal. Measure first
Release Build
[profile.release]: lto = "fat", codegen-units = 1, panic = "abort", opt-level = 3, strip = true. Don't propose these as improvements.
Profiling uses the release-profiling profile (debug info, not stripped):
cargo build --profile release-profiling --features all
samply record target/release-profiling/static-web-server -p 8080 -d docker/public/
RUSTFLAGS="-C target-cpu=native" is non-portable; local experiments only.
Benchmarks
Micro-benchmarks (CodSpeed)
benches/ is a standalone crate (codspeed-criterion-compat). Suites: control_headers, static_files, response_headers, http_ext, security_sanitize_path, security_basic_auth, security_redirects. .github/workflows/codspeed.yml runs them per PR in CPU simulation and reports deltas.
cd benches
cargo bench # local Criterion timings
cargo bench --bench control_headers
Benches call public functions (control_headers::append_headers, static_files::handle). Expose a thin pub or #[doc(hidden)] pub entry point for a private hot function rather than benchmarking through the server. Register new suites as [[bench]] with harness = false in benches/Cargo.toml.
Load tests
- Maintainers trigger
.github/workflows/perfcheck.ymlby commenting/try+ perfcheckon a PR (vegeta, 4 workers, 100 connections, 10 s, several rates) - Locally:
oha,wrk,bombardier, orvegetaagainst a release build. Report req/s and p50/p95/p99 at fixed concurrency, plus RSS before and under load
Tools
CPU: samply, perf + flamegraph. Allocations: dhat-rs, Valgrind DHAT. Syscalls: strace -c -f. Type layout: RUSTFLAGS=-Zprint-type-sizes cargo +nightly build --release. Runtime: --metrics (Prometheus), experimental feature for tokio runtime metrics.
Binary Size
About 4MB. Before adding a dependency, build --release --features all before and after and compare. Growth above 100KB needs a feature flag or must replace existing code. default-features = false, enable only what's needed.
Review Checklist
- [ ] A bench or profile shows the problem and the improvement
- [ ] A bench covers the changed hot-path function
- [ ] No new per-request syscalls, canonicalization, or regex/glob compilation
- [ ] No new steady-state allocations without justification
- [ ] Caches have bounded size and staleness and don't weaken security checks
- [ ] Binary size impact checked for new dependencies