fix(text): bound SDF layout cache eagerly; debug overlay uses SDF#78
Merged
Conversation
Two fixes for a JS-heap leak (OOM / black screen) observed running the debug overlay over animated text for several minutes on a low-RAM device. 1. examples debug overlay (?debug=true) used the Canvas text renderer (no fontFamily/SDF specified). Canvas text rasterizes a fresh ImageTexture (a full bitmap) on every text change, and the overlay rewrites its text every interval — ~390KB x ~2/s. Force SDF so the per-interval update draws from the shared atlas with no texture allocation. This was the dominant leak (~0.9MB/s). 2. The SDF layout cache only evicted on idle. A continuously animating scene never goes idle, so any app with ever-changing SDF text (clock, counter, score, fps readout) grew the cache without bound. Bound it eagerly on insert too — a single Map delete on a cache miss, so it doesn't compete with steady-state rendering. Idle cleanup is retained for bulk trimming. Verified: the original repro (stress-single-level-text&debug=true&multiplier=10) went from ~0.9 MB/s heap growth to flat (~0.004 MB/s) over a multi-minute run. Co-Authored-By: Claude Opus 4.8 (1M context) <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.
The bug
Running the debug overlay over animated text for several minutes on a low-RAM device leaked JS heap until OOM → the renderer's context died → black screen / text stops drawing. Repro:
?test=stress-single-level-text&debug=true&multiplier=10.Investigation (measured, not guessed)
Sampled
performance.memoryacross controlled variations:debug=truecontextSpy+fps, no overlayThe leak only appears with
debug=true, i.e. the debug overlay — not the test.Fixes
1. Debug overlay → SDF text (
examples/index.ts) — the dominant leak (~0.9 MB/s)The overlay's text node specified no font, so it used the Canvas text renderer, which rasterizes a fresh
ImageTexture(a full bitmap, ~390 KB at this size) on every text change (CoreTextNode.ts:254) — and the overlay rewrites its text every interval (~2/s). ForcingfontFamily: 'Ubuntu'+textRendererOverride: 'sdf'makes it draw from the shared atlas with no per-update texture allocation.2. Bound the SDF layout cache eagerly (
SdfTextRenderer.ts) — a real renderer bugThe SDF layout cache only evicted on idle (
cleanup, by design "so eviction never competes with rendering"). But a continuously animating scene never goes idle, so any app with ever-changing SDF text — a clock, counter, score, or fps readout — grew the cache without bound. Now it's also bounded on insert: a singleMapdelete on a cache miss (only when new text is laid out), which doesn't compete with steady-state rendering. Idlecleanupis retained for bulk trimming. Updated the cache test to assert the eager-eviction behavior.Notes
Testing
pnpm build,pnpm lintclean;pnpm test314/314 (rewrote the SDF cache test for eager eviction).🤖 Generated with Claude Code