perf: cache canvas dimensions and debounce render-list invalidation#119
Merged
Merged
Conversation
Two render-loop hot-path fixes: - Cache canvas.width/height as plain numbers on WebGlContextWrapper (canvasW/canvasH). On HTMLCanvasElement these are Blink DOM getters, too slow to read per render op per frame on embedded targets — they were read in every bindRenderOp (u_resolution) and in the scissor math of every clipped quad/SDF draw. The canvas is only resized via Renderer.updateAppDimensions -> updateViewport, which now refreshes the cache. Also types the previously implicit-any canvas field. - Debounce Stage.requestRenderListUpdate: invalidateQuadBuffer walks the entire render list resetting slot assignments, and was invoked once per node whose renderable state flipped. When a whole row scrolls out of the viewport that meant N full-list walks in one frame; now the first call invalidates and the rest early-return until drawFrame rebuilds the list. requestRender still runs on every call so scheduling is unchanged. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
What
Two render-loop hot-path fixes from a review of the standard TV-app scroll scenario (clipped viewport, rows of image/text cards translating in both axes):
1. Cache
canvas.width/canvas.heightonWebGlContextWrapper(canvasW/canvasH)On
HTMLCanvasElementthese are Blink DOM getters, and they were being read in the hottest per-op paths:WebGlShaderProgram.bindRenderOp—u_resolutionresolution, 2 reads per render op per frameCoreNode.draw/SdfRenderOp.draw— scissor math, 1 read per clipped op per frameOn Chrome-38-class embedded targets each read crosses into the DOM layer. The canvas is only ever resized through
Renderer.updateAppDimensions()→renderer.updateViewport(), soupdateViewportis the single refresh point for the cache (updateCanvasDimensions()). Constructor seeding is safe becauseRenderersizes the canvas before the wrapper is constructed.Also types the previously implicit-
anycanvasfield asHTMLCanvasElement | OffscreenCanvas.2. Debounce
Stage.requestRenderListUpdate()invalidateQuadBuffer()walks the entire render list resetting per-node buffer-slot assignments, and was invoked once per node whose renderable state flipped. When a whole row scrolls out of the viewport, every card in it flipsisRenderablein the same frame → N full-list walks per frame. Now the first call performs the invalidation and subsequent calls early-return untildrawFramerebuilds the list. This is safe because the render list cannot change between the dirty flag being raised and the rebuild in the same frame'sdrawFrame.requestRender()still runs unconditionally, so render scheduling is byte-for-byte unchanged.Reviewer notes
glw.canvasHinstead ofoptions.canvas.height) is the riskiest consumer — verified visually against theclippingexample: all three clipping sections render pixel-correct, SDF text (same scissor path) included.uniformDeduptest mock was updated fromcanvas: {width, height}tocanvasW/canvasHto match the new read.Testing
pnpm build(tsc) clean; changed files Prettier/ESLint cleanWebGlContextWrapperdimension-cache seeding/refresh (2),Stage.requestRenderListUpdatededupe semantics incl. canvas-backend withoutinvalidateQuadBuffer(4)?test=clipping, screenshot verified + clean console🤖 Generated with Claude Code