perf(tui): cache file-tree flatten/filter/sort between redraws - #94
Merged
Conversation
The file tree recomputed its full flatten->filter->sort pipeline on every call, and the render loop calls it several times per keystroke (cursor bounds, clamp, status bar, and both render passes in split mode). On a large image that redundant per-frame tree walk is the main source of held-key navigation lag. Memoize the result per pane behind a pointer field on the model, keyed on the pipeline inputs (selected layer, filter query, diff-only, sort mode) plus a generation counter for collapse state. A pointer is required because the method has a value receiver and the model is copied by value on every update, so a value field would be written to a throwaway copy. The output is unchanged; only the redundant work is removed.
The displayTreeFor memoization keyed on layerCursor, filters, sort, and collapse state but not on the analysis itself. If m.analysis were ever replaced while those keys stayed put, a warm cache slot would return the previous analysis's file nodes. Bump an analysisGen counter when m.analysis is assigned and include it in the cache key so a replaced analysis forces a recompute. Dormant today (analysis is set once) but removes the latent staleness trap.
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
The interactive file-tree browser recomputed its visible file list from scratch on every redraw — walking the entire file tree, then filtering and sorting it. A single keystroke drives that computation up to ~7 times per frame (cursor bounds, clamping, status bar, both render passes). On large images with deep trees this repeated work shows up as navigation lag.
This branch memoizes the flatten → filter → sort result per pane and reuses it until an input actually changes.
How it works
Impact
Testing
go build ./...andgo vet ./...clean.