fix(ui): memoise the a11y tree so the paint loop stops re-laying out every frame - #152
Merged
Conversation
…every frame Since #149 the app runs on go-widgets/window, whose accessibility bridge pulls Scene.A11yElements() on every paint frame, and run.go drives a 60 Hz repaint. Each pull rebuilt the whole accessibility tree, and building it runs a full layout pass — text measurement for every card included. At 60 fps that is a complete feed re-layout 60 times a second on the render thread, pinning a core and freezing the UI even while the app is idle (the spinning process's goroutine dump showed paintFrame -> A11yElements -> A11yTree -> layout -> wrapMeasured with no aggregation in flight). Cache the built tree against the scene's damage sequence (rev): a pull with no intervening state change returns the cached slice, and any change bumps rev and forces a rebuild on the next pull, so the tree a screen reader sees never goes stale. Measured on the ui benchmark scene: a cached pull is 1.0 ns/op with 0 allocs versus 9085 ns/op / 192 allocs to rebuild — and a real feed measures far more text per rebuild than this two-card scene. Co-Authored-By: Claude Opus 4.8 <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 freeze
Since #149 the reader runs on
go-widgets/window, whose accessibility bridge pullsScene.A11yElements()on every paint frame, andrun.godrives an unconditional 60 Hz repaint ticker. Each pull rebuilt the entire accessibility tree, and building it runs a fullScene.layout()pass — text measurement (wrapMeasured) for every card.At 60 fps that is a complete feed re-layout 60×/sec on the render thread → one core pinned at 100 %+ → the UI freezes even while idle. A goroutine dump of the spinning process showed the hot loop:
with no aggregation goroutines in flight — i.e. the spin was the idle a11y pull, not a hung fetch.
The fix
Memoise the built tree against the scene's damage sequence (
rev): a pull with no intervening change returns the cached slice; any state change bumpsrevand forces a rebuild on the next pull, so a screen reader never sees a stale tree.Measured
uibenchmark scene:~8,700× per pull — and a real feed measures far more text per rebuild than this two-card scene.
uiretained (TestA11yTreeCachesUntilAChangecovers hit + invalidation; benchmarks exercise both paths)gofmtclean, full build green🤖 Generated with Claude Code