Skip to content

Four runtime paths redo work that grows with the page: lf-suggestion highlights, obligationsIn, lf-toc measuring, Page Map search #5722

Closed

Description

@leaf-agent

The nightly survey found four runtime paths whose work grows with the square of how much the page holds, or that redo whole-page work on each input. Root AGENTS.md, "Responsiveness", rules out both. Each was found by reading the code, and none has been measured with leaf-dev bench yet. They are independent and can be fixed separately. #5194 tracks the broader large-page cost; these are specific paths that are not on its list.

  1. lf-suggestion's highlights are rebuilt once per suggestion. repaintEmphasis() collects every suggestion's ranges and replaces both CSS.highlights entries. Each suggestion's #emphasize() (from setup() and #draw()), its #settle() and its disconnectedCallback() call it (L176, L425, L507). Upgrading or replaying N suggestions therefore copies about N² ranges. A possible fix: create the two Highlights once and have each suggestion add and delete its own ranges. Highlight is a set, so the emphasized map and the rebuild go away.
  2. obligationsIn(region) scans the whole obligation list for each region. place.js is called once per tab panel (lf-tabs.js #tab), per card (lf-deck.js) and per goal (work.js interventions), so the cost is regions × obligations. current-target.js (obligationHolding) already answers the inverse question at the cost of the node's depth. A per-list WeakMap of placed sources, like contextsOf's, would serve every caller.
  3. lf-toc's map measuring.
    • In lf-toc.js around L329, #measured.find((other, at) => at > index && other.shown) runs inside #measured.forEach, which is O(sections²).
    • #mapPosition rebuilds its anchors array on each call, and #standLens calls it several times per stop.
    • #paint walks every section on each scroll frame, so the cost of a wheel step grows with the outline. A reversed pass, anchors built once per #standLens, and a binary search over the sorted positions would remove all three.
  4. Page Map search re-derives its groups on each keystroke. The search field's input goes through renderSheet → presentSheet in page-map-dialog.js, which runs says(blockAt(targetFor(entry))) for every entry and rebuilds marginMapGroups, although only the hidden filter depends on the query. Computing the groups and the search text when the entries change would leave the keystroke only the filter.

A smaller case: messageHolding scans the prepared messages with a querySelector each. anchor-placement.js calls it once per thread whose anchor doesn't resolve on the page, so the cost is threads × messages, but only on that path.

For each, a leaf-dev bench comparison of base and head on its generated largest page would confirm both the growth and the fix.

Activity

  1. leaf-agent commented on Nov 9, 2026

    @leaf-agent
    CollaboratorAuthor

    #5723 fixes item 1. The lf-sug-del and lf-sug-ins highlights are registered once. Each suggestion's arriving and leaving ranges are applied together in one microtask per task, and a removal rebuilds each highlight once from the ranges it keeps. Batching matters in Firefox, which walks a highlight's list on every delete, so deleting ranges one at a time is quadratic too. Measured as time spent inside Highlight calls on pages of 200/400/800 rewrites, Firefox's upgrade went from 37/159/649 ms to 9/16/29 ms, and removing every suggestion went from 40/157/638 ms to 0–2 ms. The PR's description has the Chromium numbers.

    Removing suggestions has a separate cost outside the highlights: removing every suggestion takes 2.4 s at 400, with or without emphasis, so the rest of disconnect also grows faster than linearly. I haven't traced it.

    Items 2–4 (obligationsIn, lf-toc's measuring, Page Map search) and messageHolding are untouched, so this issue stays open for them. Each is a separate change. For lf-toc and Page Map search, the bench's scroll and key inputs can measure the before and after.

  2. leaf-agent commented on Nov 10, 2026

    @leaf-agent
    CollaboratorAuthor

    #5790 fixes item 4. Typing in the Page Map search now only filters the groups. They and their search text are derived when the entries change or the map opens. On a page with 400 comments, handling one keystroke went from 17.4 ms to 11.8 ms in headless Chromium. The rest of that time is in holdFocus, paintKeysWithin and the filter's style writes, which the PR leaves alone. The PR's CI and landing are with tend-land.

    A note for item 2 before anyone builds it: a WeakMap of placed sources keyed on the obligation list would serve stale answers. contextsOf can cache per list because it reads only ids. obligationPlace, though, resolves elementById and projectedDatum against the live DOM, and the same list stays current while a tab panel, card or projected datum is created or replaced. That cache would need invalidating on DOM change, or a different key.

    Items 2 and 3 and messageHolding remain, so this issue stays open.

  3. leaf-agent commented on Nov 11, 2026

    @leaf-agent
    CollaboratorAuthor

    I'm not fixing item 3 (lf-toc's measuring): it doesn't grow with the page. I built the fix (a reversed pass in #measure, anchors built once per fit, binary searches in #mapPosition and #paint) and it passed the map and lens browser tests. A timing check found no difference, and the code explains why:

    • #mapPosition's anchors are bounded by the window, not the outline. They exist only for sections that got a fitted mapPosition. When the labels don't fit the track, #fitLabels switches the map to data-lf-outline and returns no shifts. Rebuilding the anchors and scanning them is quadratic only in the number of labels the track's height can hold.
    • The find inside #measure's forEach stops at the next shown section. That is usually the next index, so it is linear unless a long run of sections is hidden.
    • #paint's walk is linear over plain objects. It reads no layout, and the bench's scroll inputs on other pages already cost 5–15 ms.
    Measurement

    The bench's generated pages contain no lf-toc, so leaf-dev bench --input scroll can't see this path; on those pages the base and the fix measured the same. I used a throwaway Chromium test instead. It opened a sidebar lf-toc over 100, 400 and 1600 h2 sections, resized the window four times (each resize remeasures the map), and took the lowest ScriptDuration delta. Two interleaved rounds on each side:

    sections main fix
    100 114 / 120 ms 116 / 119 ms
    400 93 / 112 ms 98 / 95 ms
    1600 930 / 895 ms 863 / 890 ms

    At 1600 sections a resize's script time reaches about 0.9 s on both sides. That cost is somewhere other than these three paths, possibly outside lf-toc; I didn't trace it.

    That leaves item 2 (obligationsIn per region; the earlier comment explains why a per-list cache would go stale) and messageHolding, so this issue stays open for them.

  4. leaf-agent commented on Nov 16, 2026

    @leaf-agent
    CollaboratorAuthor

    #6151 fixes item 2 for lf-tabs and lf-deck. The new obligationsByRegion(regions, items) places each obligation once and climbs from its source, adding it to every region it passes, so a side tab list or deck pays for its obligations once per render instead of once per panel or card. Placement still runs against the live DOM on each call, so the staleness concern in the earlier comment doesn't apply. obligationsIn is now the one-region case of the same function.

    Measured with a new bench collection, leaf-dev bench asktabs (side tabs, each holding an ask): a click's CPU at 256 tabs went from 78.9 ms to 55.3 ms. Before the change the profile put the growth in obligationsIn (exponent 1.74). After it, nothing in the click grows past exponent 1.5. The PR's CI and landing are with tend-land.

    Still open here: work.js's interventions, which calls obligationsIn once per goal over the user's open obligations, and messageHolding.

  5. leaf-agent commented on Nov 17, 2026

    @leaf-agent
    CollaboratorAuthor

    #6276 fixes the last open item, work.js's interventions. readWork now places the open widget obligations for every goal in one obligationsByRegion call, and computes each goal's stopped once per reading instead of again for every ancestor. The work-projection browser test passes. CI and landing are with tend-land, and the PR closes this issue when it merges.

    messageHolding no longer scans: since #6056 it looks the id up in keptElements.

    readWork still grows faster than the page in two ways #6276 doesn't touch. workEntries runs querySelectorAll("*") for each goal, and widgets that call readWork from their render recompute the whole scope once per widget. The PR description describes both.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions