Repository navigation
Description
Measured against origin/main at 747bc3a817a690f00906c69103458e297bd19a2d.
LocalDrain stores per-backend metrics in a Vec and finds the entry by scanning it:
backends: Vec<LocalBackendMetrics>,let backend = self
.backends
.iter_mut()
.find(|backend| backend.backend_id == backend_id);That scan runs once per labelled metric, and record_backend_metrics! emits five per request —
backend::BYTES_IN, BYTES_OUT, RESPONSE_TIME, CONNECTION_TIME and REQUESTS — from
SessionMetrics::register_end_of_session. #426's backend_header_time makes it six. So each request
ends with six linear scans over every backend the worker has ever seen, each comparing a String
against a &str.
What is already right, and must stay right
The path is allocation-free in steady state, and that is not an accident:
receive_metricandreceive_backend_metrictake their labels as&str, never owned.backend_id.to_owned()runs only on the insert branch, i.e. a backend's first sighting.MetricsMap::receive_metriclikewise allocates the metric-name key only on a fresh insert.
Any fix must preserve that. Replacing the Vec with a HashMap<String, _> keyed by backend id
would remove the O(n) but reintroduces a key allocation — or a borrow-checker fight — on every
lookup unless the key type is chosen carefully. A fix that trades six string comparisons for one
allocation per request is not an improvement: sōzu stays maximally zero-copy on the hot and data
path, and an allocation is the more expensive side of that trade.
Why this is worth measuring before choosing a shape
The cost is O(6n) string comparisons per request where n is the number of distinct backends in
the worker's drain. For a handful of backends it is noise. For a cluster with hundreds it is not,
and the drain accumulates backends rather than pruning them per request, so n grows with the
worker's history rather than with its concurrency.
Nobody has measured which regime production is in. That measurement decides whether this is worth
changing at all, and it should come before any shape is proposed:
- what
nreaches on a realistic worker, and whether anything prunes it; - whether an existing Criterion bench in
lib/benches/captures this path, or a new one is needed; - whether the cost is visible against the rest of
register_end_of_session.
One shape worth considering, stated as a candidate rather than a plan
The caller already knows which backend it is: SessionMetrics holds backend_id. If the drain
handed out a stable index or slot on first sighting and the session kept it, the six lookups become
six direct accesses with no comparison and no allocation. That moves the cost to the first metric of
a session and keeps the steady state free. It needs an answer for what happens when the drain
removes a backend under a live session — which is exactly the kind of question that should be
answered by reading remove_backend rather than guessed at here.
Found while gating #1546 (backend_header_time), which adds the sixth scan; not in that
changeset's scope.
Activity