Skip to content

write_path bench: stage1_build_section/uniform_16k +24% after #13 although lz77_encode/file_writer are untouched (code-layout sensitivity) #17

Closed

Description

@DrunkOnJava

Criterion benches vs main baseline failed twice on #13 (ratio 1.24, threshold 1.20) and reproduces locally (main 21.26 µs → branch 28.13 µs, p < 0.05) — while in the same run alt_1k improved 17%, mixed_256k improved 10%, and mixed_64k was flat.

#13 does not change src/lz77_encode.rs or src/file_writer.rs; its only bitwriter.rs change is a new write_dd that build_sections never calls. The cell that moved is the uniform-fill input (make_uniform(16 KiB, 0x55)), i.e. the longest-match path of the encoder — the most branch-predictor / alignment sensitive one.

To do:

  1. Profile stage1_build_section/uniform_16k on main vs current main+feat(tables): R2007+ LTYPE / BLOCK_HEADER string-stream decoders, LWPOLYLINE DD vertices, R2013/R2018 boundary pins #13 (cargo bench --bench write_path -- --save-baseline, then samply/perf) and confirm it is layout (inlining / function alignment) rather than a real path change.
  2. If layout: mark the match-search hot loop #[inline(never)]/#[inline] deliberately, or pad with -C llvm-args=-align-all-functions, so the bench is stable across unrelated code additions.
  3. Consider a noise floor in .github/workflows/bench.yml: fail only when the ratio ≥ 1.20 and the absolute delta is above some µs, or require two consecutive failing runs.

#13 was merged with this job red on that basis; this issue is the tracking item.

Activity

  1. DrunkOnJava commented on Aug 30, 2026

    @DrunkOnJava
    OwnerAuthor

    Measured this. Two corrections to the diagnosis, then a fix in #19.

    1. The premise is wrong: there is no match-search path. lz77_encode::compress is literal-only — one initial literal plus a terminator, no match search at all. So "uniform" vs "alternating" vs "mixed" is not a compressibility axis; the four cells differ only in size.

    An attribution probe (replace both compute_checksum call sites in build_section with black_box(0u32), re-run) puts the time here instead:

    cell with checksums without share
    alt_1k 1.71 us 0.23 us 86%
    uniform_16k 21.25 us 1.73 us 92%
    mixed_64k 104.6 us 8.27 us 92%
    mixed_256k 333.7 us 19.9 us 94%

    section_writer::compute_checksum is sum = rot1(sum) + b, a strict loop-carried dependency of two 1-cycle ops. 2 cycles/byte is the floor; vectorisation is impossible (rot1 is 2x mod 2^32-1, the add is mod 2^32, they do not distribute). Measured ~2.2 cycles/byte — already within ~11% of the floor.

    2. It is not code layout either. Six measurements of uniform_16k on an M2 Max, taking the minimum per-iteration time across criterion 100 samples instead of the mean:

    build unperturbed + an unrelated pub fn in bitwriter.rs
    rolled checksum (main) 20802 ns / 21893 ns 20960 ns
    8-way unrolled + #[inline(never)] 21830 ns 20867 ns

    All six inside a 20.80-21.89 us band, no consistent ordering by build; the same source measured twice moves 5.2%; the perturbed builds came out faster in both rows. So step (2) of this issue has nothing to pin — the unrolled + #[inline(never)] variant was written, measured, and dropped.

    What was actually unstable is the estimator. On a loaded machine criterion mean reported 21.25 us -> 32.99 us (+55%) for a binary whose minimum-of-the-same-samples showed +4.9%; mixed_256k in that run was mean +63% / min +1.5%. A 21 us bench on a shared GitHub vCPU cannot carry a 20%-ratio verdict alone — which is exactly the shape of the #13 run, where this cell "regressed" 24% while three siblings improved 10-17%.

    Fix (#19), step (3) of this issue. Ratio stays 1.20. A bench fails only when ratio >= 1.20 AND absolute delta >= 5 us AND the same bench trips both again on an immediate re-run; the table prints either way. 5 us is chosen so the cells it exempts (alt_1k ~1.7 us, uniform_16k ~21 us) exercise the same build_sections path as mixed_64k (~100 us) and mixed_256k (~334 us), which stay gated — no code path loses coverage.

    Also in #19: critcmp text-scraping replaced by reading criterion estimates.json with jq (drops a ~1 min cargo install, gives the exact ns delta), write_path.rs switched to iter_batched so the per-iteration payload clone stops being charged to the measurement, and compute_checksum gained the attribution numbers as a doc comment plus a test pinning its output against the spec 4.6.1 reference at every length and seed.

    Leaving this open rather than auto-closing — (1) and (2) are answered with measurements, (3) is implemented; closing is your call once #19 lands.

  2. DrunkOnJava commented on Aug 30, 2026

    @DrunkOnJava
    OwnerAuthor

    Resolved by #19 with measurements rather than the original hypothesis: the cell is ~92% section_writer::compute_checksum (2-cycle/byte dependency chain, already near its floor), lz77_encode::compress is literal-only, and the cell is stable to about 5% across builds with an unrelated code addition. The +24% was Criterion's mean on a loaded runner (the minimum moved +4.9%). perf.yml now fails only on ratio >= 1.20 AND delta >= 5 µs AND a reproducing re-run, reading estimates.json via .github/scripts/criterion-gate.sh. Cell relabelling tracked in #22.

  3. added
    type:bug
    and removed on Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions