From c0a35141e6b70eed1a9ba576fe43b7f7d690b968 Mon Sep 17 00:00:00 2001 From: Adam <2363879+adamdotdevin@users.noreply.github.com> Date: Tue, 2 Dec 2025 06:50:16 -0600 Subject: feat: better code and diff rendering performance --- packages/ui/src/components/code.tsx | 29 ++++++-- packages/ui/src/components/diff-ssr.tsx | 75 ++++++++++++++++++++ packages/ui/src/components/diff.tsx | 98 ++++++++------------------- packages/ui/src/components/message-part.tsx | 32 +++++++-- packages/ui/src/components/pierre.ts | 68 ------------------- packages/ui/src/components/session-review.tsx | 8 ++- packages/ui/src/components/session-turn.tsx | 31 +++++++-- packages/ui/src/pierre/index.ts | 77 +++++++++++++++++++++ packages/ui/src/pierre/worker.ts | 5 ++ 9 files changed, 265 insertions(+), 158 deletions(-) create mode 100644 packages/ui/src/components/diff-ssr.tsx delete mode 100644 packages/ui/src/components/pierre.ts create mode 100644 packages/ui/src/pierre/index.ts create mode 100644 packages/ui/src/pierre/worker.ts (limited to 'packages/ui/src') diff --git a/packages/ui/src/components/code.tsx b/packages/ui/src/components/code.tsx index 788baf549..b4b772816 100644 --- a/packages/ui/src/components/code.tsx +++ b/packages/ui/src/components/code.tsx @@ -1,6 +1,22 @@ import { type FileContents, File, FileOptions, LineAnnotation } from "@pierre/precision-diffs" import { ComponentProps, createEffect, splitProps } from "solid-js" -import { createDefaultOptions, styleVariables } from "./pierre" +import { createDefaultOptions, styleVariables } from "../pierre" +import { getOrCreateWorkerPoolSingleton } from "@pierre/precision-diffs/worker" +import { workerFactory } from "../pierre/worker" + +const workerPool = getOrCreateWorkerPoolSingleton({ + poolOptions: { + workerFactory, + // poolSize defaults to 8. More workers = more parallelism but + // also more memory. Too many can actually slow things down. + // poolSize: 8, + }, + highlighterOptions: { + theme: "OpenCode", + // Optionally preload languages to avoid lazy-loading delays + // langs: ["typescript", "javascript", "css", "html"], + }, +}) export type CodeProps = FileOptions & { file: FileContents @@ -14,10 +30,13 @@ export function Code(props: CodeProps) { const [local, others] = splitProps(props, ["file", "class", "classList", "annotations"]) createEffect(() => { - const instance = new File({ - ...createDefaultOptions("unified"), - ...others, - }) + const instance = new File( + { + ...createDefaultOptions("unified"), + ...others, + }, + workerPool, + ) container.innerHTML = "" instance.render({ diff --git a/packages/ui/src/components/diff-ssr.tsx b/packages/ui/src/components/diff-ssr.tsx new file mode 100644 index 000000000..800aa3730 --- /dev/null +++ b/packages/ui/src/components/diff-ssr.tsx @@ -0,0 +1,75 @@ +import { FileDiff } from "@pierre/precision-diffs" +import { PreloadMultiFileDiffResult } from "@pierre/precision-diffs/ssr" +import { onCleanup, onMount, Show, splitProps } from "solid-js" +import { isServer } from "solid-js/web" +import { createDefaultOptions, styleVariables, type DiffProps } from "../pierre" + +export type SSRDiffProps = DiffProps & { + preloadedDiff: PreloadMultiFileDiffResult +} + +export function Diff(props: SSRDiffProps) { + let container!: HTMLDivElement + let fileDiffRef!: HTMLElement + const [local, others] = splitProps(props, ["before", "after", "class", "classList", "annotations"]) + + let fileDiffInstance: FileDiff | undefined + const cleanupFunctions: Array<() => void> = [] + + onMount(() => { + if (isServer || !props.preloadedDiff) return + fileDiffInstance = new FileDiff({ + ...createDefaultOptions(props.diffStyle), + ...others, + ...props.preloadedDiff, + }) + // @ts-expect-error - fileContainer is private but needed for SSR hydration + fileDiffInstance.fileContainer = fileDiffRef + fileDiffInstance.hydrate({ + oldFile: local.before, + newFile: local.after, + lineAnnotations: local.annotations, + fileContainer: fileDiffRef, + containerWrapper: container, + }) + + // Hydrate annotation slots with interactive SolidJS components + // if (props.annotations.length > 0 && props.renderAnnotation != null) { + // for (const annotation of props.annotations) { + // const slotName = `annotation-${annotation.side}-${annotation.lineNumber}`; + // const slotElement = fileDiffRef.querySelector( + // `[slot="${slotName}"]` + // ) as HTMLElement; + // + // if (slotElement != null) { + // // Clear the static server-rendered content from the slot + // slotElement.innerHTML = ''; + // + // // Mount a fresh SolidJS component into this slot using render(). + // // This enables full SolidJS reactivity (signals, effects, etc.) + // const dispose = render( + // () => props.renderAnnotation!(annotation), + // slotElement + // ); + // cleanupFunctions.push(dispose); + // } + // } + // } + }) + + onCleanup(() => { + // Clean up FileDiff event handlers and dispose SolidJS components + fileDiffInstance?.cleanUp() + cleanupFunctions.forEach((dispose) => dispose()) + }) + + return ( +
+ + +