Skip to content

perf: load comments in one search, share thread downloads, speed up swipes - #351

Merged
therealsama merged 1 commit into
mainfrom
perf/comment-fetch
Nov 26, 2026
Merged

therealsama merged 1 commit into
mainfrom
perf/comment-fetch

Conversation

@therealsama

@therealsama therealsama commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Big threads took up to 15 s to load their comments, the same thread was often downloaded several times at once, and each swipe re-rendered every comment in the mounted panels.

Changes

Loading comments

  • One Algolia search per thread, for up to 1000 hits, asking only for the 4 fields a comment needs: 404 KB instead of 1.4 MB.
  • Firebase reads go out all at once, not in batches of 10. They share one WebSocket, so batches only added round trips (300 reads: 0.9 s vs 4.8 s).
  • A prefetch orders the whole thread, so the page no longer has to finish ordering it.
  • An item with no cached copy loads its comments alongside it.
  • A thread with more than 1000 comments downloads Algolia's item tree alongside the search and cancels it if the search has every comment. A comment page cancels its search of such a story, whose results it would drop.

Sharing downloads

  • Callers that want the same thread share one download. An abort only detaches its caller.
  • Opening a story from the desktop list keeps its prefetch running, and the page joins it.
  • On mobile, the other feeds' lists load together, then each feed's first thread, once the current feed is on screen.

Swiping

  • Comment is memoized, its tooltip reuses one Intl.DateTimeFormat, and it is sanitized once unless auto-linking added a link.
  • index.html has a <base target="_self">, so React Router's querySelector("base") on every link no longer scans the whole page.

Results

Measured against live HN:

Setup Before After
772-comment thread ready Chromium 4.9 s 1.3 s
3,378-comment thread ready Chromium 15.1 s 4.2 s
Open a story while it prefetches Desktop Chrome 877 ms 468 ms
Switch to Best while its thread prefetches Pixel 7, slow 4G 26.1 s 8.7 s
Every other feed's list ready Pixel 7, slow 4G 24.9 s 5.8 s
Data on a cold start Pixel 7, slow 4G 3.4 MB 1.9 MB
Main-thread time, 8 swipes Pixel 7, 4× CPU 1,443 ms 792–916 ms

Trade-offs

  • Opening a comment or poll option with no cached copy costs one Algolia search that finds nothing. It delays nothing.
  • A comment page sends a count request (under 1 KB) with each search of its story's comments.
  • For 5 minutes after the upgrade, a thread prefetched before it can show nested replies in posting order.
  • Comment times don't refresh when swiping back to a story, and the date tooltip keeps the time zone the page loaded with.

Testing

  • npm run typecheck: passed
  • npm run lint: passed
  • npm run test:run: passed, 1224 tests
  • npx playwright test --reporter=line --retries=0: 919 passed, 44 skipped, 1 failed (Mobile Safari touch-feel.spec.ts:33, a flaky swipe test that passed 3 of 3 when rerun alone). CI passed.
  • Manual: A/B of production builds against live HN on desktop and Pixel 7 emulation, unthrottled and on slow 4G. Each change returned the same comments, field for field, on 5 threads and 7 comment pages, and the single sanitize pass matched two passes on 3,010 comments.

@therealsama
therealsama force-pushed the perf/comment-fetch branch 3 times, most recently from ed5e1c4 to 7e85b3a Compare November 26, 2026 18:24
@therealsama therealsama changed the title perf: load a thread's comments in one search and read its items all at once perf: load comments in one search and share thread downloads with prefetches Nov 26, 2026
@therealsama therealsama changed the title perf: load comments in one search and share thread downloads with prefetches perf: load comments in one search, share thread downloads, speed up swipes Nov 26, 2026
…wipes

A big thread's comments took up to 15 s to load. Algolia was paged 200
hits at a time, and Firebase reads went in batches of 10, a leftover from
when items were read over REST. Reads now share one WebSocket, so batches
only add round trips.

- One Algolia search per thread, for up to 1000 hits, its cap. It asks
  for the four fields a comment needs and no highlighting, which makes
  the response 70% smaller. An empty attributesToHighlight is ignored,
  so it takes an empty list.
- Firebase reads go out all at once: for ordering, missing parents and
  reply checks.
- A prefetch orders the whole thread, not just its top level, so the
  cache's orderedDepth and the hook's ordering completion are gone.
- With no copy of the item, its comments load alongside it, from the
  same Firebase read.
- A thread with more than 1000 comments still merges Algolia's item
  tree. A Firebase count over 1000 now starts it alongside the search,
  and it is cancelled when search turns out to have every comment.
- A comment page drops a story's comments when there are more than
  1000, so a count of them, under 1 KB, now cancels that search.

On mobile, the swipe viewer prefetches each other feed's list and first
thread so a feed switch shows at once. A story often heads two feeds, so
its thread was downloaded by the feed prefetch, the next-items prefetch
and the story on screen at the same time.

- Callers that want the same thread share one download. An abort only
  detaches its caller, and the download stops once all have.
- Lists go out together, not one feed after another 500 ms apart, and
  threads follow one at a time as their lists arrive, so a stalled list
  holds up nothing else.
- It prefetches the story a feed opens on, skipping ones read in an
  earlier session, and only a thread with no copy: a stale one shows at
  once and refreshes when opened.
- It starts once the current feed's stories are on screen, including
  when a stale cache shows during a stalled refresh.

A 772-comment thread loads in 1.3 s (was 4.9 s), and a 3,378-comment one
in under 4.2 s (was 15.1 s). On slow 4G, switching to a feed whose thread
is still prefetching takes 8.7 s (was 26.1 s).

Opening a story from the list, by its comments or a text post's title,
cancelled every prefetch, its own too, so its page downloaded the thread
again. That one now keeps running and the page joins it. On desktop, a
200-comment story opened mid-prefetch shows its comments in 468 ms (was
877 ms).

Each swipe re-rendered every comment in the five mounted panels, and
each render did more work than it needed to.

- Comment is memoized.
- A comment's timestamp tooltip reuses one Intl.DateTimeFormat, where
  toLocaleString built a new one per call.
- index.html has a <base> with no href. React Router's hash history
  looks one up for every link, which scanned the whole page when there
  was none.
- A comment is sanitized once, not twice, unless auto-linking added a
  link.

On Pixel 7 emulation at 4x CPU, 8 swipes take 37-45% less main-thread
time, and opening a 3,815-comment thread cuts its longest task from
525 ms to 425 ms.
@therealsama
therealsama merged commit 2d9ecb7 into main Nov 26, 2026
4 checks passed
@therealsama
therealsama deleted the perf/comment-fetch branch November 26, 2026 22:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants