fix(session-ui): reuse cached diff highlighting across remounts (#46769)

This commit is contained in:
Luke Parker
2026-09-02 19:24:35 +10:00
committed by GitHub
Unverified
parent 91a4c7bc32
commit e3c2e635a9
10 changed files with 362 additions and 1 deletions
@@ -0,0 +1,39 @@
# Diff Highlighting
Manual Chromium benchmark of the production `File` component, `normalize`, Pierre
pool, and bundled workers. No OpenCode server, session, or stored data is used.
From `packages/session-ui`, set `HIGHLIGHT_BUNDLE` to an external artifact
directory, then run:
```sh
bun performance/highlighting/build.ts
bun x playwright test --config performance/highlighting/playwright.config.ts --repeat-each 20
```
Each isolated page measures a cold mount, unmount/remount of the identical patch,
and an edited patch under the same filename. The fixtures contain 120 or 1,200
TypeScript route handlers with one changed line per ten handlers. Exact byte and
line counts, bundle hash, source revision, and individual samples are reported
through the existing performance reporter.
`readyMs` ends when the viewer has called `onRendered`, its pool has settled,
and `onPostRender` has committed the final worker result containing the expected
edit. `firstReadyMs` records the earlier production callback, which can represent
plain first paint. Worker request counts and round-trip time describe the
mechanism, not CPU time or desktop memory. Full reconstructed content is checked
separately after timing. No forced GC or machine-dependent thresholds are used.
`HIGHLIGHT_CORRECTNESS=1` runs the deterministic `*.correctness.ts` checks against
the same fixture instead of the timed benchmarks: cache reuse across remounts and
invalidation on content, theme, and worker option changes.
Optional output settings: `HIGHLIGHT_RESULTS`, `HIGHLIGHT_SCREENSHOTS`,
`OPENCODE_PERFORMANCE_RUN_ID`, and `OPENCODE_PERFORMANCE_TRACE_DIR` (separate
diagnostic runs, not clean timing). `HIGHLIGHT_PORT` defaults to 4793. Preserve the
baseline build before product edits, then point `HIGHLIGHT_BUNDLE` at either frozen
build to compare the same workload without rebuilding.
`HIGHLIGHT_RETENTION=1` enables a separate post-unmount, forced-GC renderer-isolate
heap diagnostic. Do not use that run for timing or describe it as desktop RAM;
it excludes worker heaps and native/browser-process memory.
@@ -0,0 +1,28 @@
import { build } from "vite"
import solid from "vite-plugin-solid"
import path from "node:path"
import { realpathSync } from "node:fs"
import { createHash } from "node:crypto"
const outDir = process.env.HIGHLIGHT_BUNDLE
if (!outDir) throw new Error("Set HIGHLIGHT_BUNDLE to an external artifact directory")
const ui = realpathSync(path.resolve(import.meta.dir, "../../node_modules/@opencode-ai/ui"))
if (ui !== realpathSync(path.resolve(import.meta.dir, "../../../ui"))) throw new Error(`Wrong workspace source: ${ui}`)
const util = realpathSync(path.resolve(import.meta.dir, "../../node_modules/@opencode-ai/util"))
if (util !== realpathSync(path.resolve(import.meta.dir, "../../../util"))) throw new Error(`Wrong workspace source: ${util}`)
await build({
configFile: false,
logLevel: "warn",
root: import.meta.dir,
plugins: [solid()],
build: { outDir, emptyOutDir: true, sourcemap: true },
worker: { format: "es" },
})
const files = await Array.fromAsync(new Bun.Glob("**/*").scan({ cwd: outDir, onlyFiles: true }))
const hash = createHash("sha256")
for (const file of files.sort()) hash.update(file).update(new Uint8Array(await Bun.file(path.join(outDir, file)).arrayBuffer()))
await Bun.write(path.join(outDir, "build.json"), JSON.stringify({
revision: Bun.spawnSync(["git", "rev-parse", "HEAD"]).stdout.toString().trim(),
sourceDiff: Bun.spawnSync(["git", "diff", "--", "src/components/session-diff.ts", "src/components/file.tsx", "src/pierre/worker.ts"]).stdout.toString(),
bundle: hash.digest("hex"), bun: Bun.version, ui, util,
}, null, 2))
@@ -0,0 +1,35 @@
import { expect, test } from "@playwright/test"
import type {} from "./viewer"
test("reuses only matching patch, theme, and worker options", async ({ page }) => {
await page.goto("/")
await page.waitForFunction(() => !!window.highlighting)
const cold = await page.evaluate(() => window.highlighting.mount(1))
expect(cold.workerRequests).toBe(1)
expect(cold.cacheSize).toBe(1)
const first = await page.locator('[data-line="11"][data-line-type="change-addition"]').innerHTML()
await page.evaluate(() => window.highlighting.unmount())
const cached = await page.evaluate(() => window.highlighting.mount(1))
expect(cached.workerRequests).toBe(0)
expect(await page.locator('[data-line="11"][data-line-type="change-addition"]').innerHTML()).toBe(first)
await page.evaluate(() => window.highlighting.unmount())
const changed = await page.evaluate(() => window.highlighting.mount(2))
expect(changed.workerRequests).toBe(1)
await expect(page.locator('[data-line="11"][data-line-type="change-addition"]')).toContainText("status: 202")
await page.evaluate(() => window.highlighting.unmount())
await page.evaluate(() => window.highlighting.configure({ theme: "github-dark" }))
const themed = await page.evaluate(() => window.highlighting.mount(2))
expect(themed.workerRequests).toBe(1)
expect(themed.options.theme).toBe("github-dark")
expect(themed.cacheSize).toBe(1)
await page.evaluate(() => window.highlighting.unmount())
await page.evaluate(() => window.highlighting.configure({ tokenizeMaxLineLength: 1 }))
const plain = await page.evaluate(() => window.highlighting.mount(2))
expect(plain.workerRequests).toBe(1)
expect(plain.options.tokenizeMaxLineLength).toBe(1)
expect(await page.evaluate(() => window.highlighting.contents()!.after === window.highlighting.input.after[1])).toBe(true)
})
@@ -0,0 +1,5 @@
<!doctype html>
<html lang="en" data-color-scheme="light">
<head><meta charset="UTF-8" /><title>Diff highlighting benchmark</title></head>
<body><div id="root"></div><script type="module" src="./viewer.tsx"></script></body>
</html>
@@ -0,0 +1,13 @@
import { defineConfig } from "@playwright/test"
export default defineConfig({
testDir: ".", testMatch: process.env.HIGHLIGHT_CORRECTNESS ? "*.correctness.ts" : "*.bench.ts", workers: 1, retries: 0, maxFailures: 1, timeout: 120_000,
outputDir: process.env.HIGHLIGHT_RESULTS,
reporter: [["line"]],
use: { baseURL: `http://127.0.0.1:${process.env.HIGHLIGHT_PORT ?? 4793}`, viewport: { width: 1280, height: 800 } },
webServer: {
command: "bun serve.ts",
url: `http://127.0.0.1:${process.env.HIGHLIGHT_PORT ?? 4793}`,
reuseExistingServer: false,
},
})
@@ -0,0 +1,11 @@
import path from "node:path"
const root = process.env.HIGHLIGHT_BUNDLE
if (!root) throw new Error("Set HIGHLIGHT_BUNDLE")
Bun.serve({
hostname: "127.0.0.1", port: Number(process.env.HIGHLIGHT_PORT ?? 4793),
fetch(request) {
const pathname = new URL(request.url).pathname
return new Response(Bun.file(path.join(root, pathname === "/" ? "index.html" : pathname)))
},
})
@@ -0,0 +1,41 @@
import { benchmark, expect } from "../../../app/e2e/performance/benchmark"
import { readFile } from "node:fs/promises"
import path from "node:path"
import type {} from "./viewer"
for (const large of [false, true]) {
benchmark(large ? "large file" : "normalized diff remount", async ({ page, browser, report }, info) => {
const errors: string[] = []
page.on("pageerror", (error) => errors.push(error.message))
await page.goto(large ? "/?large" : "/")
await page.waitForFunction(() => !!window.highlighting)
const cold = await page.evaluate(() => window.highlighting.mount(1))
await expect(page.locator('[data-line="11"][data-line-type="change-addition"]')).toContainText("status: 201")
await page.evaluate(() => window.highlighting.unmount())
const remount = await page.evaluate(() => window.highlighting.mount(1))
await page.evaluate(() => window.highlighting.unmount())
const changed = await page.evaluate(() => window.highlighting.mount(2))
await expect(page.locator('[data-line="11"][data-line-type="change-addition"]')).toContainText("status: 202")
const complete = await page.evaluate(() => {
const contents = window.highlighting.contents()!
return contents.before === window.highlighting.input.before && contents.after === window.highlighting.input.after[1]
})
expect(complete).toBe(true)
expect(errors).toEqual([])
const retention = await (async () => {
if (!process.env.HIGHLIGHT_RETENTION) return
await page.evaluate(() => window.highlighting.unmount())
const session = await page.context().newCDPSession(page)
await session.send("HeapProfiler.collectGarbage")
const heap = await session.send("Runtime.getHeapUsage")
await session.detach()
return heap
})()
report({ cold, remount, changed, retention }, {
browser: browser.version(),
dimensions: await page.evaluate(() => window.highlighting.dimensions),
build: JSON.parse(await readFile(path.join(process.env.HIGHLIGHT_BUNDLE!, "build.json"), "utf8")),
})
if (process.env.HIGHLIGHT_SCREENSHOTS) await page.screenshot({ path: path.join(process.env.HIGHLIGHT_SCREENSHOTS, `${large ? "large" : "diff"}-${info.repeatEachIndex}.png`) })
})
}
@@ -0,0 +1,145 @@
import { render } from "solid-js/web"
import { createPatch } from "diff"
import type { WorkerRequest, WorkerResponse, WorkerRenderingOptions } from "@pierre/diffs/worker"
import type { RenderDiffOptions } from "@pierre/diffs"
import { File } from "../../src/components/file"
import { normalize, text } from "../../src/components/session-diff"
import { getWorkerPool } from "../../src/pierre/worker"
import "../../../ui/src/styles/theme.css"
// This fixture uses the production component, normalizer, pool, and worker bundle.
// Observe messages without replacing the worker or its implementation.
const messages: { type: string; at: number; end?: number }[] = []
const listening = new WeakSet<Worker>()
const pending = new Map<string, (typeof messages)[number]>()
const postMessage = Worker.prototype.postMessage
Worker.prototype.postMessage = function (message: WorkerRequest, options?: Transferable[] | StructuredSerializeOptions) {
if (!listening.has(this)) {
listening.add(this)
this.addEventListener("message", (event: MessageEvent<WorkerResponse>) => {
const item = pending.get(event.data.id)
if (item) item.end = performance.now()
pending.delete(event.data.id)
}, { capture: true })
}
const item = { type: message.type, at: performance.now() }
messages.push(item)
pending.set(message.id, item)
postMessage.call(this, message, Array.isArray(options) ? { transfer: options } : options)
}
function source(count: number, revision: number) {
return Array.from({ length: count }, (_, index) => {
const status = index % 10 === 0 ? 200 + revision : 200
return `export async function route${index}(request: Request, context: RouteContext) {
const account = await context.accounts.find(request.headers.get("account-id"))
if (!account) return new Response("Account not found", { status: 404 })
const payload = await request.json()
const record = await context.records.save({
accountId: account.id,
category: "route-${index}",
title: payload.title.trim(),
enabled: payload.enabled ?? true,
})
return Response.json({ id: record.id, status: ${status} })
}
`
}).join("\n")
}
const large = new URLSearchParams(location.search).has("large")
const before = source(large ? 1200 : 120, 0)
const inputs = [1, 2].map((revision) => {
const after = source(large ? 1200 : 120, revision)
return { file: "routes.ts", patch: createPatch("routes.ts", before, after, "", "", { context: Infinity }), after }
})
const host = document.getElementById("root")!
let dispose: VoidFunction | undefined
let active: ReturnType<typeof normalize> | undefined
document.head.insertAdjacentHTML("beforeend", `<style>
:root { color-scheme: light; --font-family-mono: monospace; --font-size-small: 13px;
--color-background-stronger: white; --v2-background-bg-accent: #007acc; --v2-text-text-accent: #005a9e; }
body { margin: 16px; } #root { height: 760px; overflow: auto; overflow-anchor: none; }
[data-slot="file-header"] { height: 40px; line-height: 40px; font: 13px system-ui; padding: 0 12px; }
</style>`)
type Measurement = {
readyMs: number
firstReadyMs: number
workerRequests: number
workerRoundTripMs: number
cacheSize: number
syntaxSpans: number
options: RenderDiffOptions
}
async function mount(revision: number) {
if (dispose) throw new Error("Unmount the previous viewer first")
const input = inputs[revision - 1]
const offset = messages.length
const start = performance.now()
active = normalize({ ...input, additions: large ? 120 : 12, deletions: large ? 120 : 12 })
const pool = getWorkerPool(large ? "none" : "word-alt")!
let firstReady = 0
let rendered = 0
let finish!: (value: Measurement) => void
const result = new Promise<Measurement>((resolve) => (finish = resolve))
const check = () => {
const stats = pool.getStats()
if (!firstReady || !rendered || stats.managerState !== "initialized" || stats.activeTasks || stats.queuedTasks || stats.busyWorkers) return
const work = messages.slice(offset).filter((item) => item.type === "diff")
if (work.some((item) => !item.end || rendered < item.end)) return
const root = host.querySelector("diffs-container")?.shadowRoot
const edit = root?.querySelector('[data-line="11"][data-line-type="change-addition"]')
if (!root || !edit?.textContent?.includes(`status: ${200 + revision}`)) return
const range = document.createRange()
range.selectNodeContents(edit)
const bounds = range.getBoundingClientRect()
if (bounds.top < host.getBoundingClientRect().top || bounds.bottom > host.getBoundingClientRect().bottom) return
finish({
readyMs: performance.now() - start,
firstReadyMs: firstReady - start,
workerRequests: work.length,
workerRoundTripMs: work.reduce((sum, item) => sum + item.end! - item.at, 0),
cacheSize: stats.diffCacheSize,
syntaxSpans: root.querySelectorAll('[data-line] [style*="--syntax-"]').length,
options: pool.getDiffRenderOptions(),
})
}
const unsubscribe = pool.subscribeToStatChanges(check)
// Production surfaces place a header or earlier content above the diff inside a `[role="log"]` scroll content
// element. An empty diff element at scroll offset 0 makes Pierre's virtualizer anchor its bottom edge and scroll
// the host by the full content height once the first rows render.
dispose = render(() => <div role="log">
<div data-slot="file-header">{input.file}</div>
<File mode="diff" fileDiff={active!.fileDiff}
onRendered={() => { firstReady ||= performance.now(); check() }}
onPostRender={(_, __, phase) => {
if (phase === "unmount") return
rendered = performance.now()
queueMicrotask(check)
}} />
</div>, host)
const value = await result
unsubscribe()
return value
}
export const highlighting = {
mount,
configure(options: Partial<WorkerRenderingOptions>) { return getWorkerPool(large ? "none" : "word-alt")!.setRenderOptions(options) },
unmount() { dispose?.(); dispose = undefined; host.scrollTop = 0 },
contents() { return active && { before: text(active, "deletions"), after: text(active, "additions") } },
input: { before, after: inputs.map((input) => input.after) },
dimensions: {
functions: large ? 1200 : 120,
beforeBytes: new TextEncoder().encode(before).length,
afterBytes: new TextEncoder().encode(inputs[0].after).length,
patchBytes: new TextEncoder().encode(inputs[0].patch).length,
lines: before.split("\n").length - 1,
},
}
declare global { interface Window { highlighting: typeof highlighting } }
window.highlighting = highlighting
@@ -127,6 +127,38 @@ describe("session diff", () => {
expect(resolveFileDiff({ file: "b.ts", patch }).name).toBe("b.ts")
})
test.each([
"@@ -1 +1 @@\n-old\n+new\n",
"--- a.ts\t\n+++ a.ts\t\n@@ -1 +1 @@\n-old\n+new\n",
])("reuses a highlight identity for the same cached patch: %s", (patch) => {
const first = resolveFileDiff({ file: "a.ts", patch })
expect(first.cacheKey).toBeString()
expect(resolveFileDiff({ file: "a.ts", patch }).cacheKey).toBe(first.cacheKey)
expect(resolveFileDiff({ file: "b.ts", patch }).cacheKey).not.toBe(first.cacheKey)
expect(resolveFileDiff({ file: "a.ts", patch: patch.replace("+new", "+next") }).cacheKey).not.toBe(first.cacheKey)
})
test("keys preloaded content diffs by file name and content", () => {
const diff = { file: "a.ts", before: "one\n", after: "two\n", additions: 1, deletions: 1 }
const first = normalize(diff).fileDiff
expect(first.cacheKey).toBeString()
expect(normalize(diff).fileDiff.cacheKey).toBe(first.cacheKey)
expect(normalize({ ...diff, file: "a.py" }).fileDiff.cacheKey).not.toBe(first.cacheKey)
expect(normalize({ ...diff, after: "three\n" }).fileDiff.cacheKey).not.toBe(first.cacheKey)
})
test("does not reuse an evicted highlight identity for different content", () => {
const patch = "@@ -1 +1 @@\n-old\n+new\n"
const first = resolveFileDiff({ file: "evicted.ts", patch })
const keys = Array.from({ length: 20 }, (_, index) =>
resolveFileDiff({ file: "evicted.ts", patch: patch.replace("+new", `+new${index}`) }).cacheKey,
)
expect(keys).not.toContain(first.cacheKey)
const restored = resolveFileDiff({ file: "evicted.ts", patch })
expect(restored.additionLines).toEqual(first.additionLines)
expect(restored.cacheKey).toBeString()
})
test("keeps capped header-only patches partial", () => {
const fileDiff = resolveFileDiff({
file: "a.ts",
@@ -1,5 +1,6 @@
import { parseDiffFromFile, parsePatchFiles, type FileDiffMetadata } from "@pierre/diffs"
import { parsePatch } from "diff"
import { checksum } from "@opencode-ai/util/encode"
import type { FileDiffInfo } from "@opencode-ai/client/promise"
import type { PresentationFileDiff } from "../file-presentation"
@@ -65,6 +66,8 @@ function fileDiffFromPatch(file: string, patch: string) {
const value = contents
? fileDiffFromContent(file, contents.before, contents.after)
: ((input ? parsePatchFiles(input)[0]?.files[0] : undefined) ?? emptyFileDiff(file))
// Complete patches already carry a content key from fileDiffFromContent; partial patches are keyed by the patch text.
value.cacheKey ??= highlightKey(key)
patchFileDiffCache.set(key, value)
while (patchFileDiffCache.size > diffCacheLimit) patchFileDiffCache.delete(patchFileDiffCache.keys().next().value!)
return value
@@ -136,7 +139,16 @@ function patchInput(file: string, patch: string) {
function fileDiffFromContent(file: string, before: string, after: string) {
if (!before && !after) return emptyFileDiff(file)
return parseDiffFromFile({ name: file, contents: before }, { name: file, contents: after })
const value = parseDiffFromFile({ name: file, contents: before }, { name: file, contents: after })
value.cacheKey = highlightKey(`${file}\0${before}\0${after}`)
return value
}
// Pierre reuses and dedups worker highlighting only for diffs that carry a cacheKey, and it treats diffs with equal
// keys as the same target. Derive the key from every input that shapes the highlighted output: the file name selects
// the language and the content selects the lines.
function highlightKey(value: string) {
return `${value.length}:${checksum(value) ?? 0}`
}
function emptyFileDiff(file: string) {