Repository navigation
perf(tui): reuse unchanged tool output lines across renders - #4665
Conversation
🦋 Changeset detectedLatest commit: ad67a9a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4a7cbd6f50
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
4a7cbd6 to
98c9a34
Compare
98c9a34 to
42e89cc
Compare
42e89cc to
74d4962
Compare
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues for the tool-output rendering cache changes.
No open issues 🎉
What changed in this PR
本 PR 为 TUI 的工具输出渲染增加缓存,减少大量内容展开后的重复计算,缓解 #4618 引入分组后更明显的输入和滚动延迟,不改变渲染内容。
Changes:
- 复用工具卡片空行、Shell 输出、截断输出和 Markdown 的未变化行数组,并缓存折叠输出的截断判断。
- 缓存遵守现有渲染缓存开关,按宽度、内容或组件失效状态更新。
- 补充缓存复用、失效和禁用场景的测试,并添加 patch changeset。
| File | Description |
|---|---|
apps/kimi-code/test/tui/components/messages/tool-renderers/truncated.test.ts |
验证截断输出缓存及失效行为。 |
apps/kimi-code/test/tui/components/messages/tool-call.test.ts |
验证卡片缓存、结果更新和截断判断。 |
apps/kimi-code/test/tui/components/messages/shell-execution.test.ts |
验证 Shell 缓存与原渲染结果一致。 |
apps/kimi-code/test/tui/components/markdown/markdown.test.ts |
验证 Markdown 缓存及文本、宽度和主题更新。 |
apps/kimi-code/src/tui/components/messages/tool-renderers/truncated.ts |
缓存折叠后的截断输出。 |
apps/kimi-code/src/tui/components/messages/tool-call.ts |
增加稳定空行和截断判断缓存。 |
apps/kimi-code/src/tui/components/messages/shell-execution.ts |
复用未变化的子组件合并结果。 |
apps/kimi-code/src/tui/components/markdown/markdown.ts |
缓存 Markdown 子组件合并结果。 |
.changeset/expanded-tool-output-render.md |
记录展开大量工具输出时的响应性能改进。 |
Review effort: Balanced
The merge-base changed after approval.
74d4962 to
ad67a9a
Compare
Port upstream MoonshotAI#4665. Cache rendered lines in Markdown, ShellExecution, TruncatedOutput, and a StableSpacer for the tool card so unchanged output is not re-wrapped on every frame. Gate the existing collapsedOutcomeClips memo behind isRenderCacheEnabled to match upstream so disabling the cache re-measures each render. Output is identical when the cache is enabled.
Requirement or Bug
展开大量工具输出以后(按 ctrl+o,或在分组里点开很多步),界面每次刷新都会把一部分没有变化的工具输出重新算一遍。展开得越多,打字、滚动和动画就越慢。这个问题 main 上就有。fullscreen 默认的 30 步合并让展开的内容少一些,所以平时不明显;#4618 开启分组后不再合并,问题就明显了。这个 PR 让这些部分在内容和宽度不变时直接复用上一次的结果,画面不变。
本 PR 堆叠在 #4618 上,只需要看最后 6 个提交。#4618 合入后会 rebase 到 main。
Bug Reproduction Steps
在 #4618 的分支上,这一步约 18 毫秒;main 默认(有 30 步合并)约 5 毫秒。数据见下面的“性能”。
Root Cause
父组件按“子组件这次返回的行数组是不是上次那一个”判断能不能复用自己的缓存,例如工具卡片和工具输出里的
ToolOutputMarkdown。下面四处每次刷新都返回新数组,或者重新计算,让上层的缓存永远失效:Spacer.render每次返回一个新数组。collapsedOutcomeClips)每次刷新都重算一遍,TruncatedOutputComponent也没有缓存。这是根本修复:每一处在输入不变时返回同一个数组,上层原有的缓存就能命中。
Code Changes
四处用同一个办法:记下上次的宽度和每个子组件返回的行数组,这次都没变就返回上次拼好的结果。
StableSpacer)invalidate(),或任一子组件的输出变了rebuildContent、rebuildBody)TruncatedOutputComponentinvalidate(),或任一子组件的输出变了主题变化时,上层会调用
invalidate(),缓存跟着清掉,所以颜色变化不会被旧结果挡住。五处缓存都遵守现有的渲染缓存开关(
KIMI_TUI_NO_RENDER_CACHE=1或setRenderCacheEnabled(false)):开关关掉时既不读也不写缓存,和仓库里其他缓存一致,排查问题和对比有无缓存的性能时仍能整体关掉。一个取舍。 Shell 输出组件和 Markdown 不再走 pi-tui
Container.render,所以不再记录鼠标命中用的布局。点击落在它们上面时,pi-tui 会退回到逐个渲染子组件来算高度。结果正确,而且子组件都有缓存,这一步很便宜。阅读指南。 前四个提交各改一处缓存,第五个是 changeset,第六个让这些缓存遵守渲染缓存开关。按提交顺序看即可。测试都加在对应组件现有的测试文件里。
Behavior Changes and Affected Users
None:渲染出的文字和颜色不变,只省掉重复计算。
证据:
性能。 低速模式(macOS 后台优先级
taskpolicy -c background,模拟较慢的机器),每组 2 次,数值是每次打字回显的中位数:受影响模块。 只改
apps/kimi-code/src/tui/components/下的四个文件:messages/tool-call.ts、messages/shell-execution.ts、messages/tool-renderers/truncated.ts、markdown/markdown.ts。对应测试在apps/kimi-code/test/tui/components/下同名的测试文件里。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.