Skip to content

Load pages of many text documents in Firefox without restyling the page per document - #5226

Merged
max-sixty merged 3 commits into
mainfrom
grind/firefox-empty-documents
Nov 3, 2026
Merged

max-sixty merged 3 commits into
mainfrom
grind/firefox-empty-documents

Conversation

@max-sixty

@max-sixty max-sixty commented Nov 3, 2026 •

Copy link
Copy Markdown
Owner

A page with one 4 MB lf-text-document and 29 empty ones in side tabs took 7.5–10.4 s to settle in Firefox, against 1.4–2.1 s with the empty documents removed. Every text document now draws from one shared copy of Pierre's stylesheet, and the same page settles in 1.6–2.4 s.

Cause

The Firefox Profiler (MOZ_PROFILER_STARTUP) put 5.7 s of the 7.7 s load in refit → lineColumns → Element.clientWidth, each read forcing a style flush. The profiler's Styles markers showed about 30 flushes of about 36,000 elements each: the whole page, rows inside the long document's skipped content-visibility: auto runs included.

Pierre's static renderer writes two <style> elements into every file it renders, and lf-text-document kept them in each listing, wrapped in an implicit @scope. Firefox 157 restyles the whole document whenever a <style> holding @scope enters or leaves it. Timed in the settled page, inserting one such <style> that matched nothing cost a 110–370 ms flush, against 2–9 ms for the same rule without @scope. So each document that drew restyled every element of the page, and each restyle cost more as copies piled up (about 370 ms with 60 copies on the page, 110 ms with one). Chromium 156 showed only 0.7 s against 0.4 s; it likely skips styling inside skipped content-visibility boxes, which wasn't measured.

Change

sourceListing now takes Pierre's <style> elements out of the rendered listing and builds one CSSStyleSheet from the first listing's text, scoped by @scope (.lf-text-document-listing) in place of the implicit scope. show adopts that sheet into the document or shadow root the widget stands in, once. Pierre's sheets depend only on the syntax theme and the options sourceListing passes, so every listing on a page gets the same text. Each listing also no longer carries its own 32 kB of CSS in the DOM.

Evidence

Built with build.py from #4781's re-measure (30 lf-text-documents in lf-tabs list="side", d0 bound to a 4 MB source, d1–d29 bound to sources with no data; the control is the single bound document), exported, and loaded in headless Firefox 157 by a script that waits for the tab list to be visible, #d0 code to show text, and body[data-lf-data-version]. Base and fix exports alternated on the same machine, three rounds:

settled, base settled, fix
30 documents, 29 empty 7.91, 7.95, 7.79 s 1.96, 2.17, 1.91 s
1 document (control) 2.08, 1.82, 1.69 s 1.88, 1.83, 1.63 s

The long document's text first showed at 7.56–7.86 s on base against 1.69–2.16 s on the fix, so the empty documents held its first draw back about 6 s. The tab list's first appearance varied with scheduling in both versions, at about 0.3 s or with that first draw, so it isn't the measure here. In Chromium 156 the 30-document page settled in 0.72 and 0.77 s on base against 0.50 and 0.49 s on the fix, with peak renderer memory falling from 606–617 MB to 430 MB.

After merging main, whose #5223 and #5241 changed this widget's drawing, two more alternated Firefox rounds on a busier machine settled the 30-document page in 9.84 and 9.36 s on main against 2.21 and 2.08 s on this branch.

The computed value of every property, on the first 400 elements of the bound listing and every element of an empty one, is identical before and after the change, in Chromium and in Firefox once its font list has loaded.

Tests

  • test_every_document_draws_from_one_copy_of_pierres_sheet (new): three documents in tabs, one with text and two bound to empty sources. The page holds one copy of Pierre's sheet, and each document's first line number stands beside its line. It fails on main with three copies. With the adoption removed it fails with no copy, and with the count check skipped the line numbers stand 60 px above their lines.
  • Ran tests/browser/widgets/test_code_and_documents.py, the text-document tests in test_render_gate.py, test_render_export.py, test_rings.py, threads/test_targets.py, startup/test_projected_data.py, test_workspaces_and_bounds.py and projection/test_code_review.py (all passed), tests/runtime/text-document-lines.test.mjs, the browser type check (tsc --project browser/types/tsconfig.json), and leaf page check --render on a five-document page.

No cost test: the load time depends on the machine and engine, and the new test holds the mechanism, one sheet per page, instead.

Not verified

  • Safari/WebKit, and Firefox on Linux or Windows.
  • A document moved into another shadow root after it drew keeps drawing without Pierre's sheet until its text changes, since adoption happens when it draws. No Leaf path that moves a drawn document across roots turned up, but none was searched for exhaustively.

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

max-sixty and others added 2 commits November 3, 2026 10:35
…ge per document

A page with one 4 MB `lf-text-document` and 29 empty ones in side tabs took 7.5–10.4 s to settle in Firefox, against 1.4–2.1 s with the empty documents removed. Every text document now draws from one shared copy of Pierre's stylesheet, and the same page settles in 1.6–2.4 s.

## Cause

The Firefox Profiler (`MOZ_PROFILER_STARTUP`) put 5.7 s of the 7.7 s load in `refit` → `lineColumns` → `Element.clientWidth`, each read forcing a style flush. The profiler's Styles markers showed about 30 flushes of about 36,000 elements each: the whole page, rows inside the long document's skipped `content-visibility: auto` runs included.

Pierre's static renderer writes two `<style>` elements into every file it renders, and `lf-text-document` kept them in each listing, wrapped in an implicit `@scope`. Firefox 157 restyles the whole document whenever a `<style>` holding `@scope` enters or leaves it. Timed in the settled page, inserting one such `<style>` that matched nothing cost a 110–370 ms flush, against 2–9 ms for the same rule without `@scope`. So each document that drew restyled every element of the page, and each restyle cost more as copies piled up (about 370 ms with 60 copies on the page, 110 ms with one). Chromium 156 showed only 0.7 s against 0.4 s; it likely skips styling inside skipped `content-visibility` boxes, which wasn't measured.

## Change

`sourceListing` now takes Pierre's `<style>` elements out of the rendered listing and builds one `CSSStyleSheet` from the first listing's text, scoped by `@scope (.lf-text-document-listing)` in place of the implicit scope. `show` adopts that sheet into the document or shadow root the widget stands in, once. Pierre's sheets depend only on the syntax theme and the options `sourceListing` passes, so every listing on a page gets the same text. Each listing also no longer carries its own 32 kB of CSS in the DOM.

The new entry in `notes/browser-support.md` records the Firefox behaviour, with a recheck.

## Evidence

Built with `build.py` from #4781's re-measure (30 `lf-text-document`s in `lf-tabs list="side"`, `d0` bound to a 4 MB source, `d1`–`d29` bound to sources with no data; the control is the single bound document), exported, and loaded in headless Firefox 157 by a script that waits for the tab list to be visible, `#d0 code` to show text, and `body[data-lf-data-version]`. Base and fix exports alternated on the same machine, three rounds:

| | settled, base | settled, fix |
| --- | --- | --- |
| 30 documents, 29 empty | 7.91, 7.95, 7.79 s | 1.96, 2.17, 1.91 s |
| 1 document (control) | 2.08, 1.82, 1.69 s | 1.88, 1.83, 1.63 s |

In these runs the tab list showed together with the long document's first draw: 7.55–7.82 s on base, 1.68–1.95 s on the fix. In other runs of either version it showed at 0.3 s, before that draw. In Chromium 156 the 30-document page settled in 0.72 and 0.77 s on base against 0.50 and 0.49 s on the fix, with peak renderer memory falling from 606–617 MB to 430 MB.

The computed value of every property, on the first 400 elements of the bound listing and every element of an empty one, is identical before and after the change, in Chromium and in Firefox once its font list has loaded.

## Tests

- `test_every_document_draws_from_one_copy_of_pierres_sheet` (new): three documents in tabs, one with text and two bound to empty sources. The page holds one copy of Pierre's sheet, and each document's first line number stands beside its line. It fails on main with three copies. With the adoption removed it fails with no copy, and with the count check skipped the line numbers stand 60 px above their lines.
- Ran `tests/browser/widgets/test_code_and_documents.py`, the text-document tests in `test_render_gate.py`, `test_render_export.py`, `test_rings.py`, `threads/test_targets.py`, `startup/test_projected_data.py`, `test_workspaces_and_bounds.py` and `projection/test_code_review.py` (all passed), `tests/runtime/text-document-lines.test.mjs`, the browser type check (`tsc --project browser/types/tsconfig.json`), and `leaf page check --render` on a five-document page.

No cost test: the load time depends on the machine and engine, and the new test holds the mechanism, one sheet per page, instead.

## Not verified

- Safari/WebKit, and Firefox on Linux or Windows.
- A document moved into another shadow root after it drew keeps drawing without Pierre's sheet until its text changes, since adoption happens when it draws. No Leaf path that moves a drawn document across roots turned up, but none was searched for exhaustively.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The notes/browser-support.md entry recorded a reason the widget's own comment already
holds, and sharing one sheet is right in every engine, so no recheck could lead to
removing code. Its recheck also misled: any element appended to body restyles a
long-document page in Firefox, with or without @scope.

The test's docstring said the empty documents held the tab list back; on base the tab
list can show at about 0.3 s, and what they delayed is the long document's first draw.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@max-sixty
max-sixty marked this pull request as ready for review November 3, 2026 20:58
leaf-agent
leaf-agent previously approved these changes Nov 3, 2026
# Conflicts:
#	packages/default/widgets/lf-text-document.js
@max-sixty
max-sixty merged commit be6f5bf into main Nov 3, 2026
4 checks passed
@max-sixty
max-sixty deleted the grind/firefox-empty-documents branch November 3, 2026 21:24
max-sixty added a commit that referenced this pull request Nov 4, 2026
… a page's CSS can't name (#5244)

Leaf wrote 84 data attributes on its own elements outside the `lf-`
namespace that `page check` reserves for it: the floating panel's state
(`data-open`, `data-rest`, `data-shown`, `data-expanded`, `data-tone`
and two dozen more), a thread's and a message's identity (`data-thread`,
`data-event`, `data-message`, `data-attempt`), the paint boxes'
`data-for`, the composer's `data-words` and send corner, and the parts
the bundled widgets draw: lf-code's line numbers and elided rows,
lf-tabs' scroll buttons, the trace timeline's marks, the timeline's
events, the deck's pips, the call diff's lines and the diff's file rows.
It also wrote the `scroll` class on its own scroll surfaces. A page's
own CSS for one of those names restyled Leaf. On a probe page whose
`<style>` held `[data-tone] { --warn: red }`, `[data-event] {
--lf-msg-text-end: 123px }`, `[data-line] { background: blue }` and
`.scroll { outline: 4px solid magenta }`, main painted the panel's away
leaf red (rgb(255, 0, 0)), gave a margin card's messages a 123px text
end, painted lf-code's lines blue (rgb(0, 0, 255)) over its highlight,
and outlined lf-tabs' strip and panel, lf-code's scroller and
lf-options' "Add another option" field in magenta (4px rgb(255, 0,
255)). The chrome's `all: revert-layer` guard stops a plain declaration
but not a custom property, and a widget's parts in the page have no
guard at all.

Each is now the same name under `data-lf-`, its values unchanged, except
where that name already meant something a document-wide reader or an
unscoped rule relies on:

- the panel view's `data-view` became `data-lf-route`, since prepaint.js
sweeps `[data-lf-view]`, a revision's view members;
- the panel status's `data-held` became `data-lf-held-lines`, since
thread/presentation.js strips `data-lf-held` from every node no thread
holds;
- an On me item's `data-obligation` became `data-lf-obligation-id`,
since obligations/view.js sweeps `data-lf-obligation`, the location
mark, and chrome.css gives every `[data-lf-obligation]` a landing
margin;
- the composer field's `data-scrolls` became `data-lf-overflows`, the
name the margin card's transcript already gives the same fact, since
reach.js writes `data-lf-scrolls` on that field too.

Where the prefixed name was already in use for the same kind of fact on
other elements (`data-lf-tone`, `data-lf-state`, `data-lf-open`,
`data-lf-unread`, `data-lf-place`, `data-lf-ready`, `data-lf-line`),
every selector and query names its element, so the uses share a word
without meeting. The video bar's `data-hovered` and `data-keyed`, built
from one template, are `data-lf-hovered` and `data-lf-keyed`.

The `scroll` class stays the page's: page-authoring.md tells an author
to add `class="scroll"` to a custom horizontal scroll box, and `page
check` would refuse `data-lf-scroll` in markup. Leaf's own surfaces, and
a package module's (module-scroll-and-motion.md now says so, as it
already did for `data-lf-track`), wear `data-lf-scroll`, and reach.js
and the shared containment rule in controls.css read both. Where the
runtime marks an element the page owns (a code block, a part beside the
Layout's views, a bounded block's body), the mark joins
`RUNTIME_PAINT_ATTRIBUTE`, so a reading of the author's attributes looks
past it, as it did past `class`.

Kept as they are: the attributes an author writes (`data-width`,
`data-height`, `data-bound`, `data-sample*`, `data-annotations`,
`data-rail`, and `data-room`, which Leaf's own controls also use, one
rule for both), the playground's `data-playground-*` on its host, which
the author's preview CSS reads by design, Pierre's attributes on the DOM
it renders (`data-line`, `data-line-type`, `data-code`, `data-content`,
`data-separator` and the rest), including the ones lf-diff copies onto
its own rows and `pre` so Pierre's vocabulary keeps reading them, and
the diagram's `data-id`, which pele writes.

On the same probe this change shows Leaf's own styling: the away leaf in
the theme's warn (rgb(180, 116, 26)), the message's own 13px text end,
lf-code's lines unpainted but for the highlight, and no outline on any
widget part. Nothing changes on a page that doesn't target the old
names, and specificity is unchanged: each `[data-x]` became
`[data-lf-x]`, and `:where(.scroll)` became `:where(.scroll,
[data-lf-scroll])`.

Tests and dev tools read the new names. Attributes a test gives its own
fixture, which only share a word with Leaf's, stay: the `data-row` key
test_focus.py, focus.test.mjs and an application-boundary widget hand to
`holdFocus`, a test widget's `data-words` and `data-ready`, the shift
watch test's `data-key` on `body`, the write watch test's `data-for`,
and the examples' own `data-state` and `data-status`. The shift and
words watches' expected node paths lose the `.scroll` class
(`leaf-text#field`, `lf-diff#patch.lf-rendered`), and reach.test.mjs
asks a native code block for `data-lf-scroll`.

### Checks

- Main is merged in, and with it three new readers of the old names,
which now read `data-lf-obligation-id` (the On card selector,
`ON_CARD`), `data-lf-key` (the queue walk), `data-lf-event` (undo's
message lookup) and `data-lf-number` (a text document's line number, in
#5226's lf-text-document code and test). #5249's change bars write
theirs as `data-lf-for`, `data-lf-gap` and `data-lf-kind`, the stills
drivers that moved to `dev/leaf_dev/states.py` in #5259 read the new
names, as do #5263's fold test (`data-lf-says`, `data-lf-rest`) and
#5264's dev browser helper (`data-lf-open`).
- The pull request's `test` job: its failures outside the journeys fail
on main too; of the journeys,
`test_the_feature_gallery_exercises_its_core_journeys` failed here
because the test read the gallery's own preset buttons as
`data-lf-route`, and now reads the author's `data-view`. The
pressed-controls sweep's 28 cases,
`test_an_aimed_comment_keeps_its_place_with_on_me_open`, and the
navigation, controls and website-server journeys that failed fail the
same way on main.

- `pnpm run check` and `pnpm run test:runtime` (753 pass), pre-commit
over the changed files, and the kernel rebuilt with `pnpm run
build:browser`.
- 67 browser tests, by node id, that read the renamed attributes: the
floating panel's status peek, passing notices, versions, menu lists and
Threads' New; thread summaries, attempts, failure receipts and message
obligations; reactions; the send corner; the shift and words watches'
field paths; reach for an authored `.scroll` box; the trace timeline and
overview; timeline steps; lf-code excerpts and text-document numbers;
the diff's cut path; the call diff; overflowing tabs; On me cards; rings
and aim; a board's lifecycle; the recording bar; the smoke tier's three
changed tests; and read state.
`test_a_pointer_folds_the_panel_from_its_corner` fails the same way on
main: the corner button calls `foldPanel` directly, so no
`lf-command-invoked` arrives for the test to read.
- `leaf-dev stills` over the 61 catalogue states that draw the renamed
attributes found no computed style that differs; the style lists only
re-key the elements that lost the `scroll` class. Stills differ only
where every run differs: About's fingerprint and port, a caret's blink,
an overlay scrollbar's fade, and a target trace one arm caught
mid-transition, in the other arm on a second run. The stills drivers
name the new attributes, so for this comparison 21 states drove the
merge base's runtime through a local patch that accepted either name.

> _This was written by Claude Code on behalf of max-sixty_
max-sixty added a commit that referenced this pull request Nov 4, 2026
… Find's reach (#5314)

A page of large text documents now shows the open one at once, and every
document's text, a closed tab's included, stays in the page for the
browser's Find. `lf-text-document` holds each document as plain rows,
one a line, and paints syntax colour only on the runs of lines the
browser draws and their neighbours, as CSS custom highlights rather than
spans. This builds the option chosen for "Must browser Find reach text
in tabs nobody has opened?": keep Find reaching every tab, with plain
rows.

## Before and after

The page is #4781's repro, exported: an `lf-tabs list="side"` of 30
tabs, each an `lf-text-document` bound to its own source, with a word at
the top and foot of each source for Find. `all` holds 4 MB a source in
lines of 300–600 words; `short` holds the same 4 MB in lines of about 80
characters, 1.5 million lines in all; `md` holds 400 KB a source of
80-character Markdown with `language="markdown"`, so the colouring is
measured too. Main's exports (40e8104) beside this branch's at its
head, the arms alternated on the same host, whose one-minute load stood
at 20–45 for this final run and up to 100 in earlier ones from other
sessions, so compare within a row.

- **Open document shows**: the first animation frame in which the open
tab's listing holds its text and is visible.
- **All drawn**: the first frame in which all 30 listings are built.
- **Longest frame gap**: the longest the main thread went without a
frame between navigation and the last listing.
- **Peak RSS**: the largest resident size of any process the measuring
script's own browser ran (its renderer or content process).

| Page | Engine | Open document shows | All drawn | Longest frame gap |
Peak RSS |
| --- | --- | --- | --- | --- | --- |
| `all` | Chromium 156 | 4.55 s → **0.56 s** | 4.55 s → 1.01 s | 4.03 s
→ 0.23 s | 2.69 → **1.20 GB** |
| `short` | Chromium 156 | 22.9 s → **0.67 s** | 22.9 s → 3.04 s | 22.4
s → 0.27 s | 8.59 → **2.16 GB** |
| `md` | Chromium 156 | 3.01 s → **0.35 s** | 3.01 s → 0.64 s | 2.83 s →
0.11 s | 1.83 → **0.48 GB** |
| `all` | Firefox 157 | 25.0 s → **0.85 s** | 25.0 s → 3.31 s | 24.2 s →
0.34 s | 9.92 → **2.49 GB** |
| `md` | Firefox 157 | 15.2 s → **0.52 s** | 15.2 s → 2.75 s | 14.9 s →
0.33 s | 7.92 → **1.65 GB** |
| `short` | Firefox 157 | 217 s → **1.64 s** | 217 s → 24.4 s | 216 s →
3.15 s | 17.3 → **13.2 GB** |

Firefox `short` is from an earlier round of this branch, before the
review's fixes, which change nothing on a page with no `language`; main
took 3.6 minutes to show its open document there. Chromium is
Playwright's headless shell 156.0.8078.4; Firefox is Playwright's build
1555.

On main the page draws nothing between the side list and the moment
every document is built, so the open document waits on all 30. Here the
document the page shows draws in the first task, and each hidden one
takes a task of its own after it, so the page paints between them and
stays answerable while the closed tabs fill in. Reloading on the 30th
tab of `short`, which tab memory restores, shows its document at 0.64 s
in Chromium and 2.44 s in Firefox, where drawn in page order it took 2.8
s and 33.4 s.

**Find.** In Firefox 157, `window.find` reaches a word only the last,
unopened tab's document holds, selects it there, and opens that tab, on
this branch as on main. Chromium's `window.find` searches no
`hidden="until-found"` content on either arm, so there the new test
asserts the word stands in the closed tab's listing; its find bar wasn't
driven.

**A jump far down a long document.** On one 4 MB Python document (757
runs), End scrolls the last run into view, and it is coloured two frames
later (40 ms in Chromium, 66 ms in Firefox), painted from a fresh
grammar state; main colours it in its first frame. It is painted again
with its exact state once the runs before it have been tokenized, a
slice at a time. After End, Chromium drew no frame over 51 ms on that
document or on a 2 MB one that opens with a long docstring. In Firefox
the colouring no longer holds a frame: each token colour's
`::highlight()` rule was inserted into a sheet the first time it
appeared, and Firefox restyled the whole page for each, 0.85 s a rule,
so End's scroll appeared 1.5–1.9 s after the press. The rules are now
declared in the stylesheet. Firefox still drew one frame of 0.40–0.49 s
after End on both documents (two runs each, a browser warmed by an
earlier load of the page); an uncoloured document does the same, so it
is below under the follow-ups.

**Growth.** `leaf-dev scaling document --base` (a new axis: one ask and
a text document of 64·n lines of Python, n = 8–256, so up to 16,384
lines; Chromium main-thread CPU, against ecf4dc1): main's load went
262 → 1,018 ms and a click on the ask 28 → 99 ms, an exponent of 0.88,
above the 0.5 an input is held to; here load goes 213 → 283 ms (exponent
1.25, under the 1.5 for superlinear), a click 26 → 33 ms (too little
growth to fit) and a wheel step 3.3 → 4.9 ms. The axis joins the daily
`test_scaling.py`. `leaf-dev scaling` failed on every axis on current
main (`no median for empty data`): since #5287 a page loads more than
250 modules, which fill resource timing's buffer before the `/api/event`
answers it reads, so the tab now raises the buffer before loading the
page; the daily test shares that `measure`.

## Calls for you

These are what the change costs a reader, each kept on purpose:

- **Comments in a text document stand upright.** `::highlight()` carries
only properties that don't change layout, so a token keeps its colour
but not its font style: a comment or docstring, italic in main's text
documents, is upright here, while `lf-code` and diffs keep their italic.
`notes/browser-support.md`, "Font style in syntax highlights", records
it. Still pairs (main | branch), Chromium light, dark, and Firefox:
`~/.tmp/leaf.agent-a6a4236750c25a2da/long-source-selected.png`,
`long-source-selected-dark.png`, `long-source-selected-firefox.png`;
each pair differs only there.
- **A far jump is coloured two frames late, and may read wrong at
first.** As above: the run shows plain for two frames, then from a fresh
state, so a construct open across its first line, such as a docstring,
reads wrong until its exact state arrives.
- **A printed long document colours only the runs that were near the
screen.** Chromium prints highlights (checked in a PDF), but runs never
near the view were never tokenized. A document of 128 lines or fewer is
one run and prints coloured, as before.
- **The render gate's contrast check doesn't see document tokens.**
`unread-syntax` reads token spans, and a text document has none; the
same palette is still checked wherever code blocks or `lf-code` stand.

## What changed in the widget's contract

- **Rows.** `listing` builds a `pre > code` of runs of 128 rows; a row
is a `div` holding one line's text and its ending, exactly as the source
has them, so the listing's text is the source. Pierre no longer renders
documents: no gutter cells, `data-line*` attributes or token spans, and
no Pierre sheet, so `listingSheet`, `scopedSheet` and #5226's
shared-sheet adoption go. `pierre.js` and the Pierre bundle stay for
`lf-diff`.
- **Numbers.** A row's number is `::before` generated content, a CSS
counter each run starts on its first row from `--lf-lines-before`, with
empty alt text, so selection, copy and screen readers never take it. A
counter started on the run's own box didn't reach its rows: a skipped
run's style containment scoped it, and every run after the first
numbered from 1. The first stills showed it, and
`test_a_text_document_update_keeps_a_selection_it_carries_into_another_run`
now reads the drawn numbers off Chromium's layout tree; it fails with
the count on the run's box. A folded panel's rows
(`hidden="until-found"`) count nothing until it opens: Firefox styles
and builds the generated content of rows it doesn't draw, about 120 of
the 150 ms a folded 70,000-line document cost it.
- **Colour.** `RunColours` tokenizes the runs the browser draws
(`contentvisibilityautostatechange`, or at once for a lone run) and the
run on either side of each, while the listing has a box, and paints each
token as a `StaticRange` in one `Highlight` per token colour, named by
the theme's colour variable (`lf-token-syn-keyword` and so on).
`text-document.css` declares the `::highlight()` rule for each, and
`tests/runtime/text-document-colours.test.mjs` holds that list to the
token theme's, so painting never adds a rule. A run that leaves that set
drops its ranges; each highlight it touches is rebuilt from the ranges
it keeps, since Firefox finds a range to delete by walking the
highlight's list, and deleting two runs' ranges one by one took over 100
ms of a frame. Firefox reports a closed tab's runs drawn, and coloured
every document in a closed tab as the page loaded, so a listing with no
box takes no colour. Painted only as it was drawn, a run scrolling into
view showed one uncoloured frame in Firefox; with its neighbours
painted, a wheel scroll through a document showed none in either engine.
A run starts from the grammar state the run before it ended in, so a run
beginning inside a docstring or a fenced block is coloured as the whole
text is; a run whose state isn't known yet is painted from a fresh state
and repainted once `walk` finds it. A new text keeps the paint and
states before its first changed line. Tokens come from the highlighter
the page's code blocks and `lf-code` share, so a Markdown fence's
language is coloured where another block on the page has loaded its
grammar, as in `lf-code`; main's text documents left fences flat.
`syntax(source, lang, state)` in the portable API now resolves to
`{tokens, state}` and continues from a state it's given
(`module-reading.md`); `lf-code`, the page's code blocks and
`test_render_anchors.py` take `.tokens`.
- **First draw.** A document the page shows draws without waiting; a
hidden one first draws in a task of its own. The runs are first sized by
the listing's size observer rather than in the draw: read in each draw,
every closed tab's listing was a read of the page's layout, which
Chromium answered more slowly for each document already drawn, 0.8 s
over the 30 documents of `short`.
- **Kept as they were.** Runs and `content-visibility: auto`, `sizeRuns`
and its forgetting, `reconcileRuns` and the selection it carries across
runs, the runtime's `drawForLanding`/`skippableBox` (untouched), the
bounded viewport, `holdSource` and `cut`, the previews, and the datum
marks comments anchor to.

Line numbers, selection, the selected text and copy read the same as on
main in light and dark, in Chromium and Firefox: `leaf-dev stills` on
`captured-source` and a new state, `long-source-selected`, which selects
across the edge of two runs in the PR walkthrough's Rust capture. A
selection across rows reads exactly the source's characters on both arms
in both engines. A comment on a line deep in a long document stands its
pin at the same offset from its line on both arms in both engines, moves
with a scroll of the listing, and hides once the line scrolls out of it;
the comment-follows-its-lines test (#5223) passes in both. An
independent review confirmed these, `t` travel and a live data update in
both engines and schemes.

## Checks run

- On the head: `pnpm run test:runtime` (778 pass, the new colour-rule
test among them, which fails with a rule renamed), `pnpm run
check:browser`, `pnpm run test:browser`, pre-commit on the changed
files; `uv run pytest` on `test_render_anchors.py`,
`test_code_and_documents.py`, `test_projected_data.py`,
`test_targets.py`, `test_code_review.py`, `test_render_export.py`,
`test_render_gate.py` and `test_render_code_reading.py` `-k "shell or
syntax or document or find or captured_source or long_quote_of_replaced
or source_replacement_preserves or keeps_comments_on_their_lines or
interactive_export or language or colo or highlight or token or lf_code
or excerpt or code_line"` (51 pass, Chromium and Firefox where
parametrized,
`test_shell_commands_share_colors_in_plain_code_widgets_and_diffs` among
them); and, before this last change, the two thread tests that read
message code colour.
- `test_find_reaches_the_text_of_a_document_in_a_tab_nobody_opened` now
asserts the opened document's lines carry `lf-token-*` ranges and the
unopened one's carry none; with `RunColours.follow` a no-op it fails in
both engines.
- Earlier, before the merges: the bounds, focus-ring and
interactive-export tests that hold text documents.
`test_code_wrapping_keeps_source_copy_and_reader_choice` failed the same
way on main then (a tap on the shortcut list's Copy code timed out).
- The measuring scripts (`build.py`, `build_code.py`, `frames.py`,
`memory.py`, `endframes.py`, `lasttab.py`, `find_test.py`, `ffinval.py`)
and probes stay in the worktree's `.tmp/perf/` and `.tmp/js/`.

## Not verified

- Printing in Firefox and WebKit: only Chromium's PDF was read.
- Chromium's find bar on a closed tab: `window.find` there searches no
fold.
- WebKit: the Highlight API check ran in Playwright's WebKit 27.2
(colour, `StaticRange`, shadow root), but no text-document test or still
did.
- The daily `test_scaling.py` with the new `document` axis and the
buffer fix was not run as the daily suite runs it, only `leaf-dev
scaling document --base` and `leaf-dev scaling asks --sizes 32`.
- In Chromium, a document whose tab the reader closes keeps the colour
of the runs it last drew, since Chromium reports neither a skipped run
nor a size for a closed tab's listing; a handful of runs per document
opened.

## Found along the way, not changed here

- **Firefox restyles the whole page on an insertion near its top.**
Appending a `<p>` to `main` on the `all` page costs Firefox 157 a 2.6 s
style flush on main and about 0.27 s here, and 6–16 s on `short`;
Chromium takes 7 ms. A rule-by-rule bisect of the page's stylesheets
found no single rule behind more than 15% of it. A Gecko profile of
`short`'s load spent most of its time in style flushes that the
runtime's visibility reads force (`skipped` in `shown-box.js`, from a
frame callback and a mutation observer), which I take to be this
restyle, not shown; any interaction that inserts near the top of a large
page would pay it too.
- **Two runtime passes walk every element a widget draws.** On `short`
in Chromium, keyboard-scope marking (`markScopedIn`,
`keyboard/scopes.js`) took about 1.0 s and the `watchArrivals("*")`
owner watch (`arrivals.js`) about 0.8 s of the 3.2 s of main-thread CPU,
walking 1.5 million rows that hold no scope and no owner.
- **Firefox restyles every row once, about half a second on 4 MB.** On
the 4 MB Python document and the 2 MB docstring one, a Firefox profile
shows two style passes of 194,538 elements each (every row and its
number), with no selector matching, so a recascade from above. In a
browser warmed by an earlier load one lands after End (0.40–0.49 s); in
a freshly launched one, two land about 6 s after load with no input at
all (0.39–0.64 s each), at the moment `sizeRuns` resizes every run as
the face arrives. An uncoloured document does the same; main's builds of
these documents draw no frame over about 115 ms. Not the cause, each
tried: the inherited `--lf-lines-before` on every run (made
non-inherited), `sizeRuns`' forgetting step (turned off), the numbers'
absolute positioning (floated) and one counter name for every run.
Adding any stylesheet to the page after load pays the pass early, so
something leaves the whole listing's style dirty; finding what is a
follow-up.
- **Firefox's peak on `short` is 13 GB.** Taking the closed tabs'
listings out of rendering (`display: none`) left it at 13.1 GB, so their
frames aren't the cause. The review's control: the same 1.5 million rows
built by a bare script take 2.4 GB in Firefox, so most of the 13 GB is
Leaf's own work per row.
- **Pierre now serves only `lf-diff`**; its bundle and `pierre.js` could
move from the default package to the diff package.

Part of #4781: the open document no longer waits on every source, which
is what the issue reports; the issue stays open for the follow-ups above
until they have issues of their own.

> _This was written by Claude Code on behalf of max-sixty_

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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