Skip to content

Restructure resize_only_bench to isolate Resizer cost from clone+encode overhead #135

Open

Description

@utof

Context

Batch J (architecture audit M1) added resize_only_bench_baseline_vs_reused_proves_amortization in crates/media/src/thumbnail.rs to verify the per-worker fast_image_resize::Resizer amortization landed in #111. The test compares per-call Resizer::new() vs a single shared Resizer across N=20 iterations of a 1920×1080 RGB resize.

Problem

The intended ≥5% speedup assertion was empirically too noisy to enforce. A 15-run release reproduction on the maintainer's VM showed:

  • 11 PASS, 4 FAIL (27% flake rate against ≥1.05 threshold)
  • Per-run speedup ratio swings 0.890x — 1.049x — i.e. some runs measured the reused Resizer as slower than per-call allocation
  • Mean ~1.02x, well below the audit's 10% target and the conservative 5% lower bound

The signal is dominated by per-iter DynamicImage::clone() (~6 MB memcpy per 1920×1080 RGB image) plus the WebP encode round-trip in the existing resize_image() path. The Resizer-internal scratch-buffer + CPU-extension dispatch cache savings are a tiny fraction of that wall-clock.

Mitigations to investigate

  1. Bypass DynamicImage::clone() — pre-allocate one source Image<&[u8]> and call resizer.resize(&src, &mut dst, None) directly, without going through resize_image()'s coercion + WebP encode path.
  2. Subtract a pure-clone-baseline pass from total wall-clock to expose the Resizer-only delta.
  3. Use fast_image_resize's upstream benchmark methodology — Image::from_vec_u8 source + same destination buffer reused across iters.
  4. Adopt criterion workspace dep for statistical rigor (warmup + outlier rejection). Currently rejected as out-of-charter (Batch J spec §4.2 / §14) — revisit when a 2nd benchmark surfaces.

Current state

The test is committed as print-only (no assertion) per spec §7 option B + §8 risk #4. The eprintln line still surfaces baseline + reused durations + ratio so a human reading CI logs can spot a regression by eye, but the bare assertion would create false-positive CI failures at the observed flake rate.

Acceptance

A restructured test that:

  • Runs in cargo nextest run -p perima-media debug + release without #[ignore]
  • Asserts a stable threshold (≥5% if achievable; lower if the signal floor genuinely isn't there with image::DynamicImage-shaped inputs)
  • Documents the methodology in a WHY-block referencing this issue + spec §8 risk rustfmt nightly-only options produce warnings on stable #4

If a 5% assertion remains unstable after restructuring, document why and either lower the threshold (per spec §7 option A) or keep it print-only with a closing comment explaining the limitation.

References

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

    status/needs-research

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions