Skip to content

Compose a selection's layer once when requests ask for it together - #5856

Merged
max-sixty merged 2 commits into
mainfrom
layer-compose-once
Nov 12, 2026
Merged

max-sixty merged 2 commits into
mainfrom
layer-compose-once

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

A page server answers each request on its own thread, and every route needs the page's layer vocabulary. functools.cache held the composition, but it lets concurrent callers of a missing key each compute it. On a cold leaf.page container, /examples/pr-walkthrough/ sends 46 api/deferred loads as it opens, one for each file in its diff. The 46 threads composed the same layer at once and shared the CPU until they all finished together. A visitor's first comment, sent meanwhile, got a 47th share of the CPU. Last week's production logs show first writes of 33 s and 155 s on that page; the file-tree example has the same burst.

page_memory.once keeps a reading in a dict. The first thread to ask for a missing key makes it, and the rest wait for that one. Two kinds of cache now use it:

  • the layer's vocabulary and composition (layer._vocabulary, layer._composed), which used functools.cache;
  • the readings derived from a vocabulary (registry.storage.layer_vocabulary, _revision_composition), which used check-then-set.

This changes Leaf's main code. It's about 30 lines, and the fault isn't specific to the website: a local page server starting cold under a burst of requests duplicates the same work.

Measured in the website's container image on 1 vCPU, with 46 concurrent deferred loads at startup and a comment 1 s later:

Deferred loads all done Comment
Before 4.6–5.2 s 15.3–15.9 s
This PR 0.5 s 11.7 s
No deferred loads – 11.8 s

The comment's remaining time is that image's serial chunk uploads (#5764 has since made them parallel), so the comment now costs the same as with no burst.

Test: eight threads asking together for a cold selection's vocabulary compose it once and share the result. With functools.cache restored, the test fails with 8 compositions.

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

A page server answers each request on its own thread, and every route needs the
page's layer vocabulary. `functools.cache` held the composition, but it lets
concurrent callers of a missing key each compute it. On a cold leaf.page container,
`/examples/pr-walkthrough/` sends 46 `api/deferred` loads as it opens (one per
file of its diff), so 46 threads composed the same layer at once and shared the
CPU until all finished together. A visitor's first comment, sent meanwhile, got
a 47th share: production logged first writes of 33 s and 155 s on that page.

`page_memory.once` keeps a reading in a dict so that the first thread to ask
for a missing key makes it and the rest wait for it. The layer's vocabulary and
composition (`layer._vocabulary`, `layer._composed`) and the readings derived
from a vocabulary (`registry.storage.layer_vocabulary`, `_revision_composition`)
use it in place of `functools.cache` and check-then-set.

Measured in the website's container image on 1 vCPU, with 46 concurrent
deferred loads at startup and a comment 1 s later: the loads finish in 0.5 s
instead of 4.6–5.2 s, and the comment takes 11.7 s instead of 15.3–15.9 s,
the same as with no loads (11.8 s; that image still uploads serially).

New test: eight threads asking together for a cold selection's vocabulary
compose it once and share it. It fails with `functools.cache` (8 compositions).
leaf-agent
leaf-agent previously approved these changes Nov 12, 2026
…or 0.2 s

The suite's waits bound a hang rather than time the work (tests/AGENTS.md,
"Results do not depend on execution speed"), so the test now holds the first
composition until every other request is inside page_memory.once or composing
too, and its barrier and joins take STATED_TIMEOUT. With functools.cache
restored it still fails with 8 compositions.
@max-sixty
max-sixty merged commit a90f7ae into main Nov 12, 2026
5 checks passed
@max-sixty
max-sixty deleted the layer-compose-once branch November 12, 2026 05:19
max-sixty added a commit that referenced this pull request Nov 14, 2026
…ate (#5896)

Leaf's data contracts could mark one field of each record as `deferred`:
the page received rows without it, and a widget fetched each field with
`loadDeferred` through `/api/deferred`. Only `lf-diff`'s `unified-diff`
contract used it, for each file's patch. Since #2742, `lf-diff` derives
each row from its patch, so it fetched every patch as soon as it showed:
one request per file right after the state answer, saving no bytes. On
leaf.page those per-file requests also passed the edge's stored state
answer, so viewing a diff page started the visitor's container.

This removes the mechanism. A manifest's patches are part of the value
page state carries, which the edge already answers for a visitor who
hasn't written.

**Removed**

- `records.deferred` in the registry schema, and the `contract-records`
check, which only kept a deferred field from naming the key.
- The `/api/deferred` route and the server functions behind it
(`data_manifest`, `deferred_fields`, `deferred_value`,
`browser_data_from`), with the two source-protocol methods only it read.
- The browser's loader: `loadDeferred` in the portable API and
`runtime/deferred-value.js`, and `offlineDeferred` in the export reader.
- The export's per-value spans, which let one deferred field be decoded
alone: its data file is now one gzipped JSON object keyed by source.
- The website's capture of deferred answers into a session's stored
responses, and the Worker's routing of them.
- `lf-diff`'s Retry for a patch that failed to load, and `readEntry`,
which read a manifest patch when its file opened. `sourceRecords` now
parses each manifest patch when the source arrives. A patch it can't
draw refuses only its own file and is reported as before.

**Behaviour that changes for agents.** `leaf page state <page>
<message>` reads a message's widgets with their inputs' values. For an
`lf-diff` in a message bound to a manifest, that reading now carries
every patch (220 KB on pr-walkthrough) instead of the rows without them,
as a patch-text source always did.

**Kept.** `page_memory.once` (#5856) was justified by the 46 concurrent
deferred loads on a cold container. It still makes any requests that
reach a cold server together compose the layer once; only its docstring
changes.

**Measured** on pr-walkthrough with `leaf-dev probe --base`: the page
first presents the server's state after 315 requests instead of 363, and
8.08 MB instead of 8.09 MB. The state answer itself grows by the 220 KB
of patches, which one deferred request per file used to fetch just after
it; later state reads still leave out data the tab already holds.

**Tests**

- Deleted: the deferred route and its stale-revision refusal, offline
deferred reads, `loadDeferred`'s guards, and the in-place and reopen
Retry tests.
- Replaced: the disclosure test that walked a stale deferred read. Its
successor,
`test_a_file_opened_while_a_newer_source_draws_shows_the_newer_lines`,
holds the wait an opened file keeps on a newer revision's drawing. With
that wait removed, it fails with the older lines shown.
- Rewritten to read whole values:
  - the historical-state and snapshot server tests;
- the page-owned-contract test, whose old registry now judges data the
new one accepts;
- the anchors and seated-thread diff tests (a refused file now comes
from a patch naming another file);
  - the offline export test.
- Moved: the repeated-key refusal, from a server test to the manifest
model test.


`tests/browser/projection/test_code_review.py::test_call_diff_source_paths_do_not_alias_durable_rename_coordinates`
fails locally on macOS (`556.8 == 563.8 ± 0.5`) identically on the merge
base; this change doesn't cause it.

**Docs.** `package-data.md` and `module-state.md` lose the deferred
paragraphs, and `package-data.md` says what `records` does now. The diff
package's author instructions now say the manifest producer isolates a
file `lf-diff` can't draw, since size is no longer a reason to use it.

> _This was written by Claude Code on behalf of max-sixty_
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