# Where ScalWS trails nginx 1.31: a profile on bare metal (2026-11-13)

URL: https://scalws.com/docs/benchmarks/2026-11-13-xeon-profile/
Source: `docs/benchmarks/2026-11-13-xeon-profile.md` in the ScalWS repository

Diagnosis only: no product code changed. The question is why ScalWS is below nginx 1.31 in
some scenarios of the 1.0.0 report (`2026-11-12-xeon-1.0.0.md`), and which changes would
recover how much, towards the goal of at least 1.05x nginx 1.31 in every scenario at one
and at six CPUs.

## Setup

Host, pinning and load as in the 1.0.0 report: 2x Xeon E5-2650 v2 (Ivy Bridge, no AVX2),
Ubuntu 26.04, kernel 7.0.0-38, `performance` governor, turbo off. Server CPUs on socket 0,
applications (bench upstream, PHP-FPM, Node.js, uvicorn with 2 workers) on 6, 7, 22, 23,
`oha` 1.16.0 on 8-15 and 24-31, 64 connections, keep-alive.

Layouts:

- **6 CPUs**: `scalws-controller` with 3 instances x 2 CPUs on CPUs 0-5, sandbox on (the
  package as it runs); nginx 1.31.6 with `worker_processes 6` on 0-5.
- **1 CPU** (the 1.0.0 report has none; defined here): the controller with 1 instance of 1
  CPU on CPU 0, sandbox on; nginx with `worker_processes 1` on CPU 0. Same load CPUs.

Servers under test:

- `nginx`: /opt/nginx-1.31.6, `bench/configs/nginx.conf`.
- `1.0.0 own`: the installed `scalws_1.0.0-1` package, `http1_server: experimental`,
  `upstream_client: experimental`.
- `HEAD own` / `HEAD hyper`: v1.1 HEAD (f97bc15) with the own HTTP/1 server and client,
  and with the defaults (hyper). Bound over `/usr/bin/scalwsd` (`mount --bind`) so that the
  packaged controller, unit, user and sandbox stay as installed.

The HEAD binary is built as the package builds it: `tools/pgo-build.sh` with
`PGO_FEATURES=scalwsd/http3` (instrumented build, the training load once with the default
settings and once with the own HTTP/1 paths, then `-Cprofile-use`), release profile (thin
LTO, `codegen-units = 1`, `debug = "line-tables-only"`), no BOLT (as
`scripts/release-build.sh`), rustc 1.99.0 on the host instead of the AlmaLinux 8
container, not stripped (symbols for `perf`). Experiment variants were rebuilt from the
same profile data; a rebuild of the unchanged source (`base2`) measured within 1.5 % of the
original, which is the noise floor of the experiment table below.

Method: a driver of its own (`xeon:[private host path]`, `measure.sh`) that starts each server
the way `bench/run.sh SERVERS=scalws-service` does (same generated `scalws.yaml`, empty
controller state, applications as external processes), warms up, then runs `oha` and
`perf stat` on the server CPUs during the measured window. Main table: 3 rounds of 8 s
warm-up and 20 s measurement in rotating order, medians. Per-request CPU figures come
from `perf stat -C <server CPUs>` divided by the requests of the window (all 6 or the one
server CPU are saturated in respond, proxy and static-small; in node and python the
application is the bottleneck). Syscalls per request: syscall tracepoints counted with
`perf stat`. Profiles: `perf record -g` at 2999 Hz (flat) and `--call-graph dwarf` at 397 Hz;
TCP segments per request: `nstat`. Zero non-2xx answers in every run.

## Reproduced numbers

Requests per second, medians of 3 rounds (ratio against nginx 1.31 in brackets):

| scenario | layout | nginx 1.31 | 1.0.0 own | HEAD own | HEAD hyper (default) |
|---|---|---:|---:|---:|---:|
| static-small | 6 CPUs | 125 552 | 130 816 (1.042) | 128 744 (1.025) | 121 992 (0.972) |
| respond | 6 CPUs | 242 094 | 233 766 (0.966) | 231 234 (0.955) | 212 476 (0.878) |
| proxy | 6 CPUs | 106 613 | 105 032 (0.985) | 104 626 (0.981) | 90 509 (0.849) |
| node | 6 CPUs | 8 287 | 8 222 (0.992) | 8 107 (0.978) | 7 900 (0.953) |
| python | 6 CPUs | 6 402 | 6 399 (1.000) | 6 379 (0.996) | 6 440 (1.006) |
| static-small | 1 CPU | 21 448 | 22 351 (1.042) | 22 458 (1.047) | 21 036 (0.981) |
| respond | 1 CPU | 41 139 | 39 185 (0.953) | 39 502 (0.960) | 35 756 (0.869) |
| proxy | 1 CPU | 18 765 | 17 908 (0.954) | 18 406 (0.981) | 14 956 (0.797) |
| node | 1 CPU | 8 423 | 8 247 (0.979) | 8 316 (0.987) | 7 977 (0.947) |
| python | 1 CPU | 6 467 | 6 434 (0.995) | 6 429 (0.994) | 6 447 (0.997) |

- The gaps of the 1.0.0 report reproduce for respond, proxy and (with hyper) static-small;
  v1.1 HEAD and the 1.0.0 package are equal within noise (HEAD changed the PHP paths).
- One CPU behaves like six: the gaps are per-request costs, not contention between event
  loops or instances.
- The node and python gaps of the report do not reproduce as gaps: see below, those two
  scenarios measure the application, not the proxy.

## Where the cycles go (all scenarios)

1. **The kernel does 70 to 77 % of the server-side work, for both servers.** On loopback the
   server CPU also runs the receive path of the peer for every segment it sends, so a
   request costs a full TCP send, receive and netfilter pass in each direction. Netfilter
   alone (`nf_hook_slow`, conntrack, `nft_do_chain`, the host's firewall rules on `lo`) is
   12.5 % of the server CPUs inclusive. Example, respond at 6 CPUs: nginx 63.3k cycles per
   request of which about 47k in the kernel; ScalWS own 66.4k, about 49.5k in the kernel.
2. **System calls and packets are not the difference.** Per request (6 CPUs):

   | scenario | nginx | ScalWS own | ScalWS hyper |
   |---|---|---|---|
   | respond | recvfrom 1, writev 1, epoll_wait 0.18 | recvfrom 1, writev 1, epoll_wait 0.16 | same as own |
   | static-small | 8 calls (open, sendfile, close, 2 setsockopt cork) | 6 calls (statx, close, no cork) | 6 |
   | proxy | 5.4 calls (2 writev, 2 recvfrom, 1 getsockopt) | 4.3 (writev, sendto, 2 recvfrom) | 4.3 |

   TCP segments per request are identical (`nstat`): 2.00 in respond and static-small,
   4.00 in proxy, no pure ACKs, no delayed ACKs. The own client does not issue the
   extra `read` its idle-connection check (`Lease::alive`) could cost: readiness is
   already clear, so tokio returns without a syscall.
3. **The ScalWS user space executes more instructions and touches more memory per request,
   and the kernel then runs slower.** Respond at 1 CPU, per request:

   | counter | nginx | own | hyper |
   |---|---:|---:|---:|
   | user instructions | 9 433 | 12 834 | 14 982 |
   | kernel instructions | 24 584 | 24 782 | 25 003 |
   | kernel cycles | 48 816 | 50 694 | 52 917 |
   | L1d replacements | 867 | 1 198 | 1 381 |
   | L2 lines in | 1 994 | 2 496 | 2 895 |

   The kernel executes the same instructions (+0.8 %) but needs 1.9k (own) and 4.1k
   (hyper) more cycles: the larger user working set evicts the network stack's lines. In
   the symbol diff the kernel excess is spread over the whole TCP path, not a few
   functions. Every cache line ScalWS stops touching therefore pays twice.
4. Things that measured as small or nothing: the seccomp filter (`__seccomp_filter`,
   `populate_seccomp_data`, about 0.5k cycles per request in the profile, 0.7 %), the extra
   `fdget`/`fput` atomics of a multithreaded process (about 0.25k), shared atomics between
   the two loops of an instance (1 CPU shows the same ratios as 6).

## Per scenario

### respond (0.955 at 6 CPUs, 0.960 at 1 CPU)

Identical syscalls and packets; ScalWS own needs 3.4k more instructions and 3 to 4k more
cycles per request than nginx (66.4k against 63.3k). Flat profile, user part, cycles per
request: `conn::drive` (the inlined own HTTP/1 connection loop) 4.1k, `pipeline::handle`
2.3k, `scalws_h1::request::parse` 1.9k (nginx's request and header parsers together:
1.75k), `pipeline::finish` 1.35k, `memmove` 1.27k (nginx: 0.13k), `encode_head` 0.6k,
`HeaderName::from_bytes` 0.38k, mimalloc 0.7k.

Experiment `fastrespond` (answers `/respond` at the top of `pipeline::handle`, before the
`Outcome`, routing, checks and `finish`; the HTTP/1 layer, request id and runtime stay):
**+12.4 % at 6 CPUs (1.087x nginx) and +11.5 % at 1 CPU (1.112x)**, 58.5k cycles and 33.8k
instructions per request, i.e. nginx's instruction count at 8 % fewer cycles. The own HTTP/1
server and the runtime are already faster than nginx; the request pipeline costs about 7k
cycles (11 %) on a fixed answer. Stage timings of a `perf-instrumentation` build (1 CPU,
includes about 70 ns of probe overhead per stage): `pipeline.host_route` 0.91 µs,
`pipeline.finish` 0.84 µs, `pipeline.context` 0.76 µs (route selection and policy, up to
`fixed()`), `pipeline.checks` 0.43 µs, `pipeline.htaccess` 0.08 µs.

Experiments that did not move respond (3 rounds each, noise 1.5 %): `noacct` (finish
returns after the headers: no metrics, host metrics, telemetry, diagnostics, no
`CountingBody`) 0.963 vs 0.959 to 0.974; `sandbox: off` 0.960; `metrics.hosts: false`
0.964. Accounting is at most 2 % of respond.

### proxy (0.981 at both layouts)

The own client makes fewer syscalls than nginx (4.3 against 5.4 per request) and the kernel
side is 0.2k cycles cheaper; the user side is 3 to 5k cycles more expensive (148.4k against
144.6k total at 6 CPUs). Largest user items, cycles per request: `conn::drive` 4.9k,
`pipeline::handle` 4.6k, `memmove` 3.8k (nginx 0.4k), `Target::send` 3.2k,
`own_client::Lease::poll_head` 2.2k, `pipeline::finish` 2.1k, `scalws_h1::request::parse`
2.1k, `ProxyHandler::forward` and `prepare_forward` 1.3k each, the boxed
`cache_stage::serve` future 1.3k, `ResponseCache::is_pass` 0.8k and
`ResponseCache::bypass_reason` 0.5k. Stage timings (1 CPU): `proxy.prepare` 1.5 µs,
`proxy.checkout` 0.87 µs, and the pipeline stages 4.1 µs together.

- The bench site has `cache: {enabled: true}`, so every proxied request runs the cache
  stage, while nginx's `/api/` location has no `proxy_cache`. Experiment `nocache` (the
  same configuration without the cache): **+1.8 % at 6 CPUs (1.008x), +3.5 % at 1 CPU
  (1.038x)**, 4.4k cycles and 2.2k instructions less per request.
- `memmove` is 2.6 % of the cycles. Its callers could not be resolved: DWARF unwinding
  fails through these copies (the stack copy of 8 KiB is not enough), which itself points
  at large stack frames. The likely source is by-value moves of large `async` state
  machines and `Request`/`Response` values between the pipeline, the cache stage and the
  proxy (a Rust move copies `size_of` bytes, including a future's unused states).

### static-small (1.025 at 6 CPUs, 1.047 at 1 CPU)

Already ahead: ScalWS needs two syscalls fewer than nginx (no cork `setsockopt` pair, no
`sendfile` for 13 bytes), its kernel side is 4.5k cycles cheaper, its user side 0.7k more
expensive. The same pipeline cost as in respond (about 4 µs of stages) applies; the static
handler itself is 15 µs of wall time per request at 1 CPU, mostly the file system calls.
Slimming the pipeline is what moves this scenario past 1.05x at 6 CPUs.

### node (0.978 / 0.987) and python (0.996 / 0.994)

Both measure the application, not the proxy:

- The single Node.js process needs about 250k cycles per request on its CPU and is
  saturated; the uvicorn workers need 805k to 918k cycles per request. Server CPUs are
  mostly idle in these scenarios.
- What reaches the application is the same from all three servers (captured with
  `strace`): one request of 193 to 205 bytes with the same forwarding headers (nginx's
  longer `X-Request-Id`), one write per request; 64 keep-alive upstream connections from
  each server. uvicorn answers in three writes (head, chunk, last chunk); each server reads
  them with about three reads (recvfrom 2.99 own, 2.98 hyper; nginx 2 recvfrom and 1.8
  readv).
- Fresh applications (restarted for every measurement, 4 repetitions): node 10 394 to
  10 787 (nginx), 10 477 to 10 795 (own), 10 251 to 10 847 (hyper): equal. In the long
  runs, where the applications live through all servers and scenarios, Node.js is 20 to
  25 % slower (8.1k to 8.4k) and whichever server comes later in the rotation pays for it.
- Python varies from 5 690 to 6 476 between repetitions of the same server, and in a
  pinning experiment some runs of every server fell to about 3 600, a bimodal mode that
  depends on where the scheduler places the two uvicorn workers (two workers on one CPU or
  on hyperthread siblings of one core). Upstream cycles per request track the throughput
  exactly (rps x cycles per request = the two worker CPUs).

**Why python was 0.90 with the own client and 0.97 with hyper in the 1.0.0 report**: that
difference is this placement noise, not the client. Here own and hyper are 0.996 and 1.006
(6 CPUs), 0.994 and 0.997 (1 CPU), with the same syscalls, segments, upstream bytes and
connection spread (24 to 43 connections per worker for every server). The 1.0.0 "own"
column was also measured in a separate session after the others, with applications that
had already served the earlier runs.

A proxy cannot make the application faster: in this topology 1.05x nginx on node and python
is out of reach for any change to ScalWS. Parity is the ceiling, and the measurement
needs fixing (see item 6 below) before it can show even that reliably.

### hyper (the default) against the own paths

The default paths cost respond 8 %, proxy 13 % (6 CPUs) and 19 % (1 CPU), static-small 5 %.
Per request in proxy at 6 CPUs hyper needs 23k cycles more than own (171.6k against
148.4k): 17.8k in user space (the client connection task and dispatcher 5.0k, server
`parse`/`encode` 2.7k, `memmove` +1.8k, the header-read timer: `TokioSleep` poll, drop and
`sleep_until`, about 1k per request) and 6.8k in the kernel with the same syscalls and
segments (cache pollution, as above). In respond hyper needs 4.7k user and 1.9k kernel
cycles more than own.

## Ranked changes

Gains are against nginx 1.31 throughput. "Measured" means an A/B on this host; "estimated"
is derived from cycles per request in the profiles.

| # | where | change | gain | risk |
|---|---|---|---|---|
| 1 | `scalws-config` defaults of `server.http1_server` and `server.upstream_client` (ADR-0045, ADR-0046, roadmap P10) | make the own HTTP/1 server and client the defaults | measured: respond +8.8 % (6 CPUs) / +10.5 % (1 CPU), proxy +15.6 % / +23.1 %, static-small +5.5 % / +6.7 % (node +2.6 % / +4.2 %, within that scenario's noise) | medium: the own paths carry all traffic; needs the conformance, fuzz and soak gates before the switch |
| 2 | `crates/scalws-server/src/pipeline.rs`: `handle`, `route_and_handle`, `finish` | a short path for requests that need no context: resolve the route of fixed answers and static files once per host/route lookup; do not allocate per request what is only read later (`Arc::from(req.uri().path())`, the request id as `Arc<str>` plus `HeaderValue::from_str` copy, `Outcome` clones); skip `normalize_path`, `denied`, `rate_limits.admit`, strict SNI and the redirect check by precomputed per-app flags when the app has none; write `server` and `x-request-id` in the encoder instead of `HeaderMap` inserts | measured upper bound (whole pipeline skipped): respond +12.4 % / +11.5 %; estimated for a realistic slimming (half to two thirds of the 7k cycles): respond +6 to 9 %, static-small +3 to 5 %, proxy +3 to 4 % | low to medium: many small changes in the hottest function; existing pipeline tests cover the semantics |
| 3 | `pipeline.rs` `cacheable` closure, `cache_stage::serve`, `scalws_cache::ResponseCache::{is_pass, bypass_reason}` | decide the request-side bypass before building and boxing the cache future, and keep the lookup path for requests that can hit | measured with the cache off: proxy +1.8 % (6 CPUs) / +3.5 % (1 CPU); a cheap bypass recovers most of it | low |
| 4 | futures and values moved by value through the pipeline, cache stage and proxy (`handle`, `route_and_handle`, `cache_stage::serve`, `Target::send`, `ProxyHandler::forward`) | print `size_of_val` of these futures, box rarely taken large branches, stop moving `Request`/`Response` through several layers by value | estimated from `memmove`: proxy +1.5 to 2.5 % (3.8k cycles per request today), respond +1 % (1.3k); more through less cache pollution in the kernel | low |
| 5 | `crates/scalws-proxy/src/own_client.rs` (`OwnPool::checkout`, `take_idle`, `Lease::alive`) and `scalws_proxy::prepare_forward` | cheaper checkout (no `reserve(64)` and telemetry clock reads per checkout, shard lock only when needed), prepare forwarding headers without repeated `HeaderMap` scans | estimated: proxy +1 to 2 % (`proxy.prepare` 1.5 µs and `proxy.checkout` 0.87 µs per request today) | low |
| 6 | `bench/run.sh`, `bench/multi.sh` (not ScalWS) | start the applications fresh for every server and run, pin each uvicorn worker and Node.js to its own physical core, report the application's CPU per request next to the ratio | makes node and python comparable; no throughput gain for ScalWS possible there (application-bound) | none |
| 7 | sandbox, `metrics.hosts`, accounting in `finish` | none | measured: no change beyond the 1.5 % noise | n/a |

Expected position after the changes (6 and 1 CPU, own paths as defaults):

- static-small: 1.025 / 1.047 today, 1.06 to 1.10 with item 2.
- respond: 0.955 / 0.960 today, about 1.02 to 1.06 with items 2 and 4; 1.05 needs most of
  the pipeline cost removed for fixed answers (the measured ceiling is 1.09 / 1.11).
- proxy: 0.981 / 0.981 today, about 1.05 to 1.10 with items 2 to 5 together.
- node, python: parity is the ceiling (application-bound); 1.05x is not reachable by a
  change to ScalWS.

## Raw data and scripts

`xeon:[private host path]` holds the driver and analysis scripts (`lib.sh`, `measure.sh`,
`prof.sh`, `ctr.sh`, `seg.sh`, `pyconn.sh`, `pypin.sh`, `reqcap.sh`, `stage2.sh`,
`variants.py`, `buildvar.sh`, `agg.py`, `sysagg.py`, `symdiff.py`) and the results:
`res/m` (main table), `res/e` (experiments), `res/p` (syscall counts and profiles),
`res/py`, `res/pypin`, `res/reqcap.log`, `res/stage.log`. Binaries:
`/var/tmp/prof-target/pgo/scalwsd` (HEAD) and `/var/tmp/prof-target/var/` (experiment
variants; the patches were applied to the scratch checkout `[private host path]` and reverted).
The `perf stat` windows of `res/e` are 9 s (`WIN=9 agg.py`), those of `res/m` 14 s.
