Repository navigation
perf(tui): stop re-arranging and re-scanning per frame (supersedes #870 #871) - #881
Open
leonsong09 wants to merge 2 commits into
Open
leonsong09 wants to merge 2 commits into
leonsong09 wants to merge 2 commits into
Conversation
leonsong09
force-pushed
the
perf/tui-render-umbrella
branch
from
November 14, 2026 02:47
72d55af to
a41cc8d
Compare
This was referenced Nov 14, 2026
Closed
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.
Why one PR
Both changes sit in
frontends/tuiapp_v2.pyand both are about the same thing: the TUI does work per frame that cannot have changed since the last frame. They touch adjacent code, so reviewing them together is cheaper than reviewing them apart — and they were measured against the same 20 Hz streaming load.Merged cleanly, no conflicts.
pytest frontends/tests/test_tuiapp_v2_sidebar_refresh.py frontends/tests/test_tuiapp_v2_stream_layout.py -q→ 5 passed.1. Streaming repaints without re-arranging the transcript
Symptom. Long sessions: text arrives in lurches during
🤔 思考中, occasionally a 400 ms+ whole-screen reflow.Measured with
py-spy record 12son a live process — main thread busy 3.46s of 12s (29%), of which 2.41s (70%) was entirely:Not rendering. Re-layout. ~11.6 times/s.
Cause.
Static.update()defaults tolayout=True(textual 8.2.5), and the stream renderer calls it every 50 ms:So every token batch marks the whole message area layout-dirty, and the next frame arranges everything — cost linear in mounted messages, which is why it degrades with conversation length.
Fix.
_rendered_height()/_update_text(): compute how many lines the new content occupies at the current width and request layout only when the height changes. Appending within a line — the common streaming case — becomes a repaint. Wrapping and new paragraphs still grow the widget, so no line gets clipped.#messages.refresh()—update()already dirties.STREAM_PAINT_INTERVAL(default 100 ms,GA_TUI_PAINT_MSoverrides).Measured after — headless, real TUI widgets, 30 turns, 20 Hz stream for 2s:
Spinner unit case: 20 → 3. Global
update(layout=False)counter: 26 → 0.2. Sidebar repaints only when its content changes
_flush_stream_renders()calls_refresh_sidebar()on every batch that produced a render — 20 times/s while a reply streams. Each call rebuilds the whole Rich table and walks each session's LLM history backwards running a regex to recover the last user question and the last<summary>.During streaming neither can change: history is only appended when a turn completes. So all of it is wasted, and the cost grows with conversation length. On
render_sidebar()alone (4 sessions, best-of-N):<summary>(backward scan hits immediately)<summary>in historyA sidebar row is fully determined by
(sid, name, status, last user message, last summary). The fix computes a signature of exactly those fields and compares it against the last painted frame;_paint_sidebar()runs only when it differs.This is not a bug fix — the sidebar already updates promptly. It is purely not doing the work when nothing changed.
Tests
test_tuiapp_v2_stream_layout.pypins both directions: growth within a line must not request layout; content that grows a new line must grow the widget (height == 9, last lineline 08visible) — guards against clipping.test_tuiapp_v2_sidebar_refresh.pypins that a signature-identical refresh paints zero times, and that each of the five fields, changed alone, triggers exactly one paint.frontends/testsfull run: 273 passed, 3 pre-existing failures (test_data_backupsymlink,test_release_qualificationmissing file) — unrelated, they fail identically after stashing this branch.Scope
frontends/tuiapp_v2.py+135/-13, two new test files (342 lines). No agent-loop code, so this lands independently of #873/#872/#869/#876/#879/#874.