Skip to content

perf(data_plane): Remote Write decode 652 → 212 ns/sample - #861

Merged
zzylol merged 5 commits into
feat/tiered-view-storagefrom
perf/remote-write-decode
Nov 24, 2026
Merged

zzylol merged 5 commits into
feat/tiered-view-storagefrom
perf/remote-write-decode

Conversation

@zzylol

@zzylol zzylol commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Remote Write decode was the data plane's biggest CPU cost on the demo48 live workload (34,220 series at 10 Hz, 342k samples/s, 4 feeder shards): 55.6% of ASAP's phase CPU. This PR cuts it from 652 to 212 ns per sample in the server, and from 514 to 147 ns per sample in a single-thread benchmark on captured demo48 requests. Results do not change: the same samples, series and errors, checked against the old decoder.

Results

End to end. One data plane on 20 CPUs of a 40-core x86 server (2.2 GHz). It is fed by the four demo48 feeder shards at the demo rate and serves the demo's four query users. The table covers 20 minutes after a 2-minute warm-up. Old = 24342eab, new = this PR's changes on 24342eab. The decode path is the same on the current branch head.

old new
remote_write_decode CPU 0.223 cores (48.0% of phases) 0.073 cores (23.4%)
decode ns/sample 652 212
precompute 0.154 0.151
append 0.048 0.047
query 0.031 0.032
data plane process total 0.557 cores 0.393 cores (−29%)
samples/s accepted + unrouted 286,195 + 55,999 286,195 + 55,999
failed writes, answered queries 0, 7,023 0, 7,034

Saturation (data plane limited to 4 CPUs, feeder rate raised per series, 3 minutes measured per step):

per-series rate (samples/s offered) old new
30/s (1.03 M) sustained, decode 523 ns/sample, 1.01 cores sustained, decode 156 ns/sample, 0.63 cores
45/s (1.47 M) 503s (queue full) 503s (queue full)

The ceiling did not move. At it, the process uses under 1 of its 4 CPUs in both builds, so the limit is not decode CPU. It is a serial stage behind the ingest queue, and decode no longer competes with it for CPU. That bottleneck is outside this PR.

Benchmark (rw_corpus_bench on 1,080 captured demo48 bodies: 20.5 M samples, 2.37 GB decompressed, about 115 bytes per sample, one sample per TimeSeries; one thread with a warm cache):

ns/sample old new
snappy 111 40
protobuf walk ≈335 ≈71
series lookup ≈68 ≈36
decode total 514 147
hand-off to 4 shards 30 9.5
8 threads, decode + hand-off CPU 591 163

The snappy figures are measured on their own. The walk and lookup figures split the rest of the decode by their perf shares.

What changed, and what each step gave (decode ns/sample, benchmark)

  1. 514 → 210. Reuse a per-thread decompression buffer. A fresh zeroed allocation per request cost about 70 of the 111 ns in snappy (zeroing plus page faults). Inline the protobuf reader, with a fast path for one-byte varints. Before this, Reader::field/varint/take were out-of-line calls and took half the decode time.
  2. 210 → 188. Skip a series' leading labels by their one-byte tag and short length. Walk only the fields outside the labels when collecting samples. Read the timestamp varint of the sender's sample layout inline.
  3. 188 → 165, hand-off 28 → 12.5. Series are never forgotten, so the interner now returns &'static Series instead of Arc<Series>. RawSample.labels, Chunk.series, intern* and by_identity change type. No reference count is written per sample anywhere (decode, routing, chunk building, drop). Before, that count was written from the decode thread and from the ingest threads.
  4. 165 → 151. The per-thread series cache remembers which series came next last time. A lookup first compares the key with that prediction. Senders keep their order, so a hit costs one memcmp and no hash. Every hit is still a byte-for-byte match.
  5. Hand-off 12.8 → 9.5. try_ingest sizes each shard's vector for its share up front.

Tried and dropped: other snappy decoders, measured on the same bodies. snap 1.1.2 runs at 2.9 GB/s, the C++ library (snappy_src) at 1.5 GB/s, and the S2 decoder (minlz) at 1.5 GB/s, so snap stays. Decoding straight into per-shard vectors was not tried: after step 5 the hand-off costs 9.5 ns.

Tests

  • New tests/remote_write_reference.rs keeps the old decoder verbatim and checks that decode_samples gives the same series ids, timestamps, value bits (stale markers included) and errors on:
    • 300 generated requests: labels in sorted, reversed and shuffled order; labels interleaved with samples or after them; series appearing mid-stream; 0 to 3 samples per series; unknown fields; label lengths of one, two and three bytes.
    • Invalid series: no __name__, a duplicate label, a label that is not UTF-8, sample limits from 0 to 5.
    • Every truncation and 12,000 byte flips of encoded requests, plus truncated and corrupted snappy bodies.
    • The 1,080 captured demo48 bodies, with RW_CORPUS_DIR set (all 20.5 M samples match).
  • A mutation check: breaking the label fast path makes three of the four tests fail.
  • cargo fmt --check and cargo clippy -p data_plane --tests --examples are clean.
  • Release-mode results:
    • pass: --lib filtered to remote_write, store, series_ids, engine, ingestion and push; --test series_ids, server, remote_write_reference, no_forward, concurrency, push_precompute (3 of 3 runs), range_isolation, planner_executor_answers.
    • fail on the base too: ingest_shards::a_shard_running_ahead_does_not_close_windows_early is timing-dependent. It failed 2 of 3 runs on the base and fails here as well.
  • T15 fix (push_precompute::memory_reservations_wait_then_fail_only_a_lone_window). The test posted the small window's and the large window's requests back to back. It relied on the small window being sealed before the large window's samples were folded. With the faster decode they arrive while the small window is still open, so the large window's reservation waits (as designed) and nothing fails. Adding a 2 ms sleep per request makes it pass again, which confirms the cause. The test now seals the small window with a barrier at 10 s before posting the large one, and it passes on both the base and this branch.

Usage

# Decode cost on captured bodies (one file per request body):
cargo run --release -p data_plane --example rw_corpus_bench -- DIR [ROUNDS] [THREADS] [SHARDS]
# Compare the decoder with the reference on them:
RW_CORPUS_DIR=DIR cargo test --release -p data_plane --test remote_write_reference

rw_corpus_bench decodes a directory of captured Remote Write bodies and
reports ns/sample for snappy alone, the full decode, the hand-off to
ingest shards, and a threaded run as the server does it.

remote_write_reference keeps the decoder as it was and checks that
decode_samples gives the same samples, series and errors on generated
requests (label orders, interleaved fields, series appearing mid-stream,
unknown fields, stale markers), on every truncation and on byte flips,
and with RW_CORPUS_DIR set on captured bodies.
- Decompress into a buffer each thread keeps between requests, instead
  of a fresh zeroed allocation per request (page faults and zeroing
  were two thirds of the snappy cost).
- Inline the protobuf reader, with a one-byte varint fast path; skip a
  series' leading labels by their tag and short length; walk only the
  fields outside the labels for samples; read the timestamp varint of
  the sender's sample layout inline.
- Series are never forgotten, so the interner hands out &'static Series:
  no reference count is written per sample in decode, routing, chunk
  building or drop.
- The per-thread series cache predicts the next series from the order
  the sender used last time; a hit is one byte compare, no hash.
- try_ingest sizes each shard's vector for its share up front.

Same samples, series and errors as before (remote_write_reference).
memory_reservations_wait_then_fail_only_a_lone_window posted the small
and the large window's requests back to back and relied on the small
window being sealed before the large one's samples were folded. With
faster Remote Write decode the large window's samples arrive while the
small one is still open, so its reservation waits (as designed) and no
window fails. A barrier at 10 s seals the small window first.
@zzylol zzylol added enhancement New feature or request area: data plane Runtime ingestion, storage, and query-serving data paths labels Nov 24, 2026
…e body

A body whose snappy header claimed up to max_decompressed_bytes grew and
zeroed the thread's kept buffer, and a decompression error returned before
the keep cap was applied, so the thread held the buffer. The cap now
applies whatever the outcome, and a body over the kept size decompresses
into a fresh allocation.

test(data_plane): barrier before the 21 s window in
memory_reservations_wait_then_fail_only_a_lone_window, so no shard opens
it while another still folds the large window.
@zzylol
zzylol marked this pull request as ready for review November 24, 2026 02:37
@zzylol
zzylol merged commit 57144ee into feat/tiered-view-storage Nov 24, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: data plane Runtime ingestion, storage, and query-serving data paths enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant