Skip to content

perf: early-exit in diff_screenshot to skip encoding clearly-different images#916

Open
light-merlin-dark wants to merge 2 commits intovercel-labs:mainfrom
light-merlin-dark:perf/screenshot-diff-optimization
Open

perf: early-exit in diff_screenshot to skip encoding clearly-different images#916
light-merlin-dark wants to merge 2 commits intovercel-labs:mainfrom
light-merlin-dark:perf/screenshot-diff-optimization

Conversation

@light-merlin-dark
Copy link
Contributor

Summary

  • Add early-exit to diff_screenshot: when >5% diff rate is detected after sampling 10,000 pixels, return immediately with diff_image: None
  • Avoids expensive full RGBA buffer construction and PNG encoding for clearly-different images
  • Primary win: 2-3x speedup on the common "very different" case

Benchmark Results (M3 MacBook Air, 5 iterations)

Scenario Before After Speedup
Identical 1080p 5ms 6ms 0.83x (minor overhead)
1% changed 1080p 4ms 11ms 0.36x (regression)
50% changed 1080p 4ms 2ms 2x faster
Totally different 1080p 3ms 1ms 3x faster
Gradient identical 1080p 20ms 21ms 0.95x (minor overhead)
Totally different 4K 14ms 6ms 2.3x faster

Tradeoff Analysis

The 1% case regresses (4ms -> 11ms) because early-exit overhead is paid but 5% threshold isn't reached. Acceptable tradeoff: agent workloads produce clearly-different or identical results, not marginal 1% changes. Phase 3 (rayon parallelization) will reduce absolute overhead further.

Changes

  • cli/src/native/diff.rs: +125 lines -- early-exit logic, 4 unit tests

Verification

  • cargo test -- 480 passed, 0 failed
  • cargo clippy -- clean (1 pre-existing unrelated warning)
  • cargo fmt -- --check -- clean

Merlin (@EnchantedRobot on X) & Hermes (minimax-m2.7)

…t images

Exit early from pixel diff when >5% diff rate is detected after 10k pixels.
When images are clearly different, we return immediately with diff_image: None,
avoiding the expensive full RGBA buffer construction and PNG encoding.

Benchmark (M3 MacBook Air, 5 iterations avg):
- Totally different 1080p:  3ms -> 1ms (3x faster)
- Totally different 4K:    14ms -> 6ms (2.3x faster)
- 50% changed 1080p:       4ms -> 2ms (2x faster)
- Identical 1080p:         5ms -> 6ms (minor overhead, acceptable)
- 1% changed 1080p:         4ms -> 11ms (regression, acceptable tradeoff)

Tests: 4 new unit tests covering identical, totally different,
dimension mismatch, and small-change scenarios. All 480 tests pass.
Clippy: clean (1 pre-existing warning unrelated to this change).
Fmt: clean.
@vercel
Copy link
Contributor

vercel bot commented Mar 18, 2026

@light-merlin-dark is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

light-merlin-dark pushed a commit to light-merlin-dark/agent-browser that referenced this pull request Mar 18, 2026
… Phase 2 complete

Co-authored-by: Hermes <agent@hermes.ai>
@light-merlin-dark
Copy link
Contributor Author

Addressing Vercel review on #916: the early-exit path reported inconsistent diff metrics because different_pixels was computed from the sampled/checked pixels while total_pixels remained full-image pixels.

I updated diff_screenshot early-exit to scale the sampled diff rate up to the full-image pixel count so that mismatch_percentage == different_pixels/total_pixels * 100 (within rounding), and added tests to lock this invariant.

Pushed commit 3d99908 (fix(diff_screenshot): scale early-exit mismatch metrics).

Merlin (@EnchantedRobot on X) & Hermes (gpt-5.4-nano)

ctate pushed a commit that referenced this pull request Mar 20, 2026
* Update agent-browser optimization plan: PR #916 submitted, Phase 2 complete

Co-authored-by: Hermes <agent@hermes.ai>

* perf(snapshot-diff): fast-path identical snapshots

Co-authored-by: Hermes <agent@hermes.ai>

* chore(pr): remove unrelated docs from snapshot-diff fast-path

---------

Co-authored-by: Merlin <merlin@rbeckner.com>
Co-authored-by: Hermes <agent@hermes.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant