Repository navigation
Read which buttons a command declaration names once per change, and let each button read only its own - #5369
Conversation
…n read only its slice A row that names a native button as its `control` claims it: the row's availability becomes the button's `aria-disabled`, and a press runs the row. Every keyboard pass on main walks every scope on the page to find those buttons, twice, whenever any one button is dirty. On 512 sections of the `asks` page that is 1.8 whole-page gathers per letter typed and 4.4 per Tab, and about 60% of a letter's main-thread time. Each source, a scoped element or the page's own scopes, now has one computed reading: which buttons its rows and routes name, and through which rows. Three things invalidate it: - a signal its `routes` or `control` getters read; - a bump of the source's version from a mechanical change to it: a declaration attached, withdrawn or projected; a `paintKeysWithin` reaching it or painting inside it; its arrival or departure (`watchOwner`); - `paintKeys()`, which advances one epoch every reading reads. The element's reflection effect, which already reads its shortcuts, reads its reading and keeps membership both ways. Each button's effect reads only its own slice of its sources' readings, then its command's predicates. A press reads the same computeds, which re-read lazily if a mark moved them. Main has no index, so the three reviews of this branch's earlier versions found their cases in it. Each is now a test in `command-controls.test.mjs`: - a withdrawal followed by a full paint; - slots nested 16 deep, which walked exponentially; - a forwarded slot; - a declaration replaced in the press's own task; - a route and a control moved by a signal; - an owner removed while naming a button outside it; - a scope in a closed stage, declared before and after it entered; - a closed stage's host leaving; - a moved control costing a read per sibling button; - a command that starts unavailable; - a replaced declaration naming the same button through a getter; - a paint inside a declaring element whose control is a query. With manual slot assignment there are 14. #5313 removed handler getters, so the two cases built on them are now a declaration replaced in the task and a `when` that starts false. A paint inside an element now bumps the scoped elements it stands in, at the painted root's depth. A declaration that queries its own subtree for its control reads again when a render inside it replaces the button. On main that query went stale unless another scoped element stood in the painted root. `scopesWithin` walks down from the widget by `renderedChildElements`, the step `renderedParent` takes up, so each element is visited once. It enters a closed stage through arrivals.js, which now keeps its stages by host (`stageOf`) instead of in a set. It returns scopes in declaration order. A shadow tree that isn't a stage is not adopted. Adopting it through `adoptShadowStage` would put the page theme's shadow rules and a style node into the widget's own tree. It would also enroll the tree in every arrival watch, such as external-link marks and reading regions. So commands in an open non-stage tree are walked as before. Those in a closed non-stage tree are found by dispatch but not by `commandsWithin`, and their lifetime isn't observed. No tree in this repository is like that: Leaf's only closed root, `leaf-text`'s, declares no commands. Counted on the `asks` page at 512 sections, per input, against main with #5324: | | main | this | |---|---|---| | whole-page button gathers per letter | 1.8 | 0 | | whole-page button gathers per Tab | 4.4 | 0 | | one-source readings per letter | 0 | 3.8 | | one-source readings per Tab | 0 | 9.5 | | element readings per Tab | 10.9 | 11.3 | | button readings per Tab | 1.5 | 1.5 | A reading whose names come out the same returns the same value, so whatever reads it re-reads only when a name moved. Without that, a focus move re-read every option on the page whose control getter asks where the user is.
… press Every mechanical change to a source's button names now goes through one path. A declaration attached, withdrawn or projected, a paint in or around it, its arrival or departure, `paintKeys()`, and `commandsChanged` on an unchanged declaration each mark the source (`mark`). Marks gather as a set, and each marked source's version advances once when the frame or a press takes them (`takeChanges`). A mark asks for no frame: a painter's is read at the tail of the frame it paints in, and a change reported outside the frame asks for one where it is reported. A projection still reads its control at once, so the hints, which project every frame, ask for no next one. - Paints beside each other read the element around them once. Each painted sibling used to advance a shared ancestor's version, and each advance re-evaluated its reading: N painted children of an element naming N controls cost N²+3N getter reads. - A press settles what its task changed before it resolves a command: pending arrivals (`settleArrivals`, new in arrivals.js), the marks, then the membership of every source owed a reading. A control moved onto an already-wired button, or an owner put back after its departure was read, now runs on the next press in the same task. - A paint's marks and a widget's scope walk take the same steps (`eachRendered`), so a paint reaches a query inside a closed stage. - Every marked element is tied to its lifetime. Its departure drops its reading, its membership and its reflection's effect, so nothing subscribed through it holds it, and its placement marks it, so a widget that declared its commands before it was placed has its buttons read once it is. - `commandsChanged` on an unchanged declaration marks its element and the register's availability readers instead of calling `paintKeys()`. One call from lf-draft read every getter on the page. The scopes.js header now says what this module doesn't follow: closed trees that aren't stages.
…a record A press used to settle membership by draining every arrival watch, so it ran whatever lifecycle callbacks were pending. That failed inside a batch already being read: an arrival's callback that pressed a button missed an owner put back in the same batch. It also brought unrelated work into the press: one insertion before a press made it scan every registered stage, 40 to 680 WeakRef reads at 32 to 512 stages. Membership is now a record of what each source last named, whether or not it stood then, held through one WeakRef per source so a departed source can still be collected. Whether a source counts is read when it is asked (`isConnected`). Departure ends a source's subscriptions and keeps the record; arrival marks it for the frame. A press takes the marks, reads the membership of each source owed a reading, in the page or not, and reads its button's namers. It runs no observer callback, so it costs nine WeakRef reads whatever the stage count. `settleArrivals` is gone. The page's own scopes are marked by every paint, since their control getters may read anywhere in the document: a page command whose control moved under a `paintKeysWithin()` was left unwired. The scopes.js header names the gap left: a source that comes to name another button while out of the page, with no write reported, and is put back and pressed in one task, is found by the next frame rather than by that press.
…mes where declared Three couplings between invalidation, lifecycle and scheduling: - A take advanced each marked source's version while its subscribers were still listening, so they re-fired and asked for another frame. The command hints project their scope onto a control every frame; for a command whose routes() returns fresh, equal objects, each projection changed the reading and asked for the next frame, and the page repainted without end. A take now retires the reading instead: it disposes the element's reflection and the availability of the buttons the source named, all owed a reading by the mark, then drops the reading, which the next ask makes again. The version signal is gone. - Marking enrolled the source's lifetime, so a paint that marked a newly claimed button registered it with `watchOwner` inside a press, which read every pending mutation and ran unrelated arrival callbacks in the click. An element is now enrolled when it declares or carries a projection. - arrivals.js made the watch every owner shares inside the first owner's call, so its callbacks kept that owner alive. The watch is now made by a module-level function, and the first owner is collected once it leaves.
leaf-agent
left a comment
There was a problem hiding this comment.
The description quotes root AGENTS.md, "Responsiveness", as saying "An input's work depends on what it changes and on what the window shows, not on how much the page holds", but no file in the tree says that. The rule as written reads: "The work an input does, such as a keystroke, a click or a wheel step, depends on what the input changes, and never grows faster than the page." Since the squash merge makes this description main's commit message, please quote the rule as written, or drop "as adopted for the large-page work" if the quote comes from a branch that hasn't landed. The code change itself serves the rule as written.
|
Right — the quoted sentence is from a tightened rule on a branch that hasn't landed. The description now quotes
|
leaf-agent
left a comment
There was a problem hiding this comment.
The description now quotes the "Responsiveness" rule as it reads in AGENTS.md on main, which settles the one finding. Approving this head.
The sweep's forced repaint called commandsChanged(document.body). Since #5369 that marks only the element named, and body declares no commands, so the repaint read no element's keys and the sweep could not fail. The check API now exports paintKeys, and the sweep calls it. With every visible aria-keyshortcuts overwritten before the repaint, the old call restored none on review-a-plan and the sweep passed; paintKeys restores them and the sweep fails. With focus tracking removed from focus.js, the sweep fails on review-a-plan at step 14, a stale aria-keyshortcuts on the margin projection. On this branch and with main's runtime alike it now fails on four examples (dynamic-widgets, file-editor, motion, feature-gallery): a code block a disclosure or tab reveals gains its Copy key, Enter, only on a full repaint. The block's row asks whether its copy control has drawn its button; the likely cause, not yet confirmed, is that nothing marks the block when the control draws it.
…each margin row against only the rows beside it, and name a revealed code block's Copy key (#5866) Sending a comment on the generated asks page at 1,024 sections now draws the message in a median 185 ms in Chromium, down from 354 ms, and shows the server's reading at 298 ms, down from 504 ms. Resolving a thread there shows the server's reading at 194 ms, down from 470 ms. Both are still above the 100 ms root `AGENTS.md` ("Responsiveness") asks for; what remains is listed at the end. In Firefox, the send's main-thread time at 1,024 sections falls from 639 ms to 487 ms, and Resolve's from 601 ms to 217 ms. A code block that a disclosure or a tab reveals now names its Copy key, Enter, to assistive technology as soon as it shows; before, it named none until something repainted every key. ## Why Profiling the send at 1,024 sections in Chromium found two costs in the frame after the press that grew with the page. - **Every key on the page was reflected again.** Opening the thread card called `paintKeys()` (`previewChanged`, margin-projection.js), which marks every element that declares keys. A send re-read 7,196 elements' `aria-keyshortcuts` and button availability, about 110 ms of a profiled frame. Closing the card by Escape and opening it by its margin row did the same, through the same call and through `closePreview`, and a Resolve did it twice, before and after its landing (`landGesture`, thread/folding.js). - **Each margin row was seated against every row seated before it.** `seatRow` copied the whole list of seated footprints into its own arrays for each pin, and `packRow` filtered and sorted it for each rail row. A rows-only pass (#5817) still seats every row, so each of the send's two passes cost 27–30 ms at 1,024 sections, growing with the square of the rows. ## Changes **Key reflection follows what changed.** Each element's key reflection is a tracked effect that reads again when a signal it read moves (keyboard/scopes.js). The card already re-reads the keys of what it holds as it shows and as it hides (`paintKeysWithin(preview)`, thread-preview.js, since #4407), a margin row's reflection that asks whether the user is on the card's entry reads the card's revision signal (`onOpener`), and a settlement in flight already shows in the thread's state, which the reflections read (`threadReading`, thread-card.js). The four page-wide `paintKeys()` calls date from before the card painted its own keys (#4407) and before a reflection followed focus only where its rows asked (#5324). They are gone. The stale-keys sweep, fixed here so that it can fail, passes on every published example, and an independent probe of the card and Resolve routes found no stale key (both under Checked). Counted on the 1,024-section page: | Gesture | Elements reflected, main | This branch | | --- | ---: | ---: | | Send | 7,196 | 18 | | Escape out of the card | 7,200 | 3 | | Reopen the card from its margin row | 7,200 | 6 | | Resolve | 7,204 twice | 17 and 14 | **Seated footprints are indexed by their tops.** `Footprints` (margin-placement.js) holds the footprints the shared seating order has placed so far, sorted by top, and answers the two questions the seating folds ask: the footprint a rectangle overlaps (`hit`), and the footprints in order of their tops from the first that can reach below a line (`from`, for `packRow`'s push down). Each question reads only the footprints level with the rectangle it names, so a pass costs the rows times the logarithm of their number. Every row stands where it stood. A slide moves a seat past the first footprint it meets, and with a gap between seats, which of two overlapping footprints it meets first changes where the seat lands, so `hit` answers the overlapping footprint seated first, as main's scan in seating order did. `pinSpot` is no longer exported, since only a test used it; that assertion now goes through `seatRow`. On the 1,024-section page each rows-only pass of a send now takes 7–10 ms, from 27–30 ms (timed in the page, unprofiled). **The stale-keys sweep repaints every element's keys again.** Its forced repaint called `commandsChanged(document.body)`, which since #5369 marks only the element it names, and `body` declares no commands, so the repaint read no element's keys and the sweep could not fail. The check API now exports `paintKeys`, and the sweep calls it. **A code block a disclosure or a tab reveals names its Copy key.** The repaired sweep failed on four examples, on this branch and on main alike: dynamic-widgets, file-editor, motion and feature-gallery. A block's Copy row was live only once Web Awesome had drawn the copy control's button (`copyTrigger`), and `projectCommandScope` reads the row at once, in the task that makes the control, before the control's first update draws the button. Sampled frame by frame on motion after opening its disclosure, the row was read once, with no button yet; the button drew a frame later, and nothing read the row again, so the block's `aria-keyshortcuts` lacked Enter until something repainted every key. Blocks shown at load already named Enter once the page settled, since the sweep's reading on arrival found none stale; a block prepared later, as a disclosure or tab reveals it, stayed stale. `code.copy` (code-copy.js) now works as `heading.link.copy` (heading-links.js) already did for a control that hasn't drawn yet: its row has no condition, and its run waits for the control's definition and first update before clicking. The row so never depends on when the control draws, and nothing needs reading again. **The bench times the frame that paints.** `leaf-dev bench`'s paint pass timed a goal at the first task after the frame that showed it. When the server's answer arrived during a long input, its task ran before that one, so the send's `painted` included applying the server's reading. A profiled run at 1,024 sections read `painted` and `shown` both as 382 ms, while the frame showing the message had been laid out at about 245 ms. `painted.js` now times the frame with a size observation made as the frame renders, which the browser delivers after the frame's animation callbacks and layout, just before its paint. Both arms of a run use the same measurement, so main's numbers below are measured this way too. The review measured the step this puts into the daily chart: with the same runtime, old against new `painted.js`, at 64 sections over 4 rounds, a send's `shown` reads a median 101 → 90 ms, a Resolve's 40 → 37.5 ms and a click's 31 → 30 ms. ## Measurements Chromium, `uv run leaf-dev bench asks --sizes 256,1024 --input comment --input resolve --rounds 3 --base 3b9ff64`: both arms interleaved in one run, this branch against its merge base with main, load average 7.8 at the start and 9.2 at the end. Each cell is the median of 3 rounds, with the range in brackets. Paint is from the input to the frame that draws its own result; shown is to the frame presenting the server's reading of it; CPU is the input's main-thread time. | Input | Sections | Paint, main → branch (ms) | Shown, main → branch (ms) | CPU, main → branch (ms) | | --- | ---: | --- | --- | --- | | Send | 8 | 57 [54–88] → 55 [54–89] | 87 [86–88] → 89 [88–89] | 75 [75–84] → 85 [80–85] | | Send | 256 | 112 [111–113] → 81 [74–84] | 155 [154–158] → 124 [122–124] | 152 [146–159] → 118 [115–124] | | Send | 1,024 | 354 [310–362] → 185 [167–323] | 504 [430–507] → 298 [283–323] | 451 [447–458] → 291 [291–326] | | Resolve | 8 | 20 [19–25] → 22 [21–22] | 33 [30–45] → 32 [32–34] | 38 [37–46] → 34 [32–35] | | Resolve | 256 | 124 [119–126] → 57 [56–60] | 124 [119–126] → 57 [56–60] | 134 [124–135] → 67 [65–73] | | Resolve | 1,024 | 242 [226–249] → 108 [108–110] | 470 [438–481] → 194 [194–198] | 431 [426–470] → 210 [201–214] | The 8-section rows measure the runtime's fixed work, which this change leaves alone, and their differences are within the spread of the rounds. Firefox, the same command with `--browser firefox`, which reads only main-thread CPU there, run straight after; load average 8.8 at the start and 16.8 at the end: | Input | Sections | CPU, main → branch (ms) | | --- | ---: | --- | | Send | 8 | 75 [70–94] → 82 [74–90] | | Send | 256 | 210 [190–245] → 172 [165–268] | | Send | 1,024 | 639 [591–673] → 487 [433–489] | | Resolve | 8 | 46 [39–46] → 41 [40–54] | | Resolve | 256 | 206 [189–221] → 109 [99–119] | | Resolve | 1,024 | 601 [555–603] → 217 [189–316] | ## What remains at 1,024 sections In the run above, the bench's long-frame attribution at 1,024 sections puts 135 ms of script in the press's task and 51 ms in the frame after it. The costs below are part of that time and grow with the page. Each time given is from a profiled run of this branch at 1,024 sections, with the host's load about 17, so read the times as proportions: - **The annotations are collected and drawn whole, twice per send.** The presenter's paint of the sent comment and the card's own redraw (`showPreview` → `renderAnnotations.refresh`) each run `paintAnnotations`. Its `collectEntries` (annotation-inventory.js) reads every thread, contribution and workflow on the page. Its `renderNow` walks every entry, and though it skips drawing a host whose entry is unchanged, it registers every row and reads every target's place down the page (`readSpokenPositions`). Each marker's name also counts the markers ("3 of 1,024"), so a row added or taken away renames every marker (`nameMarkers`). Together 64 ms profiled in the press's task, and the same again when the server's reading arrives. Removing it takes an inventory that rebuilds only the entries whose inputs a publication changed (an unchanged entry already keeps its identity, `keptEntry`), a projection that visits only the hosts whose entries changed, and one paint per send rather than two. - **The application's publication shares every subtree with the last reading** (framework/snapshot.js, `share`), 16 ms profiled for the send's own publication. Folds that produce only what moved would remove it. - **The rows-only margin pass still visits every row**, 12–14 ms profiled: each row's record is carried and its seat written, though only the new row moved. #5752 describes the change that removes it, a pass that seats only the rows a change can move. - **The Tab stop is chosen from every marker** (`syncRoving` → `visibleRows`, margin-projection.js), clipping each marker's box, 8.5 ms profiled. Reading only the markers around the window would remove it. - **Focus moves record the reading place** (`recordPlaces`, version.js, through `elementsFromPoint`), 17 ms profiled across the press and the frame. This change does not close #5752: a change to the page's words or styles still reads every row. ## Checked - `test_presses_and_fills_leave_no_element_naming_stale_keys` (`tests/journeys/test_render_navigation.py`), the stale-keys sweep, on all 37 published examples: after each of 40 seeded presses and fills, some followed by Escape, it forces a repaint of every element's keys and fails on any attribute the repaint changes. - It can fail now. With every visible `aria-keyshortcuts` overwritten before the repaint, the old repaint passed on review-a-plan and `paintKeys()` fails. With focus tracking removed from focus.js, it fails on review-a-plan at step 14, on the margin projection's keys. - Repaired, it failed on four examples, identically before the copy fix on this branch and with main's runtime: dynamic-widgets, file-editor, motion and feature-gallery, each a code block a disclosure or tab revealed that lacked Enter. With the copy fix it passes on all 37; after the copy row took the heading link's design, it passes on those four again. - Under parallel load, file-tree's teardown sometimes reports an unmoved scroll: the vendored file tree (`pierre-trees`) writes a `scrollTop` that moves nothing, which the write watch flags. It is not a stale key, and main's runtime shows it too. Across every parallel run that included file-tree, it showed in 4 of 11 on this branch and 2 of 8 with main's runtime; in the last four alternating pairs over the same 10 examples, in 2 on main and none on this branch. Alone, file-tree passed in all 5 runs. - An independent review forced the real `paintKeys()` after a send (with its POST held and landed), Escape, reopening the card from its margin row, Tab into the card, a Resolve (held, landed, and refused with a 409), `z` and a second Resolve, on review-a-plan and the bench's asks page, and found no stale `aria-keyshortcuts` or `aria-disabled`. It also reproduced the speedup: send paint 348 → 193 ms and Resolve paint 227 → 124 ms at 1,024 sections in Chromium. - `the footprints answer what a scan in the order they were seated answers` (new, `tests/runtime/margin-placement.test.mjs`): 400 footprints of mixed heights, many overlapping and some sharing a top, against 400 probes, comparing `hit` and `from` with a linear scan. It fails when `hit` answers the first overlapping footprint by top rather than the first seated. The other placement tests now seat through `Footprints`. - A scratch comparison, not committed, seated rows in 4,000 randomized trials (up to 120 rows each, rail rows and pins, held, folded and level pins, and gaps up to 8 px) with main's `seatRow` and `packRow` and with this branch's: every seat and push was identical. With `hit` answering by top instead, it failed on trial 5. - 41 browser tests by name: the full-pass comparison and seat tests in `margin/test_pins.py`, the reachability and held-marker tests in `margin/test_rail.py` and `margin/test_clusters.py`, the card's opening, closing, Escape and travel tests in `margin/test_opening_threads.py`, the Resolve, Reopen, undo and in-flight tests in `threads/test_resolving.py`, and the submit-shortcut test in `navigation/test_commenting.py`. - 28 code-block copy tests by name: the copy tests in `test_render_anchors.py` (Enter beside nested links, hidden and deferred blocks, closed sections, scroll stops) and `test_render_code_reading.py`, and the copy controls in `margin/test_scrollers.py`, `drafts/test_draft_editing.py`, `threads/test_rewrites.py`, `widgets/test_lifecycle_and_controls.py` and `test_page_head.py`. All 28 pass. `test_hidden_code_arrivals_do_not_read_skipped_blocks` now also asks that the block a disclosure reveals names Enter, and fails on main's copy row. - `pnpm run test:runtime` (936 pass), `pnpm run check:browser`, pre-commit on every changed file. - The reflection counts above, read with a temporary counter in `reflectKeys` on main and on this branch. ## Not checked - Firefox's paint and shown times: the bench reads only CPU there. - WebKit. - The broad suite and the main-push browser tier. - The stale-keys sweep presses controls in a seeded random order, so it reaches the card and settlement routes it happens to press, not every one. Part of #5194.
Every keyboard reflection pass gathered every button every command declaration on the page names, so a keystroke's work grew with the page: about 2 whole-page gathers per letter and 4 per Tab on the 512-section
askspage ofleaf-dev scaling(each keystroke in the comment field repaints its buttons,input.js'sfield.updated). This keeps one reading per source of the buttons its routes and controls name, and lets each button read only its own share of it, so an input reads only the declarations its change reaches.This serves the rule in root
AGENTS.md, "Responsiveness": "The work an input does, such as a keystroke, a click or a wheel step, depends on what the input changes, and never grows faster than the page."How it works (
browser/runtime/keyboard/scopes.js)PAGE_SOURCE) has one reading of the buttons its rows name. Buttons consume their slice through reverse membership; no button effect reads a whole source.mark: a declaration attached, withdrawn or projected; a paint in or around the source (the painted root's rendered ancestors included, so a declaration that queries its own subtree hears it); an arrival or departure;paintKeys();commandsChangedon an unchanged declaration, which now marks its own source instead of repainting the page. Marks collect as a set of distinct sources and are taken once, at the frame or at a press. A take drops the source's reading and the availability effects it fed, and the next ask rebuilds them, so a take notifies nothing and schedules no frame.namers(B)holds oneWeakRefper source: what it named when last read, in the page or not. Whether a namer counts is decided when it's read (isConnected). A departure ends a source's subscriptions and keeps the record; a press reads only established state and runs no observer callback.watchOwner,arrivals.js), and dropped on departure, so a removed owner can be collected. Paint marking and scope walking share one walk that enters closed stages throughstageOf;shadow-tree.jsgainsrenderedChildElements, the inverse ofrenderedParent. The shared arrival watch no longer lives in its first owner's closure, which kept that owner alive (a defect on main).commandsWithin, and its lifetime isn't observed; and a source that changes what it names while out of the page, unmarked, then is put back and pressed in the same task, misses that press and is found at the next frame. Nothing in Leaf declares scopes in such a tree or composes that sequence.Measured on the
askspage at 512 sections, against mainA subtractive alternative was measured too: main's gather kept, run only when membership could have changed. It halves the gathers but can't skip the comment field's per-keystroke paint without reading each source separately, so a letter stayed at 7.4–8.2 ms (+68 code lines in
scopes.js, against this change's +144).A click is still page-sized on both, because the margin's contribution presenter re-projects every margin entry on each publication; that is the margin's change to make.
Checks
Seven Codex reviews; the last found it ready, with one unexercised edge (an empty page-source reading whose getter later names a newly inserted button, found at the next
paintKeys()). Each defect the earlier reviews reproduced (stale routes and controls after a signal, a replaced declaration, removed owners, handlers unavailable at first, nested paints and ancestor query getters, closed stages, nested and forwarded slots, quadratic sibling reads, re-entrant presses, press-time observer draining, repaint loops, a retained first owner) has a runtime test intests/runtime/command-controls.test.mjsorarrivals.test.mjs;tests/runtime/dom.mjsderivesassignedSlotfrom happy-dom'sassignedNodesso slot tests hold.pnpm run test:runtimepasses 807,pnpm run check:browserand pre-commit pass, and the 33 keyboard browser tests pass. A debug oracle compared every scoped element'saria-keyshortcuts,aria-disabledanddisabledafter each input with what a fresh full reflection writes, over the 24 example pages (about 30 inputs each) and a journey through margin markers and thread cards on 9 pages: no difference.