Repository navigation
perf(tui): repaint the streaming message without re-arranging the transcript - #871
Closed
leonsong09 wants to merge 1 commit into
Closed
leonsong09 wants to merge 1 commit into
leonsong09 wants to merge 1 commit into
Conversation
…nscript
`Static.update()` defaults to `layout=True`, so every 50ms streaming paint
marked the whole screen layout-dirty and made Textual re-arrange every mounted
message on the next frame. On a long session that is 5-15ms per token chunk
(and ~400ms spikes), which shows up as an unresponsive composer and text that
only appears in bursts.
Paint only what changed:
* `_rendered_height()` / `_update_text()` compare the height the new content
would occupy and request a layout pass only when it differs. Same-line
growth (the common case while tokens stream) becomes a repaint, not a reflow.
New lines (.e.g a new paragraph) still grow the widget, so nothing is clipped.
* the spinner and the post-turn annotation go through the same helper - they are
single-line and never need a layout.
* drop the redundant `#messages.refresh()`; the widget update already marks the
changed region dirty.
* the streaming paint coalescing window becomes `STREAM_PAINT_INTERVAL`
(default 100ms, `GA_TUI_PAINT_MS` to override) instead of a hard-coded 50ms.
Measured on a 30-turn transcript (headless, real widgets): 37 -> 3 layout
passes per 2s of 20Hz streaming, 466ms -> 18ms of main-thread time. Live
profile before the change: 29% of the main thread inside
`_refresh_layout -> reflow` ("py-spy record" on a running TUI).
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.
问题
TUI 在长会话里发卡、
🤔 思考中期间字成段地蹦出来。线上进程用
py-spy record 12s采到的事实:主线程忙 3.46s / 12s(29%),其中 **2.41s(70%)**全在即「重排版」,不是渲染内容本身。触发频率约 11.6 次/秒,偶发 400ms+ 的整屏重排。
根因
Static.update()默认layout=True(textual 8.2.5 源码),而流式渲染每 50ms 调一次:于是每来一小段 token,整棵消息区就被标成 layout-dirty,下一帧全量 arrange —— 开销随已挂载消息数线性增长,所以对话越长越卡。
改动
_rendered_height()/_update_text():先算新内容在该宽度下占几行,只有高度变化才请求 layout。同一段里追加字(流式最常见情况)变成重绘,换行/新段仍会撑高控件,不会裁行。#messages.refresh()(控件 update 已标脏)。STREAM_PAINT_INTERVAL(默认 100ms,GA_TUI_PAINT_MS可覆盖)。实测
headless、真实 TUI 组件、30 轮对话、20Hz 流式 2 秒:
spinner 单测:20 次 → 3 次;
update(layout=False)全局对照:26 次 → 0 次。回归
新增
frontends/tests/test_tuiapp_v2_stream_layout.py,钉两头:height == 9,最后一行line 08可见)——防裁行。frontends/tests全量:273 passed,3 个既有失败(test_data_backupsymlink、test_release_qualification缺文件)与本次无关,stash 后同样失败。