Skip to content

perf(png): reconstruct scanlines with one filter dispatch - #11818

Merged
vincentkoc merged 1 commit into
morluto:mainfrom
Afloat16:perf/png-scanline-filter-dispatch-20261117
Nov 30, 2026
Merged

vincentkoc merged 1 commit into
morluto:mainfrom
Afloat16:perf/png-scanline-filter-dispatch-20261117

Conversation

@Afloat16

@Afloat16 Afloat16 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Reduce CPU work when comparing existing PNG screenshots by selecting the reconstruction filter once per scanline. Each filter now reads only its required neighbors; unfiltered rows use a buffer copy. The decoded pixels, comparison metrics, supported formats and admission limits remain unchanged.

Problem and expected behavior

At base af062e7a2d290fec4307fb9444298a13c2fea630, unfilter computes all three neighbor values and dispatches through applyFilter for every byte, even though a row has one fixed filter. A 1920 × 1080 RGBA image therefore repeats that selection 8,294,400 times instead of 1,080 times.

The expected contract is identical reconstruction with less repeated work. This is a performance change, not a claim that existing filter arithmetic is incorrect. No separate issue is linked because the concrete reproduction, measurements and proposed repair are included here.

Related #11223 adds filter-coverage tests. This change addresses the reconstruction loop itself. Historical positional-helper implementations also still selected the filter per byte; removing an options object is not the basis of this optimization.

Change and scope

Move the switch outside the byte loop and keep one loop per filter. None copies the row, Sub reads left, Up reads above, Average reads left and above, and Paeth reads all three neighbors. The Paeth predictor and tie-breaking code are unchanged. Unsupported filter values still fail.

The PR contains only the implementation and ten fixed RGB/RGBA scanline tests. Benchmark scripts, fixtures and validation workflow are retained separately as evidence.

Contract and boundary impact

  • Semantic owner and earliest changed stage: PNG scanline reconstruction in pngVisualDiff.ts; screenshot artifact comparison keeps the same pixel and metric contracts.
  • Contribution scope: ordinary optimization within the existing PNG decoder; no advance scope decision required.
  • CLI/MCP and provider compatibility: no input, output schema, tool, provider, format or platform change.
  • Evidence/provenance: image identity, dimensions, transparency handling, comparison thresholds and unsupported-result limitations remain unchanged.
  • Target authority and cleanup: the optimization processes already supplied image buffers; no new target interaction or resource lifetime.
  • Generated metadata, packaging and installation: no changes.

Expected effects:

  • Read existing files or captured data.
  • Launch or attach to a process.
  • Inject instrumentation or run scripts/code.
  • Change process memory, registers, files, or configuration.
  • Contact network services.
  • Recover protected code or work around protections.
  • Documentation or usability only; no target interaction.
  • Not sure yet; investigation or maintainer guidance needed.

Evidence and regression coverage

The ten new vectors cover all five filters for both RGB and RGBA, first and later rows, initial columns and byte wraparound. Their PNG encodings were independently decoded and checked byte-for-byte with Pillow 13.0.0. The tests exercise the public screenshot-artifact and comparison APIs against explicit expected RGBA bytes. They pass before and after; the improvement is established by native measurements rather than a claimed functional failure.

The native benchmark uses Pillow-produced 640 × 360 RGB, 1920 × 1080 RGB and 1920 × 1080 RGBA images, each with identical and changed-image cases. An independent Pillow pixel oracle checks every comparison metric. Each timed task creates both screenshot artifacts, parses the comparison input and calls comparePngScreenshots. Disk reads, module import and fixture production are outside the timed task.

On Ubuntu 24.04 with Node 24.22.1, three alternating before/after rounds used 36 fresh processes and 252 completed public tasks. Every complete output matched byte-for-byte. Cold is the median first task from three fresh processes; warm is the median of calls 5–7 in those processes after four warm-up calls, giving nine measurements per phase and case.

Completed task Cold before / after (ms) Warm before / after (ms)
RGB 640 × 360, identical 32.480 / 23.469 23.979 / 14.271
RGB 640 × 360, changed 33.236 / 27.128 23.719 / 14.232
RGB 1920 × 1080, identical 175.836 / 127.061 163.130 / 115.460
RGB 1920 × 1080, changed 175.643 / 128.625 161.655 / 109.491
RGBA 1920 × 1080, identical 170.490 / 101.522 164.188 / 78.923
RGBA 1920 × 1080, changed 172.651 / 101.249 164.992 / 78.793
Completed task Warm CPU before / after (ms) Process peak RSS before / after (KiB)
RGB 640 × 360, identical 35.204 / 20.965 124,524 / 116,984
RGB 640 × 360, changed 34.941 / 20.706 123,680 / 116,236
RGB 1920 × 1080, identical 196.985 / 147.259 166,500 / 158,228
RGB 1920 × 1080, changed 194.322 / 139.915 166,800 / 157,460
RGBA 1920 × 1080, identical 195.760 / 104.541 160,160 / 161,724
RGBA 1920 × 1080, changed 197.655 / 103.842 162,904 / 161,540

These measurements support approximately 29–52% lower median warm elapsed time and 18–41% lower median cold elapsed time for these tasks. They do not establish an end-to-end browser capture, network, token or general memory improvement; one RGBA case has a higher process peak. The real-producer fixtures predominantly exercise Up, Paeth and Sub; the fixed vectors separately cover all five filters.

For evidence-bearing changes:

  • Observed, derived, and inferred claims remain distinguishable.
  • Artifact identity, source provenance, and failed attempts remain preserved.
  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Validation performed

At the pinned base plus the exact two files in this PR:

  • npm run test:local -- src/domain/pngVisualDiff.test.ts src/domain/pngHeader.test.ts — 53 passed before and 53 after.
  • npm run test:focused -- tests/boundary/cli/browserCli.test.ts tests/boundary/browser/browserToolContractExamples.test.ts — 19 passed against the compiled package.
  • npm run check — passed, including typecheck, lint, formatting, Knip, metadata and architecture checks; the run reports zero errors and 5,026 advisory lint warnings.
  • git diff --check — passed; the isolated source worktree changed only these two files.
  • The independent pixel oracle and full-output equality checks passed for all 252 timed public tasks.
  • Successful native validation, exact-file hashes and full evidence. Production readback matches both tested file hashes.
  • Durable full evidence archive, including all individual timings, complete outputs, PNG inputs, independent expected metrics and command logs.

An earlier workflow attempt selected built boundary tests with test:local and stopped with the runner's instruction to use test:focused. The corrected run above completed. That earlier attempt is retained and is not counted as a pass.

Compatibility, safety, and release

No breaking change, migration, dependency or package change. The comparison budget, CRC checks, image admission, failure behavior and schemas are unchanged. Validation covers native Linux/Node decoding and compiled CLI/browser-contract tests using the repository's controlled CDP fixture; it does not claim a live browser or other-OS performance measurement. No new execution, mutation or external communication capability is introduced.

Author readiness

Human owner: @Afloat16

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

@greptile-apps

greptile-apps Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge; no actionable issue was found.

Summary

Moves PNG filter selection outside the byte loop and adds ten RGB/RGBA test vectors.

  • PNG images now use one filter choice per row.

Reviews (2) · Last reviewed commit: "perf(png): dispatch reconstruction filte..." · Reviewed by Greptile

@morluto morluto added the performance Performance regressions, scaling limits, and bounded-resource work label Nov 22, 2026
@Afloat16
Afloat16 marked this pull request as ready for review November 23, 2026 01:47
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T01:58:29.996553Z 5cd7fb7 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@vincentkoc
vincentkoc merged commit 901d9a1 into morluto:main Nov 30, 2026
44 of 45 checks passed
vincentkoc added a commit to WoWHellgarve-HolyDeeW/rea that referenced this pull request Nov 30, 2026
* commit '7f7d34ad7c30a5fe015b642118dd40e8e9ac093d': (167 commits)
  fix(artifacts): classify member presence from the absent side coverage (morluto#15200)
  fix(tar): account for retained pax owner names (morluto#15195)
  fix(conformance): validate paired fixture count expectations (morluto#14125)
  fix(nds): report unreachable file name directories (morluto#15192)
  refactor(firmware): share admitted-file decoder buffering (morluto#15198)
  fix(binary): carry the runtime refusal's repair instruction in remediation (morluto#14123)
  fix(agentenv): reject COM0 and LPT0 bundle names (morluto#14971)
  test(environment): share the loopback capture fixture (morluto#15196)
  perf(png): reconstruct scanlines with one filter dispatch (morluto#11818)
  feat(iso9660): stream files across separated extents (morluto#15190)
  perf(apple): build only the requested asset catalog page (morluto#14032)
  perf(artifacts): isolate offline session composition from deep providers (morluto#15194)
  perf(evidence): bound investigation closure worklist by depth (morluto#15193)
  perf(audit): avoid copying complete log lines (morluto#15184)
  test(mcp): share server result readers (morluto#15174)
  fix(android): declare Android mapping and JNI fixed allowances (morluto#15183)
  fix(artifacts): retain deep comparison source chains (morluto#15191)
  perf(evidence): bound link inspection frontier by records (morluto#15189)
  perf(evidence): retain snapshots across duplicate imports (morluto#15186)
  fix(nro): exclude ASET header bytes from asset ranges (morluto#15185)
  ...
vincentkoc added a commit to XiaoBinGan/rea that referenced this pull request Nov 30, 2026
* commit '4ca43e2cb20f0edd86cdbf315a9c99c7b9b87468': (173 commits)
  fix(paths): reject Windows console device names (morluto#14972)
  fix(artifacts): classify member presence from the absent side coverage (morluto#15200)
  fix(tar): account for retained pax owner names (morluto#15195)
  fix(conformance): validate paired fixture count expectations (morluto#14125)
  fix(nds): report unreachable file name directories (morluto#15192)
  refactor(firmware): share admitted-file decoder buffering (morluto#15198)
  fix(binary): carry the runtime refusal's repair instruction in remediation (morluto#14123)
  fix(agentenv): reject COM0 and LPT0 bundle names (morluto#14971)
  test(environment): share the loopback capture fixture (morluto#15196)
  perf(png): reconstruct scanlines with one filter dispatch (morluto#11818)
  feat(iso9660): stream files across separated extents (morluto#15190)
  perf(apple): build only the requested asset catalog page (morluto#14032)
  perf(artifacts): isolate offline session composition from deep providers (morluto#15194)
  perf(evidence): bound investigation closure worklist by depth (morluto#15193)
  perf(audit): avoid copying complete log lines (morluto#15184)
  test(mcp): share server result readers (morluto#15174)
  fix(android): declare Android mapping and JNI fixed allowances (morluto#15183)
  fix(artifacts): retain deep comparison source chains (morluto#15191)
  perf(evidence): bound link inspection frontier by records (morluto#15189)
  perf(evidence): retain snapshots across duplicate imports (morluto#15186)
  ...
vincentkoc added a commit to ShuaixinHuang/rea that referenced this pull request Nov 30, 2026
* commit '4ca43e2cb20f0edd86cdbf315a9c99c7b9b87468': (34 commits)
  fix(paths): reject Windows console device names (morluto#14972)
  fix(artifacts): classify member presence from the absent side coverage (morluto#15200)
  fix(tar): account for retained pax owner names (morluto#15195)
  fix(conformance): validate paired fixture count expectations (morluto#14125)
  fix(nds): report unreachable file name directories (morluto#15192)
  refactor(firmware): share admitted-file decoder buffering (morluto#15198)
  fix(binary): carry the runtime refusal's repair instruction in remediation (morluto#14123)
  fix(agentenv): reject COM0 and LPT0 bundle names (morluto#14971)
  test(environment): share the loopback capture fixture (morluto#15196)
  perf(png): reconstruct scanlines with one filter dispatch (morluto#11818)
  feat(iso9660): stream files across separated extents (morluto#15190)
  perf(apple): build only the requested asset catalog page (morluto#14032)
  perf(artifacts): isolate offline session composition from deep providers (morluto#15194)
  perf(evidence): bound investigation closure worklist by depth (morluto#15193)
  perf(audit): avoid copying complete log lines (morluto#15184)
  test(mcp): share server result readers (morluto#15174)
  fix(android): declare Android mapping and JNI fixed allowances (morluto#15183)
  fix(artifacts): retain deep comparison source chains (morluto#15191)
  perf(evidence): bound link inspection frontier by records (morluto#15189)
  perf(evidence): retain snapshots across duplicate imports (morluto#15186)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance regressions, scaling limits, and bounded-resource work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants