Forge changes view responsiveness
Forge 变更视图的响应速度
The forge changes view of a large change request froze the browser. Opening #7985 (366 files, 37,563 drawn lines) kept the main thread busy for about five seconds, "expand all" for fifteen, and every later state change, such as a poll result, a tool result, a file jump, or a keystroke in the file filter, re-rendered every file section. While the view was visible, the whole app slowed down: two subject-less `:has()` r
English
Problem
The forge changes view of a large change request froze the browser. Opening #7985 (366 files, 37,563 drawn lines) kept the main thread busy for about five seconds, "expand all" for fifteen, and every later state change, such as a poll result, a tool result, a file jump, or a keystroke in the file filter, re-rendered every file section. While the view was visible, the whole app slowed down: two subject-less :has() rules in ui-sidebar-files made each DOM change restyle the entire document, and the view's hundred thousand nodes made each restyle cost 170–260 ms, so a streaming Agent turn kept the main thread saturated.
Decision
Scoped :has() rules. The navigator's covering rules name their row through data-files-navigator-row, which both hosts of sidebar.files.navigator set, so a DOM change outside such a row schedules no document-wide invalidation.
Sections draw what changed and what the reader reaches. ChangesTab passes each memoized DiffFile values whose identity survives its renders: one handler object, one comment-actions object, and per-file strings and booleans. Tab visibility is a prop of every section, so a visibility change redraws them all; leaving the view measured +57 ms against reading it from a React context, which the client packages forbid business components to do. The scrollport's reach tracker travels in the handler object. An expanded file holds a 22 px row per drawn line until its section first comes within 1200 px of the scrollport, then draws its lines and keeps them. After a jump, the browser's scroll anchoring keeps the target in place while the sections above it draw.
Highlighting in the drawing render. A section highlights its lines in the same render that draws them, as before, so the drawn text is always the file's current text and a native selection keeps its text nodes.
Fewer serial reads. A panel state equal to the published one reaches no follower. changeRequestDiff reads the patch and .gitattributes together. Snapshots carry the base commit as baseSha; ForgeService.diff passes it to the provider, and forge-github starts the compare read while its GraphQL read confirms the head.
Pixel measurement. Whether a section is near the scrollport decides whether React renders its lines at all, which CSS cannot express: content-visibility only skips layout and paint of nodes that already exist. The client packages offer no injected measurement hook, so one IntersectionObserver on the scrollport measures it off the main thread.
Alternatives considered
content-visibility: auto alone cut style work as much as scoping the rules did, but kept the React render and Shiki tokenization of every expanded file at open, about 3 s of script, and its placeholder sizes would need estimates for every collapsed and expanded section.
Full virtualization that also unmounts far sections would bound the DOM after long scrolls, but a section holds a comment draft and a text selection, which unmounting loses.
Deferred idle-time highlighting drew plain text and applied tokens once the page was idle and the scrollport still. It kept fast scrolling smooth, but tokens carry their own text, so a file whose content changed at the same path showed its previous text until the next pass, and replacing the plain text nodes collapsed a selection made in the meantime. Against it, highlighting while drawing raised the blocking time of scrolling all of #7985 from 22 ms to 1,634 ms and its longest frame gap from 34 ms to 817 ms; other operations stayed within 0.1 s.
The CSS Custom Highlight API would paint token colors over unchanged text nodes and keep deferral without either defect, but it cannot apply the themes' bold and italic, and its cost over tens of thousands of ranges is unmeasured.
Re-aligning a jumped-to section every frame for a second kept the last file flush with the end of the list, but it also pulled back any scroll it did not start, such as one from find in page; scroll anchoring already holds every other target at the top.
Highlighting in a Web Worker removes tokenization from the main thread entirely, including a heavy grammar's first use, but the shared highlighter in ui-primitives owns Shiki; moving it is a separate change for every consumer.
A provider-private cache of base commits would avoid the optional baseSha field, but it adds retained state without an owner that knows when a change request stops being watched.
Consequences
Opening #7985 now takes 0.5 s of main-thread work instead of 5.2 s, and file jumps, filtering, polling, and Agent turns no longer produce long tasks. In exchange, the browser's find in page does not see lines of files the reader has not approached, and scrolling onto files in a slow grammar's language, such as C++, highlights them during the scroll, up to about 0.8 s of main-thread work when that grammar is first used. A diff read costs a second compare request when the base commit changed after the latest status.
中文
Problem
大型变更请求的 forge 变更视图会让浏览器卡住。打开 #7985(366 个文件、37,563 个绘制行)时主线程忙约 5 秒,“全部展开”约 15 秒;之后的每次状态变化,例如轮询结果、工具结果、跳转文件或在文件筛选框中按键,都会重渲染所有文件段。视图可见时整个应用都变慢:ui-sidebar-files 中两条没有主体的 :has() 规则让每次 DOM 变化都重算整个文档的样式,而视图的十万个节点让每次重算耗时 170–260 ms,所以 Agent 流式输出的回合会让主线程一直饱和。
Decision
限定 :has() 规则。 导航器的遮盖规则通过 data-files-navigator-row 指定所在行,sidebar.files.navigator 的两个宿主都会设置它,因此这类行之外的 DOM 变化不再触发全文档失效。
文件段只绘制变化的部分和读者接近的部分。 ChangesTab 传给每个 memo 化的 DiffFile 的值在多次渲染之间保持同一标识:一个处理函数对象、一个评论操作对象,以及每个文件的字符串和布尔值。标签页可见性作为每个文件段的参数传入,可见性变化时全部文件段都会重绘;离开视图实测比从 React context 读取多 57 ms,而客户端包禁止业务组件读取 context。滚动区域的接近跟踪对象放在处理函数对象中传递。展开的文件在它的段第一次进入滚动区域 1200 px 范围之前,按每个绘制行 22 px 占位,之后绘制各行并一直保留。跳转后,上方文件段绘制时,浏览器的滚动锚定让目标保持原位。
在绘制的渲染中高亮。 文件段与修复前一样,在绘制各行的同一次渲染中完成高亮,所以画出的始终是文件当前的文本,原生选区所在的文本节点也不会被替换。
减少串行读取。 与已发布状态相同的面板状态不会通知任何关注方。changeRequestDiff 同时读取补丁和 .gitattributes。快照把基准提交作为 baseSha 携带;ForgeService.diff 把它传给提供方,forge-github 在 GraphQL 读取确认 head 的同时就开始 compare 读取。
像素测量。 文件段是否接近滚动区域,决定 React 是否渲染它的各行,这一点 CSS 无法表达:content-visibility 只能跳过已有节点的布局与绘制。客户端包没有可注入的测量 hook,所以在滚动区域上用一个 IntersectionObserver,在主线程之外测量。
Alternatives considered
只用 content-visibility: auto 减少的样式工作与限定规则相当,但打开时仍要对每个展开文件做 React 渲染和 Shiki 分词,约 3 秒脚本时间,而且每个折叠和展开的段都需要估计占位尺寸。
同时卸载远处文件段的完整虚拟化 可以在长距离滚动后限制 DOM 规模,但文件段持有评论草稿和文本选区,卸载会丢失它们。
空闲时延迟高亮 先画纯文本,等页面空闲且滚动区域静止后再应用 token。它让快速滚动保持流畅,但 token 自带文本,同一路径内容变化后会显示旧文本直到下一轮高亮完成;替换纯文本节点还会清掉期间做出的选区。与它相比,绘制时高亮让滚动整个 #7985 的阻塞时间从 22 ms 增到 1,634 ms,最长帧间隔从 34 ms 增到 817 ms;其他操作的差异在 0.1 秒以内。
CSS Custom Highlight API 可以在不变的文本节点上绘制 token 颜色,保留延迟高亮又没有上述两个问题,但它无法应用主题中的粗体和斜体,几万个范围的绘制成本也没有测过。
跳转后一秒内每帧重新对齐 能让最后一个文件贴紧列表末端,但它也会把不是它发起的滚动拉回去,例如页内查找引起的滚动;而对其他目标,滚动锚定已经能让它们保持在顶部。
在 Web Worker 中高亮 能把分词完全移出主线程,包括重型语法的首次使用,但 Shiki 由 ui-primitives 中的共享高亮器持有;迁移它是影响所有使用方的另一项改动。
由提供方私有缓存基准提交 可以省去可选的 baseSha 字段,但会引入保留状态,而且没有一个知道变更请求何时不再被 watch 的所有者。
Consequences
打开 #7985 的主线程工作从 5.2 秒降到 0.5 秒,跳转文件、筛选、轮询和 Agent 回合不再产生长任务。代价是:浏览器的页内查找看不到读者尚未接近的文件中的行;滚动到慢语法(例如 C++)的文件时会在滚动中完成高亮,该语法首次使用时最多约 0.8 秒的主线程工作。最新状态之后基准提交有变化时,一次 diff 读取会多一次 compare 请求。