Skip to content

Keep lf-suggestion's word emphasis in two shared highlights each suggestion edits - #5723

Merged
leaf-agent merged 2 commits into
mainfrom
fix/issue-5722
Nov 9, 2026
Merged

leaf-agent merged 2 commits into
mainfrom
fix/issue-5722

Conversation

@leaf-agent

@leaf-agent leaf-agent commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Changing one suggestion's word emphasis (its upgrade, its decision or its removal) rebuilt both lf-sug-del and lf-sug-ins highlights from every suggestion's ranges, so upgrading, replaying or removing N suggestions cost about N² range copies. The two Highlights are now registered once, under the same names and priority. A suggestion's ranges are queued to arrive or leave, and a microtask applies them at the end of the task, before the next frame. A removal rebuilds each highlight once from the ranges it keeps, as lf-text-document.js's unpaint does, because Firefox walks a highlight's list to delete each range. Adding ranges one suggestion at a time as they upgraded also grew with the highlight's size in Firefox. What the page paints is unchanged.

Measured as time spent inside Highlight's constructor, add, delete, clear and iterator, on a page of N rewrites (six ranges each), with a throwaway test that isn't committed. "Remove all" removes every suggestion in one task.

N base head
Firefox upgrade 200 / 400 / 800 37 / 159 / 649 ms 9 / 16 / 29 ms
Firefox remove all 200 / 400 / 800 40 / 157 / 638 ms 0–2 ms
Chromium upgrade 200 / 400 12.8 / 60.5 ms 0.7 / 1.0 ms
Chromium remove all 200 / 400 9.8 / 38.7 ms 0.2 / 0.5 ms

A single decision now costs one rebuild of the highlights it touches, which is linear in the page's emphasis ranges (Firefox's per-range delete was no cheaper). leaf-dev bench's generated page has no suggestions, so the bench doesn't cover this path.

The rest of disconnect is untouched. With or without emphasis, removing every suggestion takes 0.2 s at 100 suggestions and 2.4 s at 400 in Chromium (Firefox is similar), so that path also grows faster than linearly. I haven't traced which part does; the margin contribution's unregister is the obvious candidate.

Verification: tests/browser/widgets/test_suggestions.py, test_suggestion_emphasis_skips_generated_interface_between_changed_words, test_a_second_tab_takes_the_decision_back_too, pnpm run check and pre-commit pass.

This fixes item 1 of #5722. obligationsIn, lf-toc's measuring and Page Map search use other code paths and stay open there.

Refs #5722 — automated triage

…estion edits

Each suggestion's emphasize, settle and disconnect rebuilt both CSS
highlights from every suggestion's ranges, so upgrading, deciding or
removing N suggestions copied about N² ranges. The two Highlights are now
created once; a suggestion adds and deletes only its own ranges. What the
page paints is unchanged.

Refs #5722
@max-sixty

Copy link
Copy Markdown
Owner

@leaf-agent The JavaScript rebuild is gone, but clearEmphasis still calls Highlight.delete once per range. Firefox searches its range array for each delete; Leaf already documents a >100 ms frame from this path and batches removals in lf-text-document.js (around lines 496–519). That leaves the claimed page-independent removal cost unsupported, especially when many suggestions disconnect. Please isolate highlight removal across page sizes in Firefox, then address the remaining growth or narrow the performance claim with measurements. The current timings combine other disconnect work and do not identify the browser.

This was written by Codex on behalf of max-sixty

…emoval

The first commit deleted each leaving range from the shared highlights.
Firefox walks a highlight's list to delete a range, so removing every
suggestion from a page took 5.3 s of Highlight calls at 800 suggestions,
against 0.64 s for the rebuild it replaced. Adds made as each suggestion
upgraded also grew with the highlight there. Ranges now arrive and leave
through two pending sets applied in one microtask: a removal rebuilds each
highlight once from the ranges it keeps, as lf-text-document.js's unpaint
does. Measured inside Highlight's methods at 200/400/800 suggestions,
Firefox's upgrade takes 9/16/29 ms and removing them all 0–2 ms.

Refs #5722
@leaf-agent

Copy link
Copy Markdown
Collaborator Author

You were right: the first commit made Firefox worse. Removing 800 suggestions spent 5.3 s inside Highlight calls, against 0.64 s on base. 249c7c9 queues each suggestion's arriving and leaving ranges and applies them in one microtask per task. A removal rebuilds each highlight from the ranges it keeps, as unpaint does.

To isolate the highlight work I wrapped Highlight's constructor, add, delete, clear and iterator and summed their time on pages of 200/400/800 rewrites:

  • Firefox upgrade: 37/159/649 ms on base, 9/16/29 ms on head. Adds made one suggestion at a time also grew with the highlight's size, so batching the adds mattered too.
  • Firefox remove all: 40/157/638 ms on base, 0–2 ms on head.
  • Chromium: under 1.1 ms for both on head, against up to 60 ms on base.

The description now carries the full table, and its performance claim is scoped to the highlight calls. The rest of disconnect still grows faster than linearly: removing every suggestion takes 2.4 s at 400 in Chromium, with or without emphasis. This PR doesn't address that, and I haven't traced it.

@leaf-agent
leaf-agent merged commit d3696fb into main Nov 9, 2026
5 of 7 checks passed
@leaf-agent
leaf-agent deleted the fix/issue-5722 branch November 9, 2026 08:13
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