Skip to content

perf(block): mount below-fold children after paint - #13447

Closed
yuxi-liu-wired wants to merge 1 commit into
logseq:masterfrom
yuxi-liu-wired:perf/row-rendering
Closed

yuxi-liu-wired wants to merge 1 commit into
logseq:masterfrom
yuxi-liu-wired:perf/row-rendering

Conversation

@yuxi-liu-wired

@yuxi-liu-wired yuxi-liu-wired commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

What changes

Opening a page mounted, before its first paint, every children subtree whose top lay within 1200 px below the viewport. On a 700 px window, quantization (92 blocks) shows 13 rows but mounted 54 rows, about 4 screens, before its first frame, in 1 task of 250-356 ms cold and 114-174 ms warm. lazy-block-children decides this in a layout effect through getBoundingClientRect, so each nesting level also forced a layout: in 1 ms CPU profiles of a cold open, that task spent 71 ms in 4 forced layouts, 155 ms in component code and 49 ms in the mldoc parse.

The layout-effect check (near-block-viewport?) now mounts only subtrees that reach into the viewport; the 1200 px margin above it stays, so content above a scrolled position still mounts before paint and nothing on screen shifts. The IntersectionObserver fallback now takes the page's scroll container as its root (lazy-children-observer-root), so subtrees below the fold mount in the frames right after the first paint.

This also fixes the observer's margin. With the window as root, #main-content-container clipped the rows below its fold, so the 1200 px margin never applied: after a 700 px scroll, placeholders 551-1023 px below the viewport stayed unmounted for 1.5 s, until scrolled into view. With the scroll container as root, a scroll of 700, 1500 or 2500 px leaves no placeholder within 1200 px of the viewport's bottom edge on the 2 pages checked (quantization kept 3, 1 and 4 before; computer vision 0, 5 and 2).

Numbers

Desktop release builds of master (d9d625847) and of this branch, each shipped as only the renderer bundle in the same nightly app shell, on an imported graph of about 154k blocks. 3 runs per build, alternating build by build, each run relaunching the app per page, then opening it cold and twice warm. Medians (ranges) in ms of the frame at which every row on screen is in the DOM:

page cold, master cold, this branch warm, master warm, this branch warm again, master warm again, this branch
quantization, 92 blocks 935 (866-1051) 711 (690-752) 227 (152-248) 92 (68-95) 204 (147-235) 112 (107-135)
computer vision, 389 blocks 887 (569-896) 681 (668-774) 219 (213-309) 131 (110-147) 212 (193-258) 128 (80-148)

The 3 small pages (9-21 blocks), Tracy-Widom distribution (65 blocks) and Douglas Lenat (742 blocks) do not change beyond run-to-run spread: their rows below the fold are top-level rows, which this change leaves eager (top-level rows render eagerly below 64, use-virtual-list-opts).

Combined with #13437 (request order), 6 runs per build: quantization shows its rows on screen at 312 instead of 406 ms cold (ranges overlap) and at 86-93 instead of 214-234 ms warm.

The price is below the fold: on the 3 pages with deferred rows, the frame at which all rows mounted by the old code are present comes later in 8 of 9 medians, by 16 to 145 ms (up to 219 ms combined with #13437). Those rows are 1-4 screens down.

Tests

  • frontend.components.block.lazy-children-test (new): the layout-effect check mounts subtrees that reach into the viewport, leaves subtrees below the fold to the observer even within the margin, and keeps the margin above; the observer's root is the scroll container only when it holds the subtree. The below-the-fold assertions fail on master.
  • The whole unit suite at TZ=Etc/GMT+12 (2478 tests, 12418 assertions) and the time-zone-dependent namespaces at TZ=Pacific/Kiritimati (1446 tests, 8979 assertions): the only failures are the zone-dependent tests that fix(test): build test dates in local time #13430 fixes (6 and 4 assertions), the same as on master.

Allow edits from maintainers.


Devin Review

Opening a page mounted, before its first paint, every children subtree
that started within 1200 px below the viewport. lazy-block-children
decides this in a layout effect through getBoundingClientRect, so each
nesting level also forced a layout. On a 700 px window, quantization
(92 blocks) shows 13 rows on screen but mounted 54 rows, the lowest at
3169 px, before its first frame. In 1 ms CPU profiles of a cold open
that render task took 326-362 ms: component code 155 ms, forced layout
71 ms in 4 reads (one per nesting level), mldoc parse 49 ms.

The IntersectionObserver fallback, meant to mount the rest with the same
1200 px margin, used the window as its root. #main-content-container
still clips rows below its fold, so the margin never applied: after a
700 px scroll, placeholders 551-1023 px below the viewport stayed
unmounted for 1.5 s, until scrolled into view.

The layout-effect check now mounts only subtrees that reach into the
viewport (the margin above it stays), and the observer's root is the
scroll container, so its margin applies: subtrees below the fold mount
in the frames right after the first paint.

Measured over 3 interleaved relaunched runs per build, medians (ranges)
of the frame at which every row on screen is in the DOM:
- quantization: cold 935 (866-1051) -> 711 (690-752) ms, warm 227
  (152-248) -> 92 (68-95) ms, warm2 204 (147-235) -> 112 (107-135) ms
- computer vision (389 blocks): cold 887 (569-896) -> 681 (668-774) ms,
  warm 219 (213-309) -> 131 (110-147) ms, warm2 212 (193-258) -> 128
  (80-148) ms
- 3 small pages (9-21 blocks), Tracy-Widom distribution (65) and
  Douglas Lenat (742): within run-to-run spread, except Tracy-Widom
  warm2 221 (209-233) -> 179 (114-182) ms. Their rows below the fold
  are top-level rows, which still mount eagerly.
The last rows below the fold arrive later: on the 3 pages with deferred
rows, the frame at which all initially mounted rows are present moves
later in 8 of 9 medians, by 16 to 145 ms.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Continued in #13546 together with #13451: they share files, so they now merge once. The commits are the same, on current master.

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.

1 participant