Skip to content

t takes over a second on a page with ~100 code blocks: anchored copy controls make every forced layout ~40 ms #1839

Closed

Description

@max-sixty

In headless Chromium 126, on a page with about 100 <pre><code> blocks, pressing t (Next open thread) takes a median 1.0–1.6 s from keypress to paint, and a about 1 s. On a page with no code blocks and about 100 threads on distinct paragraphs, t takes 0.6 s. Leaf's proposed budget for a gesture's local result is under 100 ms (notes/user-feedback-responsiveness.md:45).

Two costs multiply. Every anchor-positioned box Leaf keeps in layout, chiefly one copy control per code block and one margin cluster per thread target, adds to the cost of every forced layout of the document. And one t press forces about 30 layouts.

The reported page follows the authoring guidance: a queue of items as one lf-tabs list="side" (skills/leaf/references/page-authoring.md:271) and snippets as <pre><code class="language-…"> (page-authoring.md:464). The authoring docs give no size limit.

Environment

  • Leaf 0ab6d4a and 99bdbb3. Both show the slowdown on the reported page; the generated pages were measured on 0ab6d4a only.
  • Measured in headless Chromium 126.0.6478.114 on Linux x86_64, viewport 1309×2162.
  • Reported in Chrome 143 on Linux, on a page with 26 queue items, 92 code blocks and about 30 threads. Leaf's interaction log stamps events at dispatch, and there each t keyup follows its keydown by 0.2–0.5 s. A keyup is dispatched only after the keydown task ends, so in Chrome 143 that task took at most 0.2–0.5 s, against a median 0.72–0.75 s here. I have not measured the copy-control effect in Chrome 143.

Reproduction

make_page.py generates a 26-item side-list queue with N code blocks per item and 12 comment threads. It needs leaf on PATH.

"""python make_page.py PAGE_DIR [CODE_BLOCKS_PER_ITEM]"""
import json, subprocess, sys
from pathlib import Path

page = Path(sys.argv[1])
code_per_item = int(sys.argv[2]) if len(sys.argv) > 2 else 4
ITEMS, THREADS = 26, 12
text = "The quick brown fox jumps over the lazy dog while the committee weighs the proposal. " * 3
code = "\n".join(f"def step_{j}(config, value):\n    return config.get('k{j}', value) * {j}" for j in range(6))

def item(i):
    rows = "".join(f"<tr><td>option {r}</td><td>{text[:40]}</td><td>{r * i}</td></tr>" for r in range(4))
    blocks = f'<pre><code class="language-python">{code}</code></pre>\n' * code_per_item
    return (f'<lf-tab id="t-{i}" label="Item {i}"><section id="item-{i}">'
            f"<h3>Item {i}</h3><p>{text}</p><p>{text}</p>"
            f"<table><thead><tr><th>Setting</th><th>Effect</th><th>Count</th></tr></thead>"
            f"<tbody>{rows}</tbody></table>\n{blocks}<p>{text}</p></section></lf-tab>")

html = f"""<!doctype html>
<html lang="en">
<head><title>Synthetic queue</title><meta name="description" content="Timing thread navigation."></head>
<body><main class="layout-wide">
<header id="top"><h1>Synthetic queue</h1><p class="lede">{ITEMS} items.</p></header>
<lf-tabs id="queue" list="side">{"".join(item(i) for i in range(1, ITEMS + 1))}</lf-tabs>
</main></body></html>
"""
subprocess.run(["leaf", "page", "init", str(page)], check=True)
(page / "index.html").write_text(html)
subprocess.run(["leaf", "page", "stamp", "--text", "generated", str(page)], check=True)
# User comments, appended in the shape a browser comment records.
with (page / "events.jsonl").open("a") as log:
    for k in range(THREADS):
        log.write(json.dumps({
            "kind": "comment", "revision": 1, "anchor": {"section": f"item-{1 + k * ITEMS // THREADS}"},
            "attempt": f"{k:032x}", "text": f"Question {k}?", "author": "user",
            "id": f"{0xA0000000 + k:08x}", "attention": True, "ts": "2026-10-05T09:00:00-07:00",
        }) + "\n")
  1. Generate the page and a control page without code blocks:

    python make_page.py queue 4
    python make_page.py queue-nocode 0
    
  2. Serve one with leaf server run --temporary queue and open its URL in a window about 1300 px wide.

  3. Record a DevTools Performance trace while pressing t a few times. Each press runs as two long tasks, the keydown and the next animation frame, together over a second and mostly Layout.

  4. Time one forced layout in the console:

    const d = document.body.appendChild(document.createElement("div"));
    d.style.cssText = "position:absolute;top:0;left:0";
    const t0 = performance.now();
    for (let i = 0; i < 20; i++) { d.textContent = i; d.getBoundingClientRect(); }
    console.log((performance.now() - t0) / 20, "ms per forced layout");

    It prints about 40 ms. After document.querySelectorAll(".lf-code-copy").forEach((c) => (c.style.display = "none")) it prints about 1 ms, as it does on queue-nocode.

Measurements

Headless Chromium 126, 10 presses per row. "Press to paint" is the Event Timing duration of the keydown, from the key event to the next paint. Rows 3, 4 and 6 come from variants of the script: 13 items; the items as plain sections instead of tabs; and plain sections with 104 threads on distinct paragraphs.

Page (build 0ab6d4a) t press to paint, median (max) One forced layout
26 items × 4 code blocks (104 copy controls) 1572 ms (2184) 40 ms
Same, copy controls set to display: none 244 ms (320) 1 ms
13 items × 4 code blocks (52 controls) 868 ms (912) 19 ms
26 items × 4 code blocks, sections without tabs (all 104 blocks visible) 1020 ms (1272) 40 ms
26 items × 0 code blocks 172 ms (240) 1 ms
26 sections × 0 code blocks, 104 threads on distinct paragraphs (104 margin clusters) 592 ms (712) 18 ms (1.7 ms with no threads)

On the reported page, t took 1272–1332 ms on 99bdbb3 and 1348–1380 ms on 0ab6d4a, and 300–408 ms with the copy controls hidden. a (Next Ask in 99bdbb3; Next waiting on you in 0ab6d4a) took 996 ms on 99bdbb3, and 304 ms with the controls hidden.

Traced on the reported page, each t press runs 29–31 Layout passes, almost all forced by script. Each pass takes 35–55 ms, and most mark only 3–20 of the document's ~2,300 layout objects dirty.

Cause

Anchored chrome stays in layout

watchCodeBlocks (skills/leaf/assets/runtime/code-copy.js:39–51) gives every document pre:has(> code) its own wa-copy-button.lf-code-copy, appended to the chrome root and anchored to the block with position-anchor (code-copy.js:34–35). lf-code blocks get the same control through copyCodeBlock (skills/leaf/packages/default/widgets/lf-code.js:168). The .lf-code-copy rule (skills/leaf/assets/shadow.css:833–845) makes it position: absolute with anchor() insets and opacity: 0. Every control stays in layout: invisible ones, and ones whose code block is in a hidden tab, which fall back to -9999px (shadow.css:835).

Margin clusters are anchored the same way (skills/leaf/packages/default/runtime/annotation-overlay/margin-layout.js:923). The layer they stand in is deliberately not their containing block, so that they can anchor to targets anywhere on the page (margin-layout.js:15–17). A cluster is withheld with display: none when its target does not render or an inner scroller has scrolled it out of view (margin-layout.js:921, skills/leaf/packages/default/runtime/annotation-overlay/annotation-theme.css:71). The page's own scroll does not count (margin-layout.js:278–290), so every cluster whose target renders in the document stays in layout, on screen or not.

On the generated page with all 104 code blocks visible, I restyled or replaced the 104 controls and timed one forced layout:

104 boxes in the chrome root One forced layout
Leaf's copy controls 40–45 ms
Leaf's copy controls with display: none or position: static 2 ms
Leaf's copy controls with contain: strict 38–40 ms
Plain absolutely positioned divs 2 ms
Plain absolutely positioned wa-copy-buttons, no anchoring 3 ms
Plain divs with position-anchor set, or anchor() insets 13–25 ms

Plain divs cost 13–25 ms once they use anchor positioning, against 2 ms without it. Leaf's controls cost about 40 ms, and still about 40 ms after their anchor properties were overridden, so part of their cost comes from something I have not isolated. Only taking them out of layout removed it. I have not traced this into Blink.

Forced layouts during a t press

The JS frames that forced layout during the presses, roughly in order of time:

  • measureThreadCard (skills/leaf/packages/default/runtime/annotation-overlay/margin-projection.js:620–633) writes --lf-thread-width, reads offsetHeight, writes --lf-thread-max-height, then reads getBoundingClientRect(). Floating UI's size middleware calls it from fit (margin-projection.js:763–783) on each middleware pass, about 3 times per press, forcing about 5 layouts.
  • The thread card is rebuilt on every step: the trace shows its nodes, including the reply composer's CodeMirror editor, removed (skills/leaf/assets/runtime/dom-children.js:5) and re-added. CodeMirror then measures the new editor (readSelectionRange and three other calls), about 6 forced layouts per press, and its scroll handling forces about 9 style recalculations.
  • shownParts and shownBox (skills/leaf/assets/runtime/geometry.js:421, :382), about 4 per press.
  • classify (skills/leaf/assets/runtime/reach.js:192), about 2 per press.
  • Single forced layouts from drawn (skills/leaf/assets/runtime/focus.js:230), syncBottomStatus (skills/leaf/assets/runtime/chrome-layout.js:144), renderShortcutBar (skills/leaf/assets/runtime/keyboard/shortcut-bar.js:347), and Floating UI.

The count of about 30 comes from the tabbed reported page; the flat generated page has the same per-layout cost but faster presses, so its walk likely forces fewer. At 1–2 ms per layout these passes cost 30–60 ms. With about 100 copy controls each pass costs about 40 ms, about 1.2 s per press.

Suggested fix

  1. Keep anchored chrome out of layout unless its target renders and is in or near the viewport, as one rule beside anchorName in skills/leaf/assets/runtime/anchor-names.js, which already names the anchors for copy controls, margin rows and floating surfaces. Copy controls and margin clusters would both use it. Clusters scroll with their targets on the compositor, so the rule needs a margin (an IntersectionObserver rootMargin, for example) to bring a box into layout before it scrolls into view. Offscreen clusters would also leave the Tab order until scrolled near, as withheld clusters in inner scrollers already do. Hiding the controls only on hover would leave touch and hover: none devices, where every control is visible (shadow.css:850–851), at the full cost. Measured: hiding all copy controls brought t from 1572 ms to 244 ms on the generated page; hiding only the controls for blocks in hidden tabs brought the reported page from about 1.3 s to 0.41 s.
  2. Cut the forced layouts in the t walk. Read the card's geometry once per frame before writing, and keep the reply composer mounted across thread steps instead of rebuilding it. Without copy controls a press still takes a median 170–410 ms on these pages, above the 100 ms budget.
  3. leaf-dev bench-latency times a comment, a card move, and an agent reply, status and revision (TRANSITIONS, dev/leaf_dev/bench_latency.py:71), not a thread step, on the triage board and corpus, which have few code blocks. A t transition on a source with about 100 code blocks would catch this; that source also needs the triage passage and cards the other transitions use (PASSAGE, MOVES).

I can test a patch against the generated pages.

Seen on leaf 0ab6d4a, installed 2026-10-05T08:26:55-07:00.

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

Activity

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

    @leaf-agent
    Collaborator

    I reproduced the hidden-source part of this bug: with 100 code blocks in a closed disclosure, all 100 copy controls still have layout boxes. #1840 preserves that finding as an intentionally failing browser test and checks that a copy control is available when the disclosure opens. It contains no runtime fix.

    The report's other costs—visible offscreen controls, margin clusters, and repeated forced layouts during thread navigation—remain unverified by this test. A repair needs to settle which shared layer governs anchored chrome's layout participation, then measure the thread walk on the reported page. I did not find a conservative change that handles those paths together in this triage run.

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