#3895: perf: mathematical optimization of vision pipeline via deterministic scaling
This PR addresses the high CPU overhead and memory crashes in the screenshot normalization process (Fixes #3870, #3875).
Changes: > - Replaced iterative resizing loops with a single-pass O(1) scale calculation.
Implemented area-to-byte proportionality logic (Square Root Law) for precise sizing.
Reduced ingestion latency significantly for high-resolution displays.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR replaces the prior “grid search” screenshot normalization (multiple resize+quality attempts until under `maxBytes`) with a single-pass deterministic scaling computation based on dimension and byte-budget ratios.
The change is localized to `src/browser/screenshot.ts`, which is used by the browser snapshot routes to constrain screenshot payload size before saving/serving.
Main issues:
- The updated file currently references removed imports/constants (`getImageMetadata`, `resizeToJpeg`, `DEFAULT_BROWSER_SCREENSHOT_MAX_*`), which should break compilation.
- The new return type/contentType implies JPEG output for all paths, but the early-exit can return a non-JPEG input buffer while labeling it as `image/jpeg`.
- The deterministic byte scaling is heuristic; without a post-check/fallback, outputs can still exceed `maxBytes` for certain screenshot content.
<h3>Confidence Score: 1/5</h3>
- Not safe to merge as-is due to likely compile/runtime breakage and incorrect contentType guarantees.
- `src/browser/screenshot.ts` appears to have removed required imports/exports but still references them, which should fail to build. Additionally, the function now claims to always return JPEG while the early-exit can return the original (possibly PNG) buffer, which can break downstream consumers relying on contentType/buffer alignment.
- src/browser/screenshot.ts
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
**Context used:**
- Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
- Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13))
<!-- /greptile_comment -->
Most Similar PRs
#2958: fix(media): wire tools.media.image.maxBytes config to image processin…
by shamsulalam1114 · 2026-01-27
79.0%
#9598: fix(agents): check base64 string length against 5MB API limit
by BlockBB · 2026-02-05
75.6%
#11342: fix(browser): respect fullPage=false for viewport-only screenshots
by higginsvott · 2026-02-07
73.2%
#7316: fix: /chat dashboard performance
by felipcsousa · 2026-02-02
72.8%
#23706: perf: cache image resize results to avoid redundant processing (#23...
by echoVic · 2026-02-22
72.4%
#4269: fix: support tool result image format in Control UI
by bobcyw · 2026-01-30
72.2%
#23662: fix: cache sanitized images to avoid redundant re-processing per turn
by davidemanuelDEV · 2026-02-22
71.3%
#23639: fix(agents): stop re-resizing session history images on every turn ...
by yinghaosang · 2026-02-22
71.2%
#20913: fix: intercept Discord embed images to enforce mediaMaxMb
by MumuTW · 2026-02-19
71.0%
#6819: fix(tui): handle unstructured tool results and errors in tool execu...
by TreyDong · 2026-02-02
70.3%