Repository navigation
perf(web): defer PDF repaints during Fit width drags with usePdfResize - #294
Open
orbitingflea wants to merge 1 commit into
Open
orbitingflea wants to merge 1 commit into
orbitingflea wants to merge 1 commit into
Conversation
|
This PR changes the test suite by +8 test cases (browser +8, unit 0, server 0). New browser test files: 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
force-pushed
the
feat/pdf-resize-perf-v3
branch
from
November 24, 2026 20:58
14ad4b8 to
353e0ff
Compare
Thread transfer impact
This comment will update automatically after the next completed run. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.tsxusesusePdfResize. During a drag,paintWidthholds 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
PdfPagerepaint 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. Upstreampingdotgg/t3code#41537keeps the reader's place, but does not cut render cost.apps/web/src/fork/pdfResize/usePdfResize.tsholds one drag state,{ pdf, paintWidth, mounted } | null.apps/web/src/components/files/PdfViewer.tsxcallsusePdfResize, readsvisible, and passespaintWidthtoPdfPage. 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,visibleandpaintWidthpass through so mounted pages repaint once at the final width. Scroll position, thepingdotgg/t3code#41537scroll anchor,saveView, andrestoreViewstay unchanged.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.apps/web/src/fork/pdfResize/PdfResize.browser.test.tsxdrives 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.