Skip to content

perf(bench): pre-touch output Vec before measurement loop to eliminate page-fault artifact #257

Description

@polaz

Context

Flamegraph on i9 of `decompress/level_-3_fast/decodecorpus-z000033/c_stream/matrix/pure_rust_direct` shows:

  • 82% `__memmove_avx_unaligned_erms` (decoder match-copy + literal-push)
  • 67% `do_user_addr_fault` + 60% `handle_mm_fault` + 55% `do_anonymous_page` (anonymous-page allocation)
  • 34% `alloc_anon_folio`, 20% `vma_alloc_folio_noprof`

The page-fault stack dominates because each criterion iteration allocates a FRESH output Vec sized to the declared FCS, and the kernel lazily maps anonymous pages on first touch. The cost amortises poorly because criterion runs >100 iterations and each one re-pays.

This is a bench-harness artifact, not a decoder property — the dashboard shows `-54.68%` vs donor on this fixture mostly because of this, not because of actual decoder slowness.

Proposal

In `zstd/benches/compare_ffi.rs`, the closure given to `b.iter` should:

  1. Allocate the output Vec once outside the iter (in the surrounding `bench_function_with_input`).
  2. Pre-touch every page by writing 0 (e.g. `std::ptr::write_bytes(buf.as_mut_ptr(), 0, fcs)` or `buf.fill(0)`) BEFORE `b.iter`.
  3. Inside the iter, reuse the same buffer (caller passes `&mut buf[..fcs]` to the decoder).

For the `pure_rust_direct` path that uses `decode_to_slice_trusted`, the slice already lives outside; just ensure pre-touch happens.

Acceptance criteria

  • All `compare_ffi` decompress arms (`pure_rust`, `pure_rust_direct`, `c_ffi`) pre-allocate and pre-touch their output buffer.
  • Flamegraph on `decodecorpus-z000033` L-3 c_stream pure_rust_direct: page-fault stack drops below 5% of total.
  • Dashboard delta on the fixture improves measurably (artifact removed; remainder is actual decoder cost).
  • No regression on bench correctness — output validated against expected FCS bytes.

Part of #247.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2-mediumMedium priority — important improvementenhancementNew feature or requestperformancePerformance optimization

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions