Skip to content

Match a page search's gaps with one character class, so a long whitespace run can't freeze the tab - #5843

Merged
max-sixty merged 2 commits into
mainfrom
agent-a8aa816beca1c8fde
Nov 14, 2026
Merged

max-sixty merged 2 commits into
mainfrom
agent-a8aa816beca1c8fde

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

Page search froze the tab on some multi-word queries. findText joined the query's words with (?:[ws]+|edge)+, a nested quantifier, so wherever the page's raw text held the first word followed by a long whitespace run and then something other than the next word, the regex engine tried every split of the run. On the Using guide, "card It opens" was still stuck in findText when a five-minute probe gave up; a CDP pause showed the stack. The gap is now the single class [ws edge]+, built from COLLAPSE_CHARS as GAPS already is, which matches the same strings in one pass. Searching a part at a time (#5875) doesn't help, since the backtracking happens inside one exec: the new test still times out on main.

The docs caption test gets back the How it works case #5836 dropped when it hit this. A new Chromium case on a small page with an 80-space run times out on the old pattern and passes on the new one.

Found while landing #5836 (Ref #4052); Max asked for it to land. Tests run: the page-search browser tests in tests/browser/test_render_anchors.py, the passage runtime tests and pnpm run check:browser.

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

…pace run can't freeze the tab

Page search joined a query's words with `(?:[ws]+|edge)+`, a nested quantifier.
Where the page's raw text held the query's first word followed by a long run of
whitespace and then something other than the next word, the regex engine tried
every split of the run before giving up: on the Using guide, a search for "card
It opens" still held the main thread when a five-minute probe gave up, and the
test harness saw it in Chromium and Firefox both. The gap is
now `[ws edge]+`, the same set of strings, which fails such a run in one pass.

Found while moving the docs caption test to the reader pages (#5836): its
How it works and Using cases never filled the search box, because the page was
stuck in `findText`. The test gets its How it works case back, and a small page
with an 80-space run holds the fix: it times out on the old pattern.
leaf-agent
leaf-agent previously approved these changes Nov 11, 2026
max-sixty added a commit that referenced this pull request Nov 12, 2026
…t at a time (#5875)

On #4781's page (30 tabs, each a 4 MB text document, one comment), a
triple click on a line of the open document held the main thread 450–580
ms in Chromium and 0.9–1.5 s in Firefox, and a page search held it for
the whole search: 2.6–2.7 s in Chromium and 4–4.3 s in Firefox for a
word the page doesn't hold, and for a common word typed a key at a time,
6–11 s a key in Firefox and minutes in Chromium. Now a triple click
holds it 185–270 ms in Chromium and 210–380 ms in Firefox, and each of
the search's own tasks reads at most one document: under 200 ms in
Chromium and under 380 ms in Firefox, for a word the page doesn't hold
or a common word typed. Find returns the same matches in the same order,
a walk through them steps as main's does, and a stored anchor resolves
or detaches as before.

Two costs on Find's path are not the search and stay, both on main too:
opening the search field the first time restyles the whole page in
Firefox (6 s here), and landing on a match in a closed tab reveals it
(1.4 s in Chromium, 1.6–5 s in Firefox). See "Left as it was".

## The triple click

A selection's anchor is checked unique in its datum's words (root
`AGENTS.md`, "Validate once and share readings"). Profiles in both
engines showed most of the gesture was that check reading the 4 MB
document again, several times:

- **Finding the quote's copies.** One JavaScript pass of an automaton
over every character, 85–125 ms over the document in either engine, once
for the word the double click selects and once for the line. A reading's
letters (its characters less whitespace and edges) are now kept with it
(`spell`), taken out by the platform's own replace and marked every 32
letters, 15–30 ms once a reading. A few quotes are found in them by the
platform's string search, under 1 ms a quote; a batch of more than 32
still takes one automaton pass, so placing many comments stays linear in
the page and the quotes (`eachPlace`, which `findEach` uses too). Each
place is held to its quote's spaces where it stands in raw (`copyAt`),
as before.
- **Telling a common word's 17,000 copies apart.** `holds` read the
words around every copy. It now compares a side's letters outward
through raw first and refutes at the first letter that differs,
recording that span as what it read, so the index still re-places a
quote when a change there could complete its context. Only a copy whose
letters all match has its words read.
- **Checking each copy stands under the datum.** Two binary searches and
two climbs of the tree per copy, 0.7 s in Firefox for the word. A copy
in an element's own reading stands under it, unless a slot of the
element's own tree shows it words from outside.
- **Asking again.** The comment button's placement, the command bar's
availability, the response target and the remembered selection each
asked for the anchor, so the check ran up to eight times a gesture.
`findQuote` keeps its answer with the reading it answered for; a change
makes a new reading. It still asks the quote's copies first each time,
since that is what brings a quote the index has come to keep into it, so
a change to a quote first answered for a selection is heard once its
draft is placed.

What a triple click still pays beyond its cost on the same page with 40
KB documents (89–110 ms in Chromium, 74–99 ms in Firefox): the datum's
indexed reading on its first capture (14 ms in Chromium, 30–50 ms in
Firefox), its letters (15–30 ms), the list of the word's 17,000 copies,
which anchor placement keeps for the draft's quote, and the garbage
those leave. In Firefox the three clicks run as two tasks; the second
holds 120–250 ms on this branch and 180–230 ms on main.

## Page search

`pageMatches` (was `pageFind`) is a generator: it reads a part when the
search reaches it, and yields null before each part and for each match
the page hides. The target picker takes matches 8 ms at a time, the
first slice in the input's own task and each later one in a task of its
own, so a search waits on at most one document's reading. Matches arrive
in reading order, and the search lands where it did: the first match at
or below the top of the window, else the first; after a page change,
where it stood. Until it lands, the count slot shows an ellipsis; after,
the count grows where it stands. The status is busy (`aria-busy`) while
the search runs, so a screen reader says the count once, as it ends,
rather than at each slice ("Searching" and "Match 3 of 120 so far" are
its name meanwhile).

Main searched once, as the query was typed (and again before a step
after the page changed), so a step after that took 20–48 ms. Here a step
or a choice (Tab, Shift+Tab, Enter, `n`) waits only when its target
hasn't arrived: it takes matches at once until the search has landed,
where it stood if the page changed under it, and the match the step goes
to has arrived, or the search has ended (`settle`). That matters most
after a match in a closed tab: bringing it into sight opens the tab,
which changes the page, so the next step searches again from where it
stood. A step back past the first match is the one step whose match
comes last, so while the search runs it stays put and says "Searching"
rather than wait for all of it. Once Enter has closed the field, a
search still finding stops at the first page change rather than start
again, and `n` searches again from there; only the open field searches
again where it stood after a change, as a reveal makes.

## Measurements

#4781's page, served by `leaf server run --lifetime temporary` with a
throwaway state home: main at b3387f1, the merge base, against this
branch, each arm a snapshot of its tree. One browser per engine and
round, warmed 15 s; each gesture on a fresh load, 5 s after the
comment's mark stood and every document drew, with the page's view
checks stubbed in both arms. The longest task is the longest gap in a
chain of zero timeouts, which only main-thread work holds back. Arms
alternate within each round; load average 3–13. The branch arm is this
PR before its last change, the busy status, which redraws the count each
slice as the first push did. Chromium 156 headless and Firefox 157
through Playwright. Milliseconds, one per round.

| Longest task | Chromium, main | Chromium, branch | Firefox, main |
Firefox, branch |
|---|---|---|---|---|
| Triple click, first gesture | 548, 575, 552 | 253, 270, 254 | 1,179,
1,157, 1,112 | 376, 222, 332 |
| Triple click, after a click | 448, 455, 446 | 185, 187, 192 | 1,022,
1,474, 901 | 210, 210, 224 |
| Find, a word nowhere on the page | 2,654, 2,731, 2,595 | 181, 196, 192
| 4,116, 3,953, 4,318 | 323, 373, 321 |
| Find, `heap` typed | not run | 98, 78, 100 | 11,144, 6,387, 6,702 |
364, 219, 321 |

Each Find opens and closes the search once before its window, so
Firefox's first-open restyle stays out of it, then pastes or types its
word. "Nowhere" reads every document; `heap` matches 530,000 times. In
Chromium the typed search is still running 12 s later: whether a match
shows is read from its styles, and a match in a run of a text document
the browser hasn't drawn makes it style that run to answer, 26 ms a run
here, in 15,900 runs. The page stays live meanwhile; main's arm isn't
run, since `pageFind("widget asks")` had not returned after 14 minutes.
On the first push, at load 12–30, a word in a closed tab held 4.1 s in
Chromium and 8.7–15 s in Firefox on main; on the branch its search tasks
held at most 206 ms and 349 ms, and the reveal on landing 1.4 s and
4.3–5 s. Walking `FINDTOP`'s 30 matches, one a document, Tab goes 1 to 6
in both engines, and `n` after Enter selects the next.

Steps right after typing `heap` in Firefox, while the branch's search
still runs (main's has ended, since it searched as the key was typed):
each key's time from its keydown to the next paint, by Event Timing,
three rounds per arm, alternating, load 13–31.

| Keydown to next paint | Firefox, main | Firefox, branch |
|---|---|---|
| Longest key typing `heap` | 11,912, 10,752, 11,040 | 56, 88, 80 |
| Tab | 48, 64, 72 | 32, 72, 48 |
| Shift+Tab | 48, 48, 40 | 32, 56, 48 |
| Shift+Tab from the first match | 5,696, 5,368, 6,400 | 32, 24, 24 |

Main's wrap lands on the last of the 529,957 matches, in a closed tab,
and reveals it; the branch's stays on the first and says "Searching".

Profiles: Chromium through CDP's profiler, Firefox through the Gecko
profiler (`MOZ_PROFILER_STARTUP`), for each arm's triple click and
search.

## Left as it was

- **Firefox's first open of the search field** restyles the whole page
as focus enters it: 6.0 s here on main, 58 ms on the 40 KB page, 41 ms
the second time. In single runs, disabling the theme's sheet brought it
to 0.9 s and deleting every `:has()` and focus rule only to 2.7 s, so I
didn't find the rule. #5844 fixed focus restyles in Chromium.
- **Revealing a closed tab that holds a large document**, as landing on
a match there does: 1.4 s in Chromium, 0.9 s of it `claimPaintedRoots`
(`keyboard/scopes.js`) walking every element of the revealed document;
1.6–5 s in Firefox, mostly the tab's activation. A click on the tab pays
the same.
- **A common word in Chromium** runs for minutes behind the page, as
above. Reading only the style of the box that skips a run (`skipper`)
would end it in seconds but find words CSS hides inside a run or closed
tab the browser hasn't drawn; capping the count, as Firefox's find bar
does at 1,000, would too. Either changes Find's results, so it's yours
to choose. The text document's line-number counters make each run's
forced style 26 ms rather than 4 ms.

## Calls you may reverse

- **32 quotes** as the line between string search and one automaton
pass. I kept the automaton so many comments on one large document stay
linear in the page and the quotes; one mechanism would drop either that
or the 1 ms single-quote search.
- **A step waits for its match, and a wrap back stays put.** A step past
the last match found takes matches at once until the next has arrived or
the search has ended, as main's search did up front; Shift+Tab from the
first match while the search runs stays and says "Searching". Tab and
Enter in the field before the first match has arrived do what they do
with no match, as on main for a query with none.
- **The count slot's ellipsis** before the search lands, and the count
growing under a busy status after. The slot is 7 characters wide, so a
word wouldn't fit; a count over "99 of 99" was already cut by its
ellipsis on main.

## Tests

-
`test_a_walk_through_search_matches_in_closed_tabs_reaches_each_in_turn`
(both engines): Tab walks four matches in closed tabs and Shift+Tab back
one, with the page's clock run fast so each of the search's slices takes
one step; then Enter selects the match and `n` the next. It fails on the
first push of this PR.
-
`test_a_step_back_past_the_first_search_match_stays_while_the_search_runs`
(both engines): with the page's zero-delay timers held, so the search
stands unfinished, Shift+Tab from the first match stays and says
"Searching", and Tab goes on to the next. It fails on the second push.
- `tests/runtime/passage-boundaries.test.mjs`, "a quote first answered
before the index kept it is heard when its words are redrawn": fails
with the kept answer asked before the copies.
- `tests/runtime/datum-quotes.test.mjs`: capturing a word a datum
repeats 2,000 times reads only the words beside the selected copy (fails
without the letter check, reading every line), and a datum's quote
counts no copy among the words its slot shows from outside it (fails
without the slot guard).
-
`test_a_search_finds_each_match_in_prose_and_every_part_in_reading_order`
also holds a search to reading no part ahead of the matches taken (fails
when the search reads every part first).
- The shared quote cases cover the letters and the letter check
(reversing the letter order or dropping the spaces check fails them).
Both sides of the 32-quote line run in the existing tests: `findEach`'s
cases, and 42 quotes in "a line rewritten in a part changes only the
quotes standing on it".

Ran: `pnpm run test:runtime` (951 passed), `pnpm run check:browser`,
pre-commit on the changed files, and browser selections `-k "search or
find or match"` (62 passed) and `-k "quote or capture or selection or
select or repeated or twice or datum or comment_on"` (87 passed, 3
failed) over `test_render_anchors`, `test_render_semantic_selection`,
`test_render_floating_panel`, `test_render_aim`, `test_reading_pieces`,
`test_command_reference`, `test_render_diff_controls`,
`test_render_gate`, `test_code_review` and `test_code_and_documents`.
The 3 failures fail the same with main's runtime files at the merge
base:
`test_a_text_document_update_keeps_a_selection_it_carries_into_another_run[browser]`,
`test_dense_selection_hints_stay_short_and_reach_an_atomic_visual` and
`test_target_hints_name_only_addressable_elements_shown_by_a_disclosure`.
Not run: the whole suite (CI), journeys, Safari.

#5843 (open) adds the same `COLLAPSE_CHARS` export and builds the same
class of gaps, whitespace and edges, in `findText`'s pattern that this
builds as `GAPS`. Whichever lands second should take the other's:
`findText` can join its words with `GAPS.source`, or `GAPS` can move
beside `COLLAPSE` in `kernel-facts.js`.

Part of #5194.
@max-sixty
max-sixty merged commit 830e7ae into main Nov 14, 2026
5 checks passed
@max-sixty
max-sixty deleted the agent-a8aa816beca1c8fde branch November 14, 2026 19:44
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