Skip to content

perf(web): defer PDF repaints during Fit width drags with usePdfResize - #294

Closed
orbitingflea wants to merge 1 commit into
mainfrom
feat/pdf-resize-perf-v3
Closed

orbitingflea wants to merge 1 commit into
mainfrom
feat/pdf-resize-perf-v3

Conversation

@orbitingflea

@orbitingflea orbitingflea commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Behaviour change

Behaviour change: yes, Fit width panel resize drags stretch painted pages. Pages repaint sharp after.

Why

Keep the right-panel PDF viewer smooth during Fit width drags, which cancel unpainted preload renders.

What changed

PdfViewer.tsx uses usePdfResize. In a panel drag, paintWidth holds so bitmaps stretch, mounted pages stay in preload, and new off-screen pages wait. Holds end 150 ms after the last drag width change, or at once on a zoom change, new PDF, or non-drag width change.

Scope

PdfPage, layout, scroll, links, and Find are unchanged. Only panel drags hold. Window resizes, maximize, fixed zoom, and height-only resizes keep upstream behaviour.

Tradeoffs

Sharp pages appear 150 ms later. Mid-drag text and Find keep painted scale.

Blast Radius

Only Fit width panel drags in the PDF viewer change.

Verification

Eleven Chromium tests, existing PDF and file tests, format, lint, typecheck, knip, and React Compiler pass.

Implementation, tradeoff, and test details
  • Render loop. PdfPage keys repaints on page width, which changes each frame of a Fit width drag. Painted pages wait 150 ms. Unpainted pages start a pdf.js render, text-layer rebuild, and annotation fetch at once. The next frame cancels that work and drops caches. Narrower pages are shorter, so more enter preload and loop. Upstream pingdotgg/t3code#41537 keeps scroll, but still runs these renders.
  • Hook and wiring. apps/web/src/fork/pdfResize/usePdfResize.ts holds { pdf, paintWidth, mounted } | null. apps/web/src/components/files/PdfViewer.tsx reads visible from usePdfResize and passes paintWidth to PdfPage. A hold starts only on a Fit width change with a loaded PDF while useResizeDrag sets document.body.style.cursor to col-resize. It ends 150 ms after the last drag width change, or at once on a zoom change, new PDF, or non-drag width change. Window resizes, maximize, panel toggles, and post-release width changes pass through. Mounted pages stay in preload. Pages that leave unmount and remount if scrolled on screen. Scroll, #41537, saveView, and restoreView are unchanged.
  • Tradeoffs. The 150 ms settle runs before PdfPage's 150 ms wait. At settle, mounted pages rebuild text layers and drop pdf.js caches once, like upstream. Mid-drag text and Find clip until settle. A 150 to 300 ms pause may settle and repaint on resume. Scroll memory matches upstream.
  • Browser tests. apps/web/src/fork/pdfResize/PdfResize.browser.test.tsx tests eleven real-resize cases on a 12-page PDF in Chromium. A panel drag holds the reader's page with zero paints, then repaints sharp. Pages leaving and re-entering preload do not paint. Growth from a page top keeps the page number. Mid-drag scrolls hold position. Pages scrolled on screen or crossing the edge paint once in range. A slow drag of 100 ms steps stays held 140 ms after the last step and expands at 160 ms. A non-drag width change mounts pages at once, and a post-release width change before settle ends the hold. Both repaint on-screen pages within 160 ms. Zoom and height-only resizes pass through. Tests fail if you drop the paint-width hold, preload filter, height-only guard, or cursor check, keep the hold on a non-drag width change, or time settle from the first width change.

@github-actions github-actions Bot added vouch:trusted Author is trusted by repo permissions or the VOUCHED list. size:L labels Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

This PR changes the test suite by +11 test cases (browser +11, unit 0, server 0).

New browser test files: apps/web/src/fork/pdfResize/PdfResize.browser.test.tsx.
Each browser test file costs CI seconds of module loading before its first test runs; a unit test costs milliseconds.

Keep tests that would fail for a real reason, and test logic below the UI where it can be. See Testing.

@orbitingflea
orbitingflea force-pushed the feat/pdf-resize-perf-v3 branch from 14ad4b8 to 353e0ff Compare November 24, 2026 20:58
@github-actions

Copy link
Copy Markdown

Thread transfer impact

⚠️ The latest CI run did not produce a thread transfer result for 353e0ff.

This comment will update automatically after the next completed run.

A fork hook, usePdfResize, holds the width that pages were painted at
while a panel resize drag (the body cursor useResizeDrag sets) changes
the viewer's width in Fit width, so PdfPage does no pdf.js work and the
browser stretches the bitmap. Pages that enter the off-screen preload
range during the drag wait; pages on screen mount and paint. 150 ms
after the last width change, or at once on a zoom change, a new
document or a width change outside a drag, the hook passes everything
through and each mounted page repaints once at the final width. Window
resizes and other one-off width changes keep upstream behaviour.
Upstream's resize anchor keeps the reader's place; PdfPage and the
layout helpers are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@orbitingflea
orbitingflea force-pushed the feat/pdf-resize-perf-v3 branch from 353e0ff to cfa7247 Compare November 24, 2026 23:11
@orbitingflea

Copy link
Copy Markdown
Owner Author

Closing: upstream pingdotgg#41537 already fixes the PDF resize page jump and delivers most of the drag speedup with zero fork surface. Keeping this area aligned with upstream.

@orbitingflea
orbitingflea deleted the feat/pdf-resize-perf-v3 branch November 25, 2026 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant