Repository navigation
perf(tui): repaint sidebar only when its rendered content changes - #870
Closed
leonsong09 wants to merge 1 commit into
Closed
leonsong09 wants to merge 1 commit into
leonsong09 wants to merge 1 commit into
Conversation
`_refresh_sidebar()` is called from `_flush_stream_renders()` on every 50ms paint batch during a stream, so a busy session rebuilds the session table — and re-derives each session's Q/S previews by walking and regexing the LLM history — 20x/s, even though none of it changes while tokens are arriving. Gate the repaint on a signature of what `render_sidebar()` actually draws (session id, name, status, Q/S previews): * streaming: the history is only appended at turn boundaries, so the signature is stable and the 20x/s calls collapse into a tuple comparison; * task completion (running -> idle), a new prompt or a fresh summary flips the signature and repaints on that frame — no timer, no polling; * Q/S derivation is memoised per session on (history identity, length, last message) so the preview text is not re-scanned on every call. `force=True` is kept for callers that know the view must be rebuilt (switch/rename/rewind/theme). Measured on a 1500-turn session with no <summary> in history (4 sessions): 9.4ms per repaint before, ~0 after (only the signature compare runs). Co-Authored-By: GenericAgent <bot@gaagent.ai>
Author
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
_refresh_sidebar()infrontends/tuiapp_v2.pynow repaints only when the sidebar's visible content actually changes, instead of on every call.A sidebar row is fully determined by
(sid, name, status, last user message, last summary). This PR computes a cheap signature of exactly those fields and compares it against the signature of the last painted frame;_paint_sidebar()runs only when it differs.This is not a bug fix — the sidebar already updates promptly today. It is purely about not doing that work when nothing changed.
Why
_flush_stream_renders()batches streamed output every 50 ms and calls_refresh_sidebar()whenever the batch caused a render — i.e. 20 times per second while a reply streams. Each call:<summary>.During streaming neither of those can change (history is only appended when a turn completes), so all of it is wasted — and the cost grows with conversation length.
Measured on
render_sidebar()alone (4 sessions, ties excluded, best-of-N):<summary>(backward scan hits immediately)<summary>in historyThe 0.12 ms row is the lucky path — the backward scan finds a summary in the first message it looks at. Once a session has no
<summary>anywhere (older histories, sessions written by another client, history whose summaries were trimmed), every repaint rescans the entire history: ~19% of one core, continuously, on the UI thread, while streaming.render_sidebar()only — Textual's own layout/render cost on top of that is not included.After the change, the same 20 Hz path costs 0.008 ms per call (4 sessions × 1500 turns, signature only, no table build, no regex) — 0.16 ms/s instead of 188.6 ms/s, ~1180× less.
Behaviour
Verified through the real display-queue entry point (
_consume_display_queue→_on_stream(done=True)) in a Textualrun_testapp:So: nothing while a reply streams, immediately when the turn ends / a message is added / a status flips.
Compatibility
_refresh_sidebar(force: bool = False)— the parameter is optional; all 8 existing call sites are unchanged._sidebar_last_user/_sidebar_last_summaryreuse the previous frame's result when the underlying history object, its length and its last message are unchanged, so even the signature check avoids repeated regex work.Tests
frontends/tests/test_tuiapp_v2_sidebar_refresh.py(4 tests):_refresh_sidebar()calls produce no repaintforce=True→ always repaintspytest frontends/tests/test_tuiapp_v2_sidebar_refresh.py→ 4 passed.Scope
frontends/tuiapp_v2.py(+68/−1) and one new test file. No other files touched.Happy to drop this if you'd rather keep the sidebar path as simple as it is — it's a cpu/abstraction tradeoff, not a correctness argument.