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
120 lines
3.4 KiB
Svelte
120 lines
3.4 KiB
Svelte
<script lang="ts" module>
|
|
/**
|
|
* Variable-height windowing list. Unlike {@link VirtualList} (uniform row
|
|
* pitch), each row declares its own `height`, so a single list can mix section
|
|
* headers and content rows of differing heights — e.g. the photo timeline's
|
|
* date headers and (square or justified) tile strips.
|
|
*
|
|
* It builds a prefix-sum offset table once per `rows` change and binary-searches
|
|
* the visible band on scroll, rendering only those rows (plus an overscan
|
|
* margin) and reserving the full height with a spacer so the scrollbar and any
|
|
* end-of-list sentinel behave exactly as with a fully-rendered list. Declared
|
|
* `height` MUST match the rendered row height or rows will drift.
|
|
*/
|
|
export interface VirtualRow {
|
|
/** Rendered height of this row in px (incl. its own bottom gap). */
|
|
height: number;
|
|
/** Stable identity; keeps unchanged rows mounted as the window slides. */
|
|
key?: string | number;
|
|
}
|
|
|
|
export interface VirtualRowsProps<T extends VirtualRow> {
|
|
rows: T[];
|
|
/** Extra pixels rendered above and below the viewport. */
|
|
overscan?: number;
|
|
windowClass?: string;
|
|
windowStyle?: string;
|
|
row: import('svelte').Snippet<[T, number]>;
|
|
}
|
|
</script>
|
|
|
|
<script lang="ts" generics="T extends VirtualRow">
|
|
import { onMount } from 'svelte';
|
|
import { useVirtualWindow } from '$lib/composables/useVirtualWindow.svelte';
|
|
|
|
let {
|
|
rows,
|
|
overscan = 600,
|
|
windowClass = '',
|
|
windowStyle = '',
|
|
row
|
|
}: VirtualRowsProps<T> = $props();
|
|
|
|
let rootEl: HTMLDivElement;
|
|
const vw = useVirtualWindow();
|
|
|
|
// offsets[i] = Y of row i; offsets[rows.length] = total height.
|
|
const offsets = $derived.by(() => {
|
|
const o = new Array<number>(rows.length + 1);
|
|
o[0] = 0;
|
|
for (let i = 0; i < rows.length; i++) o[i + 1] = o[i] + rows[i].height;
|
|
return o;
|
|
});
|
|
const totalHeight = $derived(offsets[rows.length] ?? 0);
|
|
|
|
/** First index whose offset is > x. */
|
|
function upperBound(arr: number[], x: number): number {
|
|
let lo = 0;
|
|
let hi = arr.length;
|
|
while (lo < hi) {
|
|
const mid = (lo + hi) >> 1;
|
|
if (arr[mid] <= x) lo = mid + 1;
|
|
else hi = mid;
|
|
}
|
|
return lo;
|
|
}
|
|
|
|
/** First index whose offset is >= x. */
|
|
function lowerBound(arr: number[], x: number): number {
|
|
let lo = 0;
|
|
let hi = arr.length;
|
|
while (lo < hi) {
|
|
const mid = (lo + hi) >> 1;
|
|
if (arr[mid] < x) lo = mid + 1;
|
|
else hi = mid;
|
|
}
|
|
return lo;
|
|
}
|
|
|
|
const band = $derived.by(() => {
|
|
const n = rows.length;
|
|
if (n === 0) return { first: 0, last: 0, top: 0 };
|
|
const topPx = vw.aboveBy - overscan;
|
|
const botPx = vw.aboveBy + vw.viewportH + overscan;
|
|
let first = upperBound(offsets, topPx) - 1; // row straddling/just above the top
|
|
if (first < 0) first = 0;
|
|
let last = lowerBound(offsets, botPx); // exclusive: first row starting at/after bottom
|
|
if (last < first + 1) last = first + 1;
|
|
if (last > n) last = n;
|
|
return { first, last, top: offsets[first] };
|
|
});
|
|
|
|
const visible = $derived(rows.slice(band.first, band.last));
|
|
|
|
onMount(() => vw.observe(rootEl));
|
|
</script>
|
|
|
|
<div bind:this={rootEl} class="vrows" style:height="{totalHeight}px">
|
|
<div
|
|
class="vrows__window {windowClass}"
|
|
style="transform: translateY({band.top}px); {windowStyle}"
|
|
>
|
|
{#each visible as r, i (r.key ?? band.first + i)}
|
|
{@render row(r, band.first + i)}
|
|
{/each}
|
|
</div>
|
|
</div>
|
|
|
|
<style>
|
|
.vrows {
|
|
position: relative;
|
|
width: 100%;
|
|
}
|
|
|
|
.vrows__window {
|
|
position: absolute;
|
|
inset: 0 0 auto;
|
|
will-change: transform;
|
|
}
|
|
</style>
|