Skip to content

perf(lookup): date only candidate versions and stream derives through a buffer - #408

Merged
sweepies merged 1 commit into
mainfrom
perf/cold-lookup-and-streaming
Nov 24, 2026
Merged

sweepies merged 1 commit into
mainfrom
perf/cold-lookup-and-streaming

Conversation

@sweepies

@sweepies sweepies commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

PR 1 of 3 in the performance regression series: cold lookups, streaming derives, and datasource and lookup CPU.

Why

Per-version dating (#342) made cold lookups ask Go's proxy, Maven POMs and NuGet registration pages about every version ever published, and stored each answer twice: once as release-dates state, once as an HTTP row. The measured cold run of a large Go monorepo went from about 12 s to 62–86 s, with ~34.6k requests (~33.8k to proxy.golang.org). The corpus cold run went from 6.7k to 23k requests. Streamed derives also read their bodies one byte at a time.

What changes

Candidate-scoped dating, with #342's guarantee kept

  • Evaluator::candidates(dep) names the versions policy can read a date or flag of. These are the releases newer than the baseline evaluate_with compares against, wherever they sort, so a patch on an older line counts like a new major. Prereleases are included only when the current version is one or a permitted filter is set.
  • Every version is a candidate when the baseline needs the catalog to work out: an extractVersion pattern, a runtime toolchain range, or a current value that is not a version (latest, a dist-tag).
  • The engine widens candidates per catalog key across the whole batch (lowest floor, prereleases if anyone wants them). It hands them to Client::with_candidates.
  • Go .info, Maven POM HEADs, NuGet registration pages and the versions-host tags only describe candidates. Everything else is listed unchecked, and policy never proposes those versions.
  • A stored catalog view records the candidates it was read for. It is reused only when those cover what the new lookup needs, so a dependency on an older version re-reads instead of finding its candidates unchecked.
  • release-dates still keeps every answer. A later lookup from an older baseline only dates the versions it adds.
  • A new invariant test runs policy twice, once over a fully dated catalog and once over a candidate-scoped one with the rest unchecked. Proposals, holds and not-proposed lists must match. It covers a wait, a patch on the current line, prerelease currents and filters, a Maven range floor, and a lockfile-resolved current.

Date requests skip the HTTP table

  • New Cached::uncached(method, url, accept). It never reads or writes the http table, so no writer transactions, and no npm packuments evicted from the 256 MiB budget.
  • It keeps URL validation, operator mirrors and credentials, host permits and cooldowns. A 429 is retried like a registry read; one that outlasts the retries is Status(429).
  • Maven fix: POM HEAD used registry_head, which skipped the mirror that served maven-metadata.xml. It also returned Ok on a 429, which got stored permanently as "dated, no date". POMs now go through the mirror, and a 429 leaves the version unchecked.
  • release-dates is keyed by the entry that actually answered (Cached::plain_credential), mirrors included.

NuGet: the registration index now keeps each page's lower/upper. A linked page whose upper is at or below the floor is not fetched. Pages without bounds are always read.

versions host: an undated mirror tag that is not a candidate (a floating v1/v3 below the baseline) no longer throws away the whole mirror. An undated candidate tag still falls back to GitHub, as before.

Streaming derives and CPU

  • json_document reads through a 64 KiB BufReader. Trailing-data detection and stream-error propagation are unchanged, and tested.
  • Anchors scans in linear time: case-insensitive byte search, a cursor, and one drain per fed chunk.
  • PYPI_INDEX_CATALOG looks at the first non-whitespace byte. { is parsed as PEP 691 JSON, anything else is scanned as HTML. The Tee is gone.
  • Typed serde structs replace Value DOMs for SBOMs (no flatten, which would buffer everything), image configs, Packagist, RubyGems and NuGet.
  • nodejs.org/dist/index.json is now a node-lts derived document: just the newest LTS version.
  • Helm reduces each entry while the walk reads it.
  • OSV listings are read concurrently and written in one transaction with set_states. Tag objects are read concurrently and written once per GraphQL page.
  • Groups::collect returns immediately when the repository has no Pkl packages.
  • Advisory affected versions and ranges are parsed and sorted once per evaluation.
  • Filter::asks_for_prerelease takes the release text lazily and assumes the caller already matched the filter, so no second matches.

Versions: every derived representation is bumped, because derive.rs is in every row. So are the pull-token row (its file changed), catalog, catalog-controls, changelog, source-head, tag-catalog, tag-object, osv-advisory, extract, files, config and config-files. Docs updated: guide/sources.md and internals/cache.md.

Behaviour notes for review

  • A private-index page that starts with { but is not PEP 691 is now a document error instead of being retried as HTML. No HTML page starts with {, and a page that doesn't start with { could never parse as JSON, so every other page reads exactly as before.
  • Typed derives turn a wrong-typed field into a document error, where the DOM code would have skipped it. Unknown fields are still ignored.
  • NuGet: the current version's own page may be skipped when upper == current. Policy never reads the current version's yanked or deprecated flags; only versions above the baseline are reported as rejected.
  • Small edits outside the listed scope were needed: crates/engine/src/lib.rs (threading the candidates map into lookup_key, plus state version bumps) and crates/notes (representation version bumps forced by derive.rs).

Tests

Run on the remote VM:

  • cargo test --locked --no-fail-fast -p patchula-http -p patchula-datasource -p patchula-lookup -p patchula-config -p patchula-engine -p patchula-notes -p patchula-cache -p patchula-update -- --skip lockfiles::tests::real_: 70 binaries, 932 passed, 0 failed. This includes the derive_parity and state_parity tests.
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean. cargo fmt --all --check: clean.

New tests: buffered json_document read sizes and mid-stream failure; Anchors edge cases at every chunk size, bounded memory, and the JSON/HTML sniff; uncached (no cache reads or writes, 429 retry then error, mirror plus credentials); Go candidate dating and that it never touches the HTTP cache; Maven candidates, POM 429, and POM through the mirror; NuGet page skipping; versions-host floating tags; the catalog view covers reuse rule; Groups::collect short-circuit; the candidate/full-catalog policy invariant; Candidates widen/covers; node-lts.

I did not re-measure live cold runs in this PR; the request and RSS numbers above are from before the fix.

View guided diff Turn on auto-fix

… a buffer

Cold lookups regressed after per-version dating (#342) asked Go's proxy,
Maven POMs and NuGet registration pages about every version ever
published, and stored each answer twice. Streamed derives read their
bodies a byte at a time.

- `Evaluator::candidates` names the versions policy can read a date or
  flag of: releases newer than the baseline evaluation compares against,
  wherever they sort, prereleases only when the current version is one or
  a `permitted` filter is set; every version when the baseline needs the
  catalog. The engine widens them per catalog key, and Go, Maven, NuGet
  registration pages and the versions host's tags describe only those;
  the rest are listed unchecked. A stored catalog view records the
  candidates it was read for and serves only lookups they cover.
- Per-version date requests go through `Cached::uncached`: no HTTP table
  reads or writes (release-dates state keeps the answer), with host
  permits, cooldowns, 429 retries, operator mirrors and credentials.
  Maven POMs are now read through the mirror that serves the metadata,
  and a POM 429 leaves its version unchecked instead of dated as undated.
- NuGet's registration index keeps each page's bounds; a page at or below
  every dependency's version is not read.
- An undated mirror tag that is no candidate (a floating `v3` below `v4`)
  no longer sends the whole tag catalog to GitHub.
- `json_document` reads through a 64 KiB buffer; the PEP 503 anchor scan
  is linear, and a private index page is sniffed so PEP 691 JSON no longer
  feeds the HTML scan.
- SBOMs, image configs, Packagist, RubyGems and NuGet derive through typed
  structs; nodejs.org's index is a `node-lts` derived document; Helm
  entries reduce as they stream.
- OSV listings are read concurrently and written in one transaction; tag
  objects likewise, per page.
- `Groups::collect` returns at once without Pkl packages; advisory ranges
  parse once per evaluation; prerelease filters compute the release text
  only for a `regex:` filter.

Every derived representation and the affected code-versioned states are
bumped in tests/derived-versions.tsv and tests/state-versions.tsv.
@sweepies
sweepies marked this pull request as draft November 24, 2026 06:35
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
count-patchula 6acbfb6 Commit Preview URL

Branch Preview URL
Nov 24 2026, 06:35 AM

@sweepies
sweepies marked this pull request as ready for review November 24, 2026 07:09
@sweepies
sweepies merged commit fd73111 into main Nov 24, 2026
13 checks passed
@sweepies
sweepies deleted the perf/cold-lookup-and-streaming branch November 24, 2026 18:41
@sweepies-ops sweepies-ops Bot mentioned this pull request Nov 24, 2026
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.

1 participant