Skip to content

perf(datasource): helm indexes, PyPI pages and image digests stream - #294

Merged
sweepies merged 6 commits into
mainfrom
perf/stream-rest
Nov 13, 2026
Merged

sweepies merged 6 commits into
mainfrom
perf/stream-rest

Conversation

@sweepies

@sweepies sweepies commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Follow-up to #288. Three transforms still held their whole response: Helm index.yaml, PyPI simple-index pages and container image digests. They now derive as the bytes arrive.

  • Helm index (crates/datasource/src/helm.rs): serde_yaml_ng::from_reader read the whole document and built a full value tree, about ten times the document's size. It is replaced by a pull-event walk over libyaml-safer's parser.
    • Decoding: libyaml-safer reads the stream as UTF-8, as serde_yaml_ng does.
    • Anchors and aliases are replayed, keeping only the fields the derive reads: version, created, digest, deprecated, sources and the artifacthub.io/changes annotation.
    • Scalars resolve as serde_yaml_ng's core schema did (booleans, hex, octal and binary integers, .inf/.nan, nulls), including its quirks: tagged scalars read verbatim as strings, a non-null !!null is an error, an empty plain scalar reads as an empty map or sequence, and the last duplicate key wins.
    • serde_yaml_ng becomes a dev-dependency of the datasource crate (the test oracle); libyaml-safer joins it.
  • PyPI simple pages: the JSON path already streamed. On a JSON failure the rest of the body drains through a bounded PEP 503 anchor scanner: tags are case-insensitive, quoted > is allowed in attributes, data-yanked is read, anchors may split across chunks, and malformed UTF-8 fails the document. The catalog output is unchanged.
  • Image digests: a HashRead updates SHA-256 per read while the document parses, and stores the finalised digest with no second pass and no retained body (image-platform, sbom-os).

No behaviour change.

Proof:

  • derive_parity runs every derive over every fixture body whole and in 997-byte and 1-byte chunks: byte-identical output for all five touched transforms, error cases included.
  • a_simple_page_scans_anchors_the_same covers the HTML fallback, which no fixture exercised. It also caught a bug in the first draft, now fixed.
  • Real captured documents give identical output hashes before and after: the bitnami Helm index (27.6 MB), the boto3, botocore and awscli simple pages, and torch's HTML page (987 anchors).
  • Versions bumped to 2 and guard rows updated: helm-index, pypi-index-catalog, image-platform, sbom-os, plus terraform-catalog and pypi-simple-catalog, whose listed source file changed.

Peak memory (release binary, VmHWM, VM):

document before after
bitnami index.yaml (27.6 MB) 274 MB 61 MB
boto3 simple (2.2 MB, JSON) 11.3 MB 9.2 MB
botocore simple (2.6 MB, JSON) 12.6 MB 10.1 MB
torch simple (508 KB, HTML) 6.4 MB 6.1 MB

View guided diff Turn on auto-fix

Parity with the buffered derive (review fixes)

Two review rounds compared origin/main's buffered derive with the walk. The first found ten divergence classes and a PyPI panic (a multi-byte character split at a chunk boundary). The second found a process abort and more divergences that came from yaml-rust2's scanner (tabs, %YAML versions and unknown directives, U+2028/U+2029, the flow-nesting cap).

The walk now reads events from libyaml-safer, a line-by-line safe-Rust port of the unsafe-libyaml that serde_yaml_ng uses, run against libyaml's test suite, with UTF-8 forced as serde_yaml_ng forces it. Scanner behaviour matches by construction; the walk only emulates serde's layer (scalar resolution, duplicate keys, the 128 recursion budget, alias replay, the alias jump bound). yaml-rust2 is gone from this crate.

  • A deeply nested anchored collection aliased into a kept field overflowed the stack and aborted the process. Recording past serde's depth now ends as "too deep", which fails only when replayed into a kept position, and replay is bounded, so no input can overflow the stack. Tested at 3,000 levels on a 2 MB thread.
  • Any panic inside the derive becomes a failed document.
  • The differential test: origin/main's derive copied verbatim as the oracle (serde_yaml_ng is a dev-dependency only), about 140 hand cases from both reviews plus 6,000 seeded documents, each fed whole and in 1-, 3-, 7- and 64-byte chunks; output must be byte-identical or both must fail. Zero divergences. The PyPI scan and image digests are checked at every chunk size 1–16 on non-ASCII pages.

Peak memory and time on the bitnami index (27.6 MB): 273.9 MB / 0.46 s buffered → 59.9 MB / 0.30 s streaming.

Nested anchors share one recording: each event is stored once while any anchor is open, and an anchor is a range into that buffer, so 128 nested anchors around a 10 MB value peak at 35.6 MB (it was 1.29 GB, and the old derive uses 52 MB).

Two residuals, both fail-closed and adversarial only, both asserted by tests:

  • serde bounds alias replay relative to the whole document's event count, which a stream only knows at the end. The walk checks serde's bound at stream end and also caps replay at 1M events, so a document that replays a small anchor more than a million times while staying within serde's bound fails here and succeeds in the old derive.
  • libyaml-safer 0.3.0 panics (panic!("TODO: What is expected here?") in its scanner) on a tag followed directly by , in a flow collection, such as [!x, a], where C libyaml emits the tag. The derive turns the panic into a failed document. Helm indexes don't contain it.

A stress run found no crash: 96 nesting cases up to 100k levels on a 2 MB stack, about 335k fuzzed inputs, and 4,000 mutations of a real index compared against the old derive.

Versions: helm-index, pypi-index-catalog, pypi-simple-catalog, terraform-catalog, image-platform, sbom-os → 3.

The walk sat on yaml-rust2, whose scanner is not libyaml; every scanner
divergence from the buffered serde_yaml_ng derive had to be compensated
in code. It now reads libyaml-safer's events — the safe-Rust port of the
unsafe-libyaml serde_yaml_ng runs — forced to UTF-8 the way serde's own
parser is, so scanner behaviour matches by construction and the Chars
filter, colon_in_flow tracking and FEFF column tricks are gone.

Anchored nodes are recorded whole as they close and an alias replays
into a stack of frames, the way serde's jump gives each alias its own
cursor, so a nested alias replays ahead of the outer frame's tail. A
recording past serde's 128-deep recursion budget ends as a too-deep
marker that fails only if aliased into a kept position — deep ignored
anchors still succeed — keeping clone, replay and drop shallow. The
whole derive runs under catch_unwind so a libyaml-safer panic is a
failed document, never a worker abort. Alias work keeps serde's
hundred-jumps-per-event bound checked at stream end, plus a fixed
replayed-event ceiling that only an alias bomb reaches; the residual is
documented and tested on both sides.

Differential coverage against the verbatim buffered derive now spans the
hand table from both review rounds — directives, tags, tabs, U+2028/29,
NEL, BOMs, UTF-16, control characters, flow nesting 249-300, alias
bombs, redefined and nested anchors, merge keys and duplicate keys
spelled differently — plus 6,000 generated documents, each at whole,
1-, 3-, 7- and 64-byte chunkings. Deep cases run on a 2 MB thread the
way production does. No case diverges.

On the real 27.6 MB bitnami index the walk peaks at 59.9 MB in 0.30 s
against the buffered derive's 273.9 MB in 0.46 s.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@socket-security

socket-security Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedcargo/​libyaml-safer@​0.3.010010097100100

View full report

sweepies and others added 2 commits November 13, 2026 11:41
Recording every event into every open anchored tree kept a copy per
enclosing anchor: 128 anchors nested around a scalar stored it 128
times, and ~60 MB of that shape aborted the process. Open recordings
now share one append-only event buffer and each anchor binds its
start..end span — a nested span covers the inner anchors' events
without holding them again — so anchored memory tracks the anchored
events, not the nesting. An abandoned past-budget recording is simply
never bound, which fails a kept alias the way serde's remaining-depth
check would, and an honest index records nothing.

On the real 27.6 MB bitnami index the walk still peaks at ~59.8 MB in
0.29 s against the buffered derive's ~274 MB in 0.41 s, same derived
bytes. On 128 nested anchors around a scalar the peak is 7.8 MB for a
1 MB scalar (was 148 MB) and 35.6 MB for a 10 MB scalar (was 1.29 GB),
under the buffered parse's 51.8 MB.

Also asserted as a residual: a tag immediately followed by `,` in a
flow collection, where libyaml-safer's scanner panics and C libyaml
emits the Tag token — the buffered parse accepts, the walk's
catch_unwind fails the document.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@sweepies
sweepies merged commit 64bd469 into main Nov 13, 2026
12 checks passed
@sweepies
sweepies deleted the perf/stream-rest branch November 13, 2026 13:33
@sweepies-ops sweepies-ops Bot mentioned this pull request Nov 13, 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