perf(scripts): add a device-free PNG crop benchmark

Merged
TH
2mo ago
+581-0

Summary

Adds pnpm bench:png-crop, which runs both crop pipelines over the same bytes as PNG worker jobs in one process, so content, deflate stream, thread and machine stay fixed. Both sides pay the job round trip the shipped command pays:

  • whole-image — the previous crop: one job decodes the whole capture to RGBA, the box rows are copied out of that bitmap, a second job encodes the box.
  • region — the shipped crop: one job reads the box's rows and encodes them.

Neither writes a file; publishing the artifact is the same work on both sides, so it would only dilute the ratio, and each row reports the encoded byte length instead. Neither side reads less of the capture either: a deflate stream has to be inflated to its end, so both inflate the whole compressed image, and the region path inflates it into a buffer sized for every filtered row. What stops at the box is the pixel work.

Six generated captures at iPhone, Android-phone and iPad resolutions cross three crop-box shapes (card, header, control) that match what --crop-on resolves.

pnpm bench:png-crop -- --rounds 5 --file ./real-capture.png

The corpus is generated, so a run costs seconds with no device. Every corpus entry prints its own compressed size against a real capture's, which is how a corpus that stopped resembling a device becomes visible instead of flattering. Real captures join the same table with --file, and they decide the verdict.

12 files, 581 gross lines: ten new tooling files plus one package script and one gate registration; no production path is touched.

Validation

Validated at the stack head 03baaa59c3, on base 71a4386108 from #2504. This layer carries no production code; the parser fix and the live Android crop evidence landed in the lower layer.

pnpm check:affected --run runs 368 test files / 2,350 tests plus format, oxlint, typecheck, layering, di-seams and the eager-closure budget suite clean. mutation-model and production-exports fail identically at origin/main in this worktree (63 export findings at the merge base, none in png-* or scripts/png-crop-benchmark) — pre-existing, unrelated. pnpm check:fallow --base origin/main reports no issues in 29 changed files. scripts/png-crop-benchmark/*.test.ts (12 tests) run in unit-core via the chore(gates) commit here.

Numbers quoted in the README come from 7-round runs at this commit over three real captures: 2.6x to 5.6x faster and up to 2.16x smaller on a 245 kB iOS UI capture, 1.78x to 2.52x faster and up to 1.31x smaller on a 3 MB iOS photo capture, and a wash on time (0.98x to 1.64x) with a crop up to ~13% larger on a noisy 1.4 MB Android screencap. The README states that losing case rather than averaging it away, and says to check your own captures with --file before reading a win or a loss into any number here.