Virtualize the patch-review diff renderer

Route the comment-capable diff through the same single virtualizer as
the commit diff instead of rendering every file and line at once.
This commit is contained in:
Rūdolfs Ošiņš
2026-07-03 11:28:36 +02:00
parent 41535bf32c
commit 737703422e
8 changed files with 742 additions and 773 deletions
+70 -4
View File
@@ -21,6 +21,11 @@
bufferSize?: number;
// Height of any content rendered before this list inside the same scroller.
startMargin?: number;
// Measure the list's own offset within the scroll viewport and use it as
// `startMargin`, kept up to date as content above the list resizes. Use when
// the list is not the first thing in the scroller (e.g. the patch diff sits
// below the patch metadata and commit list).
autoStartMargin?: boolean;
// Distance from the bottom (in pixels) at which to request the next page.
prefetchPx?: number;
// Restore a previously-captured scroll position (used on history nav).
@@ -50,6 +55,7 @@
estimatedItemSize = undefined,
bufferSize = undefined,
startMargin = undefined,
autoStartMargin = false,
prefetchPx = 800,
initialCache = undefined,
initialScrollOffset = undefined,
@@ -65,6 +71,61 @@
let handle = $state<VirtualizerHandle | undefined>(undefined);
let restored = false;
// A zero-height marker placed right before the virtualizer; its position
// tells us how far down the list starts inside the scroll viewport.
let anchorEl = $state<HTMLElement | undefined>(undefined);
let measuredStartMargin = $state(0);
const effectiveStartMargin = $derived(
autoStartMargin ? measuredStartMargin : startMargin,
);
function measureStartMargin() {
if (!autoStartMargin || !anchorEl || !viewport) return;
// Offset within the scroll content (scroll-invariant: the moving rect top is
// cancelled by scrollTop), so it only changes when content above resizes.
const offset =
anchorEl.getBoundingClientRect().top -
viewport.getBoundingClientRect().top +
viewport.scrollTop;
const next = Math.max(0, Math.round(offset));
if (next !== measuredStartMargin) {
measuredStartMargin = next;
}
}
$effect(() => {
const vp = viewport;
if (!autoStartMargin || !vp) return;
// The content above the list lives in sibling/ancestor elements, not in this
// component, so observe the viewport's direct children: any height change
// upstream resizes the child that contains it. A MutationObserver keeps the
// observed set current if those children are added/removed.
const refresh = () => {
measureStartMargin();
// The list may not have scrolled, so re-report so sticky-header math picks
// up the new margin immediately.
reportScrollState();
};
const ro = new ResizeObserver(refresh);
const observeChildren = () => {
ro.disconnect();
for (const child of Array.from(vp.children)) {
ro.observe(child);
}
};
const mo = new MutationObserver(() => {
observeChildren();
refresh();
});
observeChildren();
mo.observe(vp, { childList: true });
measureStartMargin();
return () => {
ro.disconnect();
mo.disconnect();
};
});
$effect(() => {
// Restore the saved scroll offset once, after the virtualizer has mounted
// with the restored size cache (so the offset maps to the right item).
@@ -86,10 +147,13 @@
function reportScrollState() {
const h = handle;
if (!h || !onScrollState) return;
const scrollOffset = h.getScrollOffset();
// `getScrollOffset` is the raw viewport scrollTop; `findItemIndex` expects it
// raw (it subtracts the start margin internally). `getItemOffset` is
// list-relative, so the reported offset is too, letting callers mix the two.
const raw = h.getScrollOffset();
onScrollState({
topIndex: h.findItemIndex(scrollOffset),
scrollOffset,
topIndex: h.findItemIndex(raw),
scrollOffset: raw - (effectiveStartMargin ?? 0),
itemOffset: index => h.getItemOffset(index),
});
}
@@ -117,12 +181,14 @@
const id = requestAnimationFrame(() => {
maybeLoadMore();
reportScrollState();
measureStartMargin();
});
return () => cancelAnimationFrame(id);
});
</script>
{#if viewport}
<div bind:this={anchorEl} style:height="0"></div>
<Virtualizer
bind:this={handle}
data={items}
@@ -130,7 +196,7 @@
{getKey}
itemSize={estimatedItemSize}
{bufferSize}
{startMargin}
startMargin={effectiveStartMargin}
cache={initialCache}
onscroll={onScroll}
onscrollend={reportState}>