Skip to content

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

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

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 right-panel drags stretch painted pages. Pages repaint sharp once the width holds.

Why

Fit width drags cancel unpainted preload renders. This PR defers repaints.

What changed

PdfViewer.tsx uses usePdfResize. During a drag, paintWidth holds so bitmaps stretch, new off-screen pages wait, and on-screen pages paint once. Mounted pages repaint once at the final width 150 ms after the last width change.

Scope

PdfPage, layout, scroll, links, and Find are unchanged. Fixed zoom and height-only resizes start no drag. A zoom change ends a drag at once, so fixed zoom repaints at its width with the normal preload range.

Tradeoffs

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

Blast Radius

Only Fit width drags in file preview and the right panel change.

Verification

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

Implementation, tradeoff, and test details
  • Render loop. Each PdfPage repaint effect keys on page width, which changes every frame of a Fit width drag. Painted pages wait 150 ms. Unpainted mounted pages start a pdf.js render, text-layer rebuild, and annotation fetch at once. The next frame cancels that work and drops pdf.js caches. Narrower pages are shorter, so more enter the preload range of one viewport above and two below and repeat that loop. Upstream pingdotgg/t3code#41537 keeps the reader's place, but does not cut render cost.
  • Hook and wiring. apps/web/src/fork/pdfResize/usePdfResize.ts holds one drag state, { pdf, paintWidth, mounted } | null. apps/web/src/components/files/PdfViewer.tsx calls usePdfResize, reads visible, and passes paintWidth to PdfPage. A drag starts on the first Fit width change with a PDF loaded and ends 150 ms after the last width change, or at once on a zoom change. Mounted pages stay mounted in the preload range. At drag end, visible and paintWidth pass through so mounted pages repaint once at the final width. Scroll position, the pingdotgg/t3code#41537 scroll anchor, saveView, and restoreView stay unchanged.
  • Tradeoffs. The 150 ms settle runs before PdfPage's own 150 ms wait. At settle, mounted pages rebuild text layers and drop pdf.js page caches once, as on any upstream width change. Mid-drag text layers and Find highlights clip until settle. A 150 to 300 ms drag pause can settle and repaint as the drag resumes. This change adds no scroll memory over upstream.
  • Browser tests. apps/web/src/fork/pdfResize/PdfResize.browser.test.tsx drives real resizes on a 12-page PDF in Chromium and counts each pdf.js paint once per page across eight cases. During a drag, the reader's page holds with zero paints, then every mounted page repaints sharp. Pages that leave and re-enter the preload range do not paint, and the page number holds when width grows from the top of a page. A mid-drag scroll holds its place, and pages scrolled on screen paint once each. A page crossing the screen edge mid-drag paints once. A mid-drag zoom change passes the zoomed preload range through, and a height-only resize mounts the taller preload range at once. Removing the paint-width hold, the preload filter, or the height-only guard fails these tests.

@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 +8 test cases (browser +8, 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.

A fork hook, usePdfResize, holds the width that pages were painted at
while the viewer's width changes 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 once. 150 ms after the last width change the hook passes
everything through and each mounted page repaints once at the final
width. 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 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.

This branch has not been deployed

No deployments
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