fix: address pierre comparison review nits

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ruben Fiszel
2026-11-04 20:03:07 +01:00
co-authored by Claude Opus 5.5
parent 3aff744bfa
commit e171971259
9 changed files with 81 additions and 46 deletions
@@ -24,6 +24,8 @@
editorOptions?: Omit<FileEditorOptions, 'renderSelectionAction'>
selectionAction?: Snippet<[SelectionActionContext]>
markers?: Marker[]
/** A later editor with the same key resumes this one's draft and undo history. */
editStateKey?: string
onChange?: (code: string) => void
onEditor?: (editor: Editor<'file', undefined, undefined> | undefined) => void
class?: string
@@ -38,6 +40,7 @@
editorOptions,
selectionAction,
markers,
editStateKey,
onChange,
onEditor,
class: className = ''
@@ -100,7 +103,8 @@
renderSelectionAction: selectionAction
? (ctx) => elements.render(selectionAction, ctx)
: undefined
}))
})),
untrack(() => editStateKey)
)
dispose = e.edit(f)
editor = e
+5 -1
View File
@@ -39,7 +39,11 @@ export async function switchMainThreadHighlighter(type: HighlighterTypes): Promi
const pools = new Map<HighlighterTypes, WorkerPoolManager>()
/** One pool per highlighter backend: a pool's backend is fixed when its workers start. */
/**
* One pool per highlighter backend: a pool's backend is fixed when its workers start. Pools live
* as long as the page, shared by every component that asks; a caller that wants its workers back
* must `terminate()` the pool and accept that later components restart it.
*/
export function pierreWorkerPool(preferredHighlighter: HighlighterTypes): WorkerPoolManager {
let pool = pools.get(preferredHighlighter)
if (!pool) {
@@ -1,21 +1,22 @@
import { mount, unmount, type Snippet } from 'svelte'
import SnippetHost from './SnippetHost.svelte'
type Entry = { target: HTMLElement; component?: Record<string, any>; seen: boolean; frames: number }
type Entry = { target: HTMLElement; component?: Record<string, any>; seen: boolean; sweeps: number }
// Pierre may build an element and drop it in the same pass without inserting it.
const MAX_UNSEEN_FRAMES = 10
const MAX_UNSEEN_SWEEPS = 10
const SWEEP_MS = 500
/**
* Pierre's render callbacks return detached DOM nodes and later remove them without telling
* the caller. This mounts a Svelte snippet into each one and unmounts it once its node has
* left the document, checked on the frame after a render so a node Pierre detaches and
* reinserts within one render is kept.
* the caller (a re-render with fewer annotations, a selection popover closing). This mounts a
* Svelte snippet into each one and unmounts it once its node has left the document. Nodes are
* checked on a timer rather than at render, so one Pierre detaches and reinserts within a
* render is kept, and one removed with no render following is still released.
*/
export function snippetElements() {
const entries = new Set<Entry>()
let destroyed = false
let sweepScheduled = false
let timer: ReturnType<typeof setInterval> | undefined
function drop(entry: Entry) {
if (entry.component) unmount(entry.component)
@@ -23,42 +24,33 @@ export function snippetElements() {
}
function sweep() {
sweepScheduled = false
if (destroyed) return
let pending = false
for (const entry of entries) {
if (entry.target.isConnected) {
entry.seen = true
} else if (entry.seen || ++entry.frames > MAX_UNSEEN_FRAMES) {
drop(entry)
} else {
pending = true
}
if (entry.target.isConnected) entry.seen = true
else if (entry.seen || ++entry.sweeps > MAX_UNSEEN_SWEEPS) drop(entry)
}
if (pending) scheduleSweep()
if (entries.size === 0) stop()
}
function scheduleSweep() {
if (sweepScheduled) return
sweepScheduled = true
requestAnimationFrame(sweep)
function stop() {
clearInterval(timer)
timer = undefined
}
return {
render<T>(snippet: Snippet<[T]>, arg: T, tag: 'div' | 'span' = 'div'): HTMLElement {
const entry: Entry = { target: document.createElement(tag), seen: false, frames: 0 }
const entry: Entry = { target: document.createElement(tag), seen: false, sweeps: 0 }
entries.add(entry)
timer ??= setInterval(sweep, SWEEP_MS)
// Pierre re-renders synchronously when an edit session ends, which can be inside an
// effect teardown, where Svelte refuses to create the mounted component's effects.
queueMicrotask(() => {
if (destroyed || !entries.has(entry)) return
if (!entries.has(entry)) return
entry.component = mount(SnippetHost<T>, { target: entry.target, props: { snippet, arg } })
})
scheduleSweep()
return entry.target
},
destroy() {
destroyed = true
stop()
for (const entry of entries) drop(entry)
}
}
@@ -1,5 +1,5 @@
<script lang="ts">
import { tick } from 'svelte'
import { onDestroy, tick } from 'svelte'
import { Button } from '$lib/components/common'
import DiffEditor from '$lib/components/DiffEditor.svelte'
import ToolCodeDiffView from '$lib/components/copilot/chat/ToolCodeDiffView.svelte'
@@ -8,7 +8,7 @@
import { switchMainThreadHighlighter } from '$lib/components/pierre/pierre'
import { caseFileDiff, type ComparisonCase } from './cases'
import { countNodes, formatMs, observeLongTasks, sleep, waitForSelector } from './measure'
import { diffOptions, settingsPool, type PierreSettings } from './pierreSettings'
import { diffOptions, useSettingsPool, type PierreSettings } from './pierreSettings.svelte'
let {
diffCase,
@@ -86,8 +86,14 @@
let box: HTMLDivElement | undefined = $state()
const currentSettings = $derived(current?.pierre ? { ...settings, ...current.pierre } : settings)
const pool = useSettingsPool(() => currentSettings)
const fileDiff = $derived(caseFileDiff(diffCase))
// Leaving the tab must stop the loop: a later backend switch would dispose the highlighter
// whatever is mounted next relies on.
let destroyed = false
onDestroy(() => (destroyed = true))
async function runAll(rounds: number) {
running = true
onRunningChange?.(true)
@@ -97,6 +103,7 @@
for (const r of renderers) {
current = undefined
await tick()
if (destroyed) return
const s = { ...settings, ...r.pierre }
if (r.pierre && !s.useWorkers) await switchMainThreadHighlighter(s.highlighter)
await sleep(300)
@@ -191,7 +198,7 @@
class="h-full overflow-auto"
items={[{ type: 'diff', id: diffCase.id, fileDiff }]}
options={diffOptions(currentSettings)}
workerPool={settingsPool(currentSettings)}
workerPool={pool.current}
/>
{:else if current?.pierre}
<PierreDiff
@@ -200,7 +207,7 @@
filename={diffCase.filename}
lang={diffCase.diff.lang}
options={diffOptions(currentSettings)}
workerPool={settingsPool(currentSettings)}
workerPool={pool.current}
/>
{/if}
</div>
@@ -10,7 +10,7 @@
import WorkspaceScriptPicker from './WorkspaceScriptPicker.svelte'
import { switchMainThreadHighlighter } from '$lib/components/pierre/pierre'
import { caseFileDiff, cases, type ComparisonCase } from './cases'
import { DEFAULT_SETTINGS, diffOptions, settingsPool } from './pierreSettings'
import { DEFAULT_SETTINGS, diffOptions, useSettingsPool } from './pierreSettings.svelte'
let settings = $state({ ...DEFAULT_SETTINGS })
let virtualized = $state(false)
@@ -23,7 +23,7 @@
: (cases.find((c) => c.id === activeCaseId) ?? cases[0])
)
const options = $derived(diffOptions(settings))
const pool = $derived(settingsPool(settings))
const pool = useSettingsPool(() => settings)
</script>
<div class="flex flex-col gap-4">
@@ -104,7 +104,7 @@
class="h-full overflow-auto"
items={[{ type: 'diff', id: activeCase.id, fileDiff: caseFileDiff(activeCase) }]}
{options}
workerPool={pool}
workerPool={pool.current}
/>
{:else}
<PierreDiff
@@ -114,7 +114,7 @@
lang={activeCase.diff.lang}
beforeLang={activeCase.diff.beforeLang}
{options}
workerPool={pool}
workerPool={pool.current}
/>
{/if}
{/snippet}
@@ -10,10 +10,14 @@
import { loadCopilot } from '$lib/components/copilot/loadCopilot'
import { copilotInfo } from '$lib/aiStore'
import { workspaceStore } from '$lib/stores'
import { randomUUID } from '$lib/utils/uuid'
import { Redo2, Undo2 } from 'lucide-svelte'
import { lint } from './lint'
import { REVIEW_BEFORE } from './reviewSample'
// Changing the prediction mode recreates the editor; the key carries the draft and undo
// history over. Per tab instance, so reopening the tab starts from the sample again.
const editStateKey = randomUUID()
let pierreCode = $state(REVIEW_BEFORE)
let monacoCode = $state(REVIEW_BEFORE)
let markers: Marker[] = $state.raw(lint(REVIEW_BEFORE))
@@ -117,6 +121,7 @@
{#key predictionMode}
<PierreFileEditor
code={REVIEW_BEFORE}
{editStateKey}
filename="billing.ts"
lang="typescript"
{editorOptions}
@@ -7,7 +7,7 @@
import PierreCodeView from '$lib/components/pierre/PierreCodeView.svelte'
import { switchMainThreadHighlighter } from '$lib/components/pierre/pierre'
import { countNodes, formatMs, observeLongTasks, sleep, waitForSelector } from './measure'
import { DEFAULT_SETTINGS, diffOptions, settingsPool } from './pierreSettings'
import { DEFAULT_SETTINGS, diffOptions, useSettingsPool } from './pierreSettings.svelte'
import PierreSettingsBar from './PierreSettingsBar.svelte'
type Side = 'pierre' | 'monaco'
@@ -49,7 +49,7 @@
)
}))
)
const pool = $derived(settingsPool(settings))
const pool = useSettingsPool(() => settings)
async function show(side: Side) {
stats[side] = undefined
@@ -114,7 +114,7 @@
class="h-full overflow-auto"
{items}
options={diffOptions(settings)}
workerPool={pool}
workerPool={pool.current}
/>
{/await}
{/key}
@@ -2,7 +2,7 @@
import Select from '$lib/components/select/Select.svelte'
import Toggle from '$lib/components/Toggle.svelte'
import { canUseWASM } from '@pierre/diffs'
import { THEMES, type PierreSettings } from './pierreSettings'
import { THEMES, type PierreSettings } from './pierreSettings.svelte'
let { settings = $bindable() }: { settings: PierreSettings } = $props()
@@ -56,10 +56,33 @@ export function diffOptions(
}
}
/** Theme and inline-diff granularity belong to the pool when one is used, not to the component. */
export function settingsPool(s: PierreSettings): WorkerPoolManager | undefined {
if (!s.useWorkers) return undefined
const pool = pierreWorkerPool(s.highlighter)
void pool.setRenderOptions({ theme: THEMES[s.theme], lineDiffType: s.lineDiffType })
return pool
// Pools are shared across components, so the options last applied are tracked per pool.
const appliedOptions = new WeakMap<WorkerPoolManager, string>()
/**
* The pool for these settings, with its render options kept in sync: theme and inline-diff
* granularity belong to the pool when one is used, not to the component. Call during component
* init; the getter is safe in `$derived` and templates.
*/
export function useSettingsPool(settings: () => PierreSettings): {
readonly current: WorkerPoolManager | undefined
} {
const pool = $derived(
settings().useWorkers ? pierreWorkerPool(settings().highlighter) : undefined
)
$effect(() => {
const s = settings()
if (!pool) return
const key = `${s.theme}:${s.lineDiffType}`
// Setting options re-renders every mounted component and clears the pool's cache.
// `pierreWorkerPool` starts a pool on the pierre theme and Pierre's default `word-alt`.
if ((appliedOptions.get(pool) ?? 'pierre:word-alt') === key) return
appliedOptions.set(pool, key)
void pool.setRenderOptions({ theme: THEMES[s.theme], lineDiffType: s.lineDiffType })
})
return {
get current() {
return pool
}
}
}