perf: virtual list was rendering every single item and then destroying

This commit is contained in:
Xylight committed 2026-06-11 12:17:50 -07:00
1 parent 62f2fe80f7
commit aca4a3e06a
4 files changed
+77 -49

No files matched your search

+37 -3
View File
@@ -20,6 +20,7 @@
useWindow?: boolean useWindow?: boolean
height?: number height?: number
itemContainer?: string itemContainer?: string
initialScrollIndex?: number
} }
let { let {
@@ -33,6 +34,7 @@
useWindow = true, useWindow = true,
height = 0, height = 0,
itemContainer = 'div', itemContainer = 'div',
initialScrollIndex = 0,
...rest ...rest
}: Props = $props() }: Props = $props()
@@ -80,7 +82,23 @@
return cumulation return cumulation
}) })
let scrollY = $state(0) let isRestoring = untrack(() => initialScrollIndex > 0)
let initialScrollY = untrack(() => {
if (isRestoring && initialScrollIndex < itemHeights.length) {
let sum = 0
for (let i = 0; i < initialScrollIndex; i++) {
sum += itemHeights[i] || estimatedHeight
}
const targetPx = sum - (initialOffset || 0)
if (targetPx >= 0) {
return targetPx
}
}
return 0
})
let scrollY = $state(initialScrollY)
let viewportHeight = $state(0) let viewportHeight = $state(0)
let visibleItems = $state<{ index: number; offset: number }[]>([]) let visibleItems = $state<{ index: number; offset: number }[]>([])
@@ -206,6 +224,10 @@
}) })
untrack(() => { untrack(() => {
if (initialScrollIndex && initialScrollIndex > 0) {
scrollToIndex(initialScrollIndex, useWindow)
isRestoring = false
}
visibleItems = updateVisibleItems() visibleItems = updateVisibleItems()
initialRender = false initialRender = false
}) })
@@ -217,7 +239,13 @@
bind:scrollY={ bind:scrollY={
() => (useWindow ? scrollY : 0), () => (useWindow ? scrollY : 0),
(v) => { (v) => {
if (useWindow) scrollY = v if (useWindow) {
if (isRestoring) {
if (v === 0) return
isRestoring = false
}
scrollY = v
}
} }
} }
/> />
@@ -225,7 +253,8 @@
<div <div
bind:this={virtualListEl} bind:this={virtualListEl}
style="position: relative; height: {height || style="position: relative; height: {height ||
cumulativeItemHeights[visibleItems?.[visibleItems.length - 1]?.index]}px;" cumulativeItemHeights[items.length - 1] ||
0}px;"
{...rest} {...rest}
id="feed" id="feed"
onscroll={() => { onscroll={() => {
@@ -246,6 +275,11 @@
{@render itemSnippet(item.index)} {@render itemSnippet(item.index)}
</svelte:element> </svelte:element>
{/each} {/each}
<div
style="height: {(cumulativeItemHeights[items.length - 1] || 0) -
(cumulativeItemHeights[visibleItems?.[visibleItems.length - 1]?.index] ||
0)}px; border: 0 !important;"
></div>
</div> </div>
{#if settings.debugInfo} {#if settings.debugInfo}
<Expandable> <Expandable>
+28 -44
View File
@@ -9,7 +9,8 @@
import Placeholder from '$lib/ui/info/Placeholder.svelte' import Placeholder from '$lib/ui/info/Placeholder.svelte'
import EndPlaceholder from '$lib/ui/layout/EndPlaceholder.svelte' import EndPlaceholder from '$lib/ui/layout/EndPlaceholder.svelte'
import { Button, Material, Spinner } from 'mono-svelte' import { Button, Material, Spinner } from 'mono-svelte'
import { onDestroy, onMount, untrack } from 'svelte' import { onDestroy, untrack } from 'svelte'
import type { Attachment } from 'svelte/attachments'
import { import {
ArchiveBox, ArchiveBox,
ArrowsPointingOut, ArrowsPointingOut,
@@ -106,55 +107,35 @@
} }
} }
const callback: IntersectionObserverCallback = (entries, observer) => { const observer = browser
entries.forEach((entry) => { ? new IntersectionObserver(
if (!entry.isIntersecting) return (entries) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) return
const element = entry.target as HTMLElement const element = entry.target as HTMLElement
const id = element.getAttribute('data-index') const id = element.getAttribute('data-index')
if (!id) return if (!id) return
lastSeen = Number(id) lastSeen = Number(id)
observer.unobserve(element) observer?.unobserve(element)
}) })
},
{
threshold: 0.5,
},
)
: null
const intersectionAttach: Attachment = (node) => {
observer?.observe(node)
return () => {
observer?.unobserve(node)
}
} }
onMount(() => {
const observer = new IntersectionObserver(callback, {
threshold: 0.5,
})
const observePost = (node: Node) => {
if (
node instanceof HTMLElement &&
node.classList.contains('post-container')
)
observer.observe(node)
}
const unobservePost = (node: Node) => {
if (
node instanceof HTMLElement &&
node.classList.contains('post-container')
)
observer.unobserve(node)
}
document.querySelectorAll('.post-container').forEach(observePost)
const feed = document.getElementById('feed')
if (!feed) return
new MutationObserver((mutations) => {
mutations.forEach(({ addedNodes, removedNodes }) => {
addedNodes.forEach(observePost)
removedNodes.forEach(unobservePost)
})
}).observe(feed, { childList: true, subtree: false })
})
$effect(() => { $effect(() => {
if (listComp) { if (listComp) {
untrack(() => { untrack(() => {
@@ -169,6 +150,7 @@
onDestroy(() => { onDestroy(() => {
abortLoad?.abort() abortLoad?.abort()
observer?.disconnect()
}) })
</script> </script>
@@ -201,6 +183,7 @@
estimatedHeight={settings.view == 'cozy' ? 500 : 150} estimatedHeight={settings.view == 'cozy' ? 500 : 150}
bind:restore={virtualList} bind:restore={virtualList}
bind:this={listComp} bind:this={listComp}
initialScrollIndex={lastSeen}
> >
{#snippet item(row)} {#snippet item(row)}
<!--god svelte is gonna make me lose it--> <!--god svelte is gonna make me lose it-->
@@ -215,6 +198,7 @@
filter.value.action == 'hide' && 'hidden', filter.value.action == 'hide' && 'hidden',
row < 7 && '', row < 7 && '',
]} ]}
{@attach intersectionAttach}
> >
<!--TODO make my component isolation not abysmal--> <!--TODO make my component isolation not abysmal-->
{#if filter.value.action == 'none'} {#if filter.value.action == 'none'}
+6 -1
View File
@@ -104,7 +104,12 @@
bind:posts bind:posts
bind:lastSeen={() => client.lastSeen ?? 0, (v) => (client.lastSeen = v)} bind:lastSeen={() => client.lastSeen ?? 0, (v) => (client.lastSeen = v)}
bind:params={getParams} bind:params={getParams}
virtualList={{ itemHeights: client?.itemHeights ?? [] }} bind:virtualList={
() => ({ itemHeights: client.itemHeights ?? [] }),
(v) => {
if (v) client.itemHeights = v.itemHeights
}
}
/> />
<svelte:element <svelte:element
this={settings.infiniteScroll && !settings.posts.noVirtualize this={settings.infiniteScroll && !settings.posts.noVirtualize
+6 -1
View File
@@ -86,7 +86,12 @@
() => feed.client.lastSeen ?? 0, (v) => (feed.client.lastSeen = v) () => feed.client.lastSeen ?? 0, (v) => (feed.client.lastSeen = v)
} }
bind:params={feed.params} bind:params={feed.params}
virtualList={{ itemHeights: feed.client?.itemHeights ?? [] }} bind:virtualList={
() => ({ itemHeights: feed.client.itemHeights ?? [] }),
(v) => {
if (v) feed.client.itemHeights = v.itemHeights
}
}
/> />
<svelte:element <svelte:element
this={settings.infiniteScroll && !settings.posts.noVirtualize this={settings.infiniteScroll && !settings.posts.noVirtualize