Skip to content

Draw preview capture visits every off-crop sibling, about 0.4 ms per block #2775

Closed

Description

@max-sixty

What happens

I hit this drawing a comment on a page of about 8,200 elements. After I released the stroke, the page's interaction log records nothing for about 13 seconds, and then the keys I typed in the meantime all arrive within 50 ms. On a long page, finishing a stroke in Draw mode freezes the tab. The time grows with
the length of the page, not with the size of the stroke or of the element drawn on. At
about 7,000 elements, releasing one 600px stroke inside a single card blocks the main
thread for 5.6 s in one long task, and for about 8.5 s in total. At about 16,000
elements, the second stroke's release blocked for 13.4 s in one task.

The stroke itself is fine. Pointer moves stay at one frame (median 16.6 ms) at every
size I tried, and only the release is slow.

Expected

A stroke on one card should cost about the same whatever the length of the page. The
preview's own docstring says it should: drawing-context.js says it captures "the
painted island that contains the crop, rather than making every widget and offscreen
image in a long page participate".

Repro

  1. Make a page with N cards in one <section> inside <main class="layout-column">,
    each card like this, with 40 short lines in the code block:

    <article class="card" id="card-0">
      <h3>Card 0</h3>
      <p>Card 0 summary: alpha bravo charlie delta echo foxtrot golf hotel.</p>
      <details open><summary>Transcript 0</summary><pre><code>00 alpha bravo charlie delta echo foxtrot golf hotel
    01 bravo charlie delta echo foxtrot golf hotel india
    …</code></pre></details>
    </article>
  2. Serve it, scroll a card in the middle to the top, press w, and draw a stroke 600px
    straight down inside that card's code block. Then release.

  3. Draw a second stroke beside it with the composer open, and release.

I measured on aa1d84c. drawing-context.js and margin-layout.js are unchanged in e16097e.

I drove this with Playwright in headless Chromium: 60 pointermoves per stroke, with
long tasks taken from a PerformanceObserver and self/inclusive time from a CDP CPU
profile sampled every 200 µs. Each release phase covers the release plus 3 s.

Measurements

cards elements move median / max longest task, release 1 longest task, release 2 restingRail, release 1 one forced layout
50 768 16.6 / 44 ms 407 ms 354 ms 80 ms 35 ms
200 1,968 16.6 / 86 ms 1,418 ms 1,310 ms 278 ms 122 ms
800 6,768 16.6 / 259 ms 5,652 ms 5,056 ms 932–1,067 ms 477 ms
2,000 16,368 17.1 / 905 ms 3,181 ms (more spilled past the window) 13,420 ms 1,663 ms 1,317 ms

"One forced layout" is the median of appending a div to body, reading
offsetWidth, removing it, and reading body.offsetWidth. The same markup opened as
a plain file without Leaf takes about 0 ms for this at 800 and at 2,000 cards.

The 800-card release profile has about 9 s of samples, almost none of them idle. Three
things account for most of it:

  • About 3.9 s in the drawing preview (drawingThumbnail → the bundled
    modern-screenshot 4.7.0 in vendor/drawing-context.esm.js). Its self time is in
    getPropertyValue, setProperty, CSSStyleDeclaration.item and
    serializeToString, which means it copies computed style element by element. I
    counted getComputedStyle calls after pointerup. At 800 cards the release read
    computed style for 5,631 of the page's 6,767 elements, in all 800 cards. At 50 cards
    it read 382 elements, again in every card. So the clone takes in the whole page, not
    the card under the ink. No ancestor of the card has a background-image, so the
    backgroundImage climb in contextSource doesn't explain it. I haven't found which
    step widens the source.
  • About 2.1 s in placement passes (runWork → pass). Of that, about 1.8 s is
    placeFab → commentAttachment, and about half of that is marginSpot. When the
    page has no rail row yet,
    marginSpot calls restingRail, which appends a measuring row, reads offsetWidth
    and removes the row again. Under Leaf each mutation like that makes the next geometry
    read pay a whole-page pass, the 477 ms column above. So restingRail costs about 1 s
    of self time, and the getBoundingClientRect calls after it (anchorFor,
    floatingGeometry and others) cost another 1.1 s.
  • About 0.5 s in syncLayout → syncFloats (chrome-layout.js).

restingRail was also the largest script cost in a page-load profile of a similar long
page, which points to the same mechanism: each call re-measures a row that it
inserts and removes, on a page where every insertion forces a full relayout.

Possible directions: confine the preview's clone to the island contextSource means to
pick, or render it off the critical path; cache restingRail's measurement, which
depends only on the cascade and not on the content; and find why a single insertion
under body restyles or relayouts the whole page when Leaf serves it, since that makes
every measure-by-inserting step O(page). I haven't split style from layout in that
cost.

Seen on leaf aa1d84c.

This was written by Claude Code on behalf of max-sixty

Activity

  1. added
    bug
    on Oct 13, 2026
  2. leaf-agent commented on Oct 13, 2026

    @leaf-agent
    Collaborator

    I reproduced page-wide work on stroke release. #2778 preserves a failing browser test: with the preview renderer held, releasing a stroke in one card reads computed styles in all 40 cards. That places at least one broad walk before preview capture starts. The test does not yet resolve the separate screenshot and rail-layout costs, so the issue remains open for a fix at those boundaries.

  3. changed the title [-]Releasing a Draw-mode stroke on a long page blocks the main thread for seconds: the preview clones the whole page and restingRail forces full relayouts[/-] [+]Draw preview capture visits every off-crop sibling, about 0.4 ms per block[/+] on Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions