Skip to content

Batch of profiling-derived performance optimizations - #570

Merged
tinchox5 merged 9 commits into
zumerlab:mainfrom
Hema2-official:perf/first-capture-comments
Nov 18, 2026
Merged

tinchox5 merged 9 commits into
zumerlab:mainfrom
Hema2-official:perf/first-capture-comments

Conversation

@Hema2-official

Copy link
Copy Markdown
Contributor

What does this change?

Each commit is an optimization and tests to back it up. Mostly lookup speedups producing identical results, or caching.

  • Scroll nodes come from a narrower selector instead from all elements.
  • A tag's defaults are read for the capture's property universe, not for all ~440 properties.
  • Retained author rules are matched once per styled element (with the selectors joined), not once per rule (where styled elements are rarely found).
  • Without a universe (a sheet from another origin that can't be read), element and pseudo-element snapshots read one property list by name instead of walking the style by index.
  • Without the scroll gate, the memo's scroll walk checks computed overflow before the layout reads (Chromium only).
  • A retained <style>'s selectors are cached between captures.
  • ::before/::after are probed only where a rule can give them content (like content, all, animation, which can all set content), so a *, ::before, ::after reset done commonly by CSS frameworks is no longer regarded.
  • Open shadow roots are taken from the memo's safety scan.

There are two minor behavioral changes, where a case turned out to be insignificantly rare, so I cut it out (yes, I, as in the human writing this up):

  • The full-read property list is now built once and shared by every document, instead of being cached per document, so fullReadableProps(style) no longer takes a document argument. To ensure that it doesn't become an issue, a test now asserts that an iframe’s document lists the same properties as the top one.
  • The scroll gate is built from the document's rules, so it cannot see a closed shadow root's :host { overflow } anymore. A host that scrolls only through such an internal rule (by changing its scrollTop) is no longer sampled for a scroll written in the same task as a capture. A scroll event, however, still marks the memo dirty. The invalidation matrix already sends closed roots to invalidate: true, so it has no new row.

Why?

On my setup, a first capture of a 5,200-element page took 163 ms in Chrome. These changes take 25 ms off, and 47 ms when a font sheet from another origin can't be read.
In Firefox it takes 14 ms off 196, and 21 ms with that font sheet.

Browser versions used for measurements: Chrome 157, Firefox 160

Checklist

  • Speed: yes
  • Fidelity: the PNG is the same as main's
  • Architecture: follows the existing structure
  • Existing tests pass and new ones cover the change: WebKit needs testing!! I'm unable to test it due to my unusual setup.

AI

  • I used AI in this PR

If you did:

  • Tests included
  • Demo included (HTML page, capture or video)
  • I reviewed every line and can explain it
Each commit, paired A/B in Chrome

Fresh context per sample, 16 rounds (32 for the match, the overflow precheck, the pseudo gate and the shadow roots), median of paired differences, 1280×800 at DPR 1. A later capture follows a one-word text change.

Commit Page First capture Later capture
Scroll gate 5,200 elements -8.5 ms of 191.5 -8.8 of 89.6
Tag defaults 5,200 elements -8.4 of 168.2
Match once per element 5,200 elements -3.1 of 80.4
Full read by name 5,200 elements, font sheet from another origin -32.2 of 248.0
Overflow precheck 5,200 elements -4.0 of 145.9
Pseudo read by name 300 list items with ::before/::after icons, font sheet from another origin -39.0 of 185.1
Selector cache 5,200 elements -3.8 of 76.9
Pseudo gate 5,200 elements -6.1 of 158.6 -4.4 of 72.1
Shadow roots 5,200 elements -1.2 of 134.3

Whole branch on the 5,200 elements: -25.1 ms of 163.0 first, -16.2 of 79.1 later; with the font sheet from another origin, -47.5 of 253.6 first and -17.8 of 108.1 later.

In Firefox (Zen, same harness, 16 rounds, the display's DPR of 1.25): -14.0 ms of 195.5 first and -13.0 of 91.5 later; with the font sheet, -21.0 of 256.0 first and -12.5 of 123.0 later. The full read by name alone takes 17 ms off that first capture and adds about 3 ms to the later one there.

Demo

After npm run compile, put this in the repo root and serve the root. Each load prints its first capture; compare main's dist with this branch's. Without the <link>, every sheet is readable and the other commits apply.

<!doctype html>
<meta charset="utf-8">
<title>First capture</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter&display=swap">
<style>
  *, ::before, ::after { box-sizing: border-box }
  body { font: 14px/1.4 Inter, Arial, sans-serif; margin: 16px }
  .grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 4px }
  .card { padding: 4px 6px; border: 1px solid #ddd; border-radius: 4px }
  .card:nth-child(4n)::before { content: '\2605  '; color: #c80 }
  .scroll { height: 60px; overflow: auto; border: 1px solid #ccc }
</style>
<pre id="out">capturing...</pre>
<div class="scroll"><div style="height: 300px">A box that scrolls</div></div>
<div class="grid" id="grid"></div>
<script type="module">
  import { snapdom } from './dist/snapdom.mjs'
  const grid = document.getElementById('grid')
  for (let i = 0; i < 2000; i++) {
    const card = document.createElement('div')
    card.className = 'card'
    card.innerHTML = `<b>Item ${i}</b> <span style="color: hsl(${i % 360} 60% 40%)">detail</span>`
    grid.append(card)
  }
  await document.fonts.ready
  await new Promise((done) => requestAnimationFrame(() => requestAnimationFrame(done)))
  const start = performance.now()
  const result = await snapdom(document.documentElement, { clip: 'viewport' })
  await result.toCanvas()
  document.getElementById('out').textContent = `first capture: ${(performance.now() - start).toFixed(1)} ms (reload to repeat)`
</script>

In Chrome at 1280×800 (median of 8 loads), main took 554 ms and this branch 412 ms; without the <link>, 230 and 203 ms.

Hema2-official and others added 9 commits November 18, 2026 17:54
Only elements that a rule, attribute or UA style makes scroll
containers can hold a scroll offset, so the memo's scroll walk now
queries a selector of those instead of visiting every element.

Chrome 157, first capture of 5,200 elements: -8.5 ms (-4%).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…y property

A tag's defaults were read for all ~440 properties, though only the
capture's universe is compared. They are now read for the universe,
and for any other property on its first miss.

Chrome 157, first capture of 5,200 elements: -8.4 ms (-5%).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…er rule

Protecting inline styles from retained author rules ran one
querySelectorAll per rule. The selectors are now joined into one list
and tested once per element with a style attribute.

Chrome 157, later capture with a 1,200-rule <style>: -3.1 ms (-4%).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Without a property universe, as under an unreadable cross-origin
sheet, the full read walked each computed style by index, which in
Chrome costs about as much as reading the value. Every element lists
the same properties, so the read now goes by name through one list.

Chrome 157, first capture of 5,200 elements with a cross-origin font
sheet: -32.2 ms (-13%).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…ull scroll walk

Without the scroll gate, the memo's walk made up to six layout reads
per element. Chromium now checks computed overflow first, which rules
out most elements. Firefox reads layout faster, so it and Safari keep
the layout reads.

Chrome 157, first capture of 5,200 elements: -4.0 ms (-3%).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Without a pseudo universe, a pseudo-element's snapshot walked its
computed style by index, custom properties included. It now reads the
element snapshot's list by name.

Chrome 157, first capture of 300 items with ::before/::after icons and
a cross-origin font sheet: -39.0 ms (-21%).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…e next

Each capture that reached interning parsed every author <style> in the
clone for its selectors. The last capture's results are now kept by
style text, so only new text is parsed.

Chrome 157, later capture with a 1,200-rule <style>: -3.8 ms (-5%).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…content

A ::before/::after without content has no box, yet any rule on one
gated the probe, so the common `*, ::before, ::after` reset probed
every element twice. Only rules that set content, all, an animation or
a transition gate it now, and rules with nested rules. The SVG loses
some `data-snapdom-has-*` marks; the pixels are the same.

Chrome 157, first capture of 5,200 elements under a box-sizing reset:
-6.1 ms (-4%).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…walks

The safety scan before a memo capture already lists the open shadow
roots, yet captures walked the subtree for them again. A root attached
during a capture is now found by the next capture's scan, which
invalidates the memo as the walk did.

Chrome 157, first capture of 5,200 elements: -1.2 ms (-1%).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@tinchox5
tinchox5 merged commit 9001f8a into zumerlab:main Nov 18, 2026
tinchox5 added a commit that referenced this pull request Nov 18, 2026
Chromium 140 lists only the logical scroll margins, so the key kept scroll-margin-top.

ref #570
tinchox5 added a commit that referenced this pull request Nov 18, 2026
@tinchox5

Copy link
Copy Markdown
Member

Thank you!!! This is merged.

Here is a dev version with your changes: npm install @zumer/snapdom@dev (3.8.2-dev.0). The stable release will be published later.

tinchox5 added a commit that referenced this pull request Nov 18, 2026
Firefox 141 has no @scope and dispatches loadingdone after fonts.ready.

ref #570
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