ccb85f53c0
The Photos "moments" grid rendered every tile into the DOM, so a 20k-photo library mounted ~140k nodes / 20k <img> elements, held ~196MB JS heap, took ~3.5s to first paint and scrolled at ~6fps. It also ran the justified-layout maths inside the template (recomputed on every reactive change) and generated client-side video thumbnails for every off-screen video, not just visible ones. Introduce `VirtualRows` — a variable-height, section-aware sibling of `VirtualList` — and flatten the grouped timeline into one list of fixed-height rows (a date header or a strip of explicitly-sized tiles) shared by both the square and justified layouts. Only the rows near the viewport are mounted; a prefix-sum offset table + binary search find the visible band, and a spacer reserves the full height so the sticky header and load-more sentinel are unchanged. The justified packing now runs once per groups/width/layout change in a $derived, not per render. To avoid duplicating the scroll-tracking logic across the two windowing components, extract it into a `useVirtualWindow` composable (scroll-ancestor detection + rAF-throttled aboveBy/viewportH signals); `VirtualList` is refactored onto it with identical measured numbers. Measured in headless Chromium (1280x900), synthetic photos, before/after: SQUARE | mount→tiles | DOM nodes | <img> | JS heap | scroll frame ------------+-------------+-----------+-------+---------+------------- 2000 | 416→94 ms | 14k→629 |2000→96| 21→5 MB | 29→29 ms 5000 | 916→114 ms | 35k→629 |5000→96| 50→9 MB | 62→26 ms 20000 | 3455→220 ms | 140k→629 |20k→96 |196→29 MB|152→33 ms JUSTIFIED 20000: mount 245 ms · DOM 315 · <img> 44 · heap 33 MB · ~60fps Rendered DOM, mounted <img> count and heap are now flat (O(visible)) regardless of library size; mount is ~16x faster and scroll jank drops from 413 to ≤24 frames. Off-screen video-thumbnail generation no longer fires for non-visible tiles. Correctness verified by probing a deep scroll in both layouts (tiles land within the viewport band; square cells equal-width, justified rows aspect-preserving). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01M8Vb9QHmLZnEMzHz7MrFy6
115 lines
3.7 KiB
Svelte
115 lines
3.7 KiB
Svelte
<script lang="ts" module>
|
|
/**
|
|
* Generic windowing list. Renders only the rows intersecting the nearest
|
|
* scrollable ancestor's viewport (plus an overscan margin), reserving the full
|
|
* scroll height with a sized spacer so the scrollbar, sticky headers and any
|
|
* end-of-list sentinel keep behaving exactly as with a fully-rendered list.
|
|
*
|
|
* Scroll-ancestor based (not its own scroll box) so it drops into the existing
|
|
* `.content-area` layout without changing the single-scrollbar UX. Row height
|
|
* is auto-measured for the single-column case; pass `rowHeight` as the estimate
|
|
* (and for multi-column grids, where it must be the row pitch incl. gap).
|
|
*/
|
|
export interface VirtualListProps<T> {
|
|
items: T[];
|
|
/** Row pitch in px (height incl. row gap). Auto-refined when columns === 1. */
|
|
rowHeight?: number;
|
|
/** Items per row; > 1 lays the window out as a grid. */
|
|
columns?: number;
|
|
/** Extra rows rendered above and below the viewport. */
|
|
overscan?: number;
|
|
/** Class applied to the inner window (e.g. the grid container class). */
|
|
windowClass?: string;
|
|
/** Inline style applied to the inner window (e.g. grid-template-columns). */
|
|
windowStyle?: string;
|
|
/** Stable key per item (defaults to the absolute index). */
|
|
key?: (item: T, index: number) => string | number;
|
|
row: import('svelte').Snippet<[T, number]>;
|
|
}
|
|
</script>
|
|
|
|
<script lang="ts" generics="T">
|
|
import { onMount } from 'svelte';
|
|
import { useVirtualWindow } from '$lib/composables/useVirtualWindow.svelte';
|
|
|
|
let {
|
|
items,
|
|
rowHeight = 48,
|
|
columns = 1,
|
|
overscan = 6,
|
|
windowClass = '',
|
|
windowStyle = '',
|
|
key,
|
|
row
|
|
}: VirtualListProps<T> = $props();
|
|
|
|
let rootEl: HTMLDivElement;
|
|
/** Measured row pitch in px; 0 until known, then refined from a real row. */
|
|
let measuredRow = $state(0);
|
|
const vw = useVirtualWindow();
|
|
|
|
const cols = $derived(Math.max(1, columns));
|
|
const effRowH = $derived(measuredRow > 0 ? measuredRow : rowHeight);
|
|
const rowCount = $derived(Math.ceil(items.length / cols));
|
|
const totalHeight = $derived(rowCount * effRowH);
|
|
|
|
// Visible row band, derived from the shared scroll signals + the row pitch.
|
|
const rh = $derived(effRowH || rowHeight);
|
|
const firstRow = $derived(Math.max(0, Math.floor(vw.aboveBy / rh) - overscan));
|
|
const lastRow = $derived(
|
|
Math.min(rowCount, Math.ceil((vw.aboveBy + vw.viewportH) / rh) + overscan)
|
|
);
|
|
const startIndex = $derived(firstRow * cols);
|
|
const endIndex = $derived(Math.min(items.length, lastRow * cols));
|
|
const offsetY = $derived(firstRow * effRowH);
|
|
const visible = $derived(items.slice(startIndex, endIndex));
|
|
|
|
/** Single-column: adopt the real rendered row height once it's known. */
|
|
function refineRowHeight(): void {
|
|
if (cols !== 1 || !rootEl) return;
|
|
const firstChild = rootEl.querySelector('.vlist__window > *') as HTMLElement | null;
|
|
if (!firstChild) return;
|
|
const h = firstChild.getBoundingClientRect().height;
|
|
if (h > 0 && Math.abs(h - measuredRow) > 0.5) measuredRow = h;
|
|
}
|
|
|
|
onMount(() => {
|
|
const stop = vw.observe(rootEl);
|
|
requestAnimationFrame(() => {
|
|
refineRowHeight();
|
|
vw.remeasure();
|
|
});
|
|
return stop;
|
|
});
|
|
|
|
// Refine the measured row height once rows are actually in the DOM.
|
|
$effect(() => {
|
|
void visible.length;
|
|
refineRowHeight();
|
|
});
|
|
</script>
|
|
|
|
<div bind:this={rootEl} class="vlist" style:height="{totalHeight}px">
|
|
<div
|
|
class="vlist__window {windowClass}"
|
|
style="transform: translateY({offsetY}px); {windowStyle}"
|
|
>
|
|
{#each visible as item, i (key ? key(item, startIndex + i) : startIndex + i)}
|
|
{@render row(item, startIndex + i)}
|
|
{/each}
|
|
</div>
|
|
</div>
|
|
|
|
<style>
|
|
.vlist {
|
|
position: relative;
|
|
width: 100%;
|
|
}
|
|
|
|
.vlist__window {
|
|
position: absolute;
|
|
inset: 0 0 auto;
|
|
will-change: transform;
|
|
}
|
|
</style>
|