Repository navigation
Description
Status (checked on next at 5424d0a, 2026-11-14)
A bare per-leaf count isn't enough. The issue assumes LeafEntry.first_key and last_key can resolve groups that span two leaves. Since 25e0721 they are routing bounds, widened over retracted history, not the keys of the first and last live rows. The leaflet directory's keys are widened the same way, and that already makes today's walk undercount (#2394). To stay exact without opening leaves, each SPOT/OPST leaf entry needs its count and its first and last live lead. That is 20–24 B more per entry (about 17–21%), against about 3.4% for a bare u32. The alternative is a "keys widened" flag with a fallback to the directory walk.
Other things to know before deciding:
- For the default graph, the leaf list is stored inline in the v5 root (
index_root.rs), so the field grows the root, and the root is re-fetched on every index publish. - The change is purely additive within v5. A v4 root (root version 2, branch version 1) keeps the directory walk, and since the first v5 build is a full rebuild, every v5 manifest would carry the field.
- The value is mostly cold reads on remote stores. Today the walk does one header-and-directory read per leaf: about 31K range reads per order at 7.8B triples on S3, and whole-leaf downloads on stores without ranged reads, such as the browser peer. Warm, it is a few ms. These figures are estimates, and the cold/warm benchmark this issue set as its bar has never been recorded.
- A leaf whose object-type range may hold NumBig objects should fall back to the directory walk instead of baking that rule into the format.
- PSOT/POST interior leaves could use the same field for per-predicate distinct counts.
Original description
Status: needs a worth-it determination
This is the "endgame" option for the sparqloscope number-of-subjects / number-of-objects shapes — filed to track the idea, not yet decided. The recent improvements (directory-only leaf opens with LeafletCache dir caching, parallel boundary-stitch reduce) already brought these queries from whole-index reads down to one header+directory pread per leaf. This issue is only worth doing if cold-cache latency on these query shapes still matters after those land in benchmarks.
Idea
Global COUNT(DISTINCT ?s) (SPOT) and COUNT(DISTINCT ?o) (OPST) are computed from leaflet-directory lead_group_counts with a boundary dedup that compares adjacent lead-key prefixes. LeafEntry in the branch manifest already carries full first_key/last_key (30 B each), so the boundary dedup needs zero leaf I/O — the only missing piece is each leaf's total lead-group count.
Adding one per-leaf lead-group total to the branch manifest makes the whole query a sum over the in-memory manifest: microseconds, cold or warm, no leaf opens at all.
Scope
Only SPOT and OPST branches need the field — they're the only consumers (global distinct subjects/objects). The per-predicate COUNT(DISTINCT ?o) fast path needs leaflet-granularity POST lead groups, which a per-leaf total can't serve, so PSOT/POST shouldn't pay for it.
A u32 suffices: default leaf capacity is 250K rows (leaflet_rows = 25_000 × leaflets_per_leaf = 10), so distinct leads per leaf ≤ 250K.
Size growth (the "bloat" cost)
Current serialized LeafEntry: 2×30 B keys + 8 B row_count + ~38 B leaf CID + 1 B sidecar flag ≈ 107 B (~145 B with a history sidecar). Adding a u32:
| scale | leaves/order (250K rows/leaf) | extra (SPOT+OPST) | relative manifest growth |
|---|---|---|---|
| 1B triples | ~4,000 | ~32 KB | ~3.7% of those two manifests (~430–580 KB each today) |
| 7.8B (Wikidata Truthy) | ~31,200 | ~250 KB | same ~3.7% |
| 20B+ (full Wikidata) | ~80,000+ | ~640 KB+ | same ~3.7% |
Every snapshot load pays this whether or not the query shape is ever used, but it's tens-to-hundreds of KB on manifests already loaded in full — unmeasurable against index sizes in the tens-to-hundreds of GB.
Real cost
The field itself is trivial; the actual work is the format change: BRANCH_V3_VERSION bump, back-compat decode (old branches → fall back to the existing dir walk, which is now cached and parallel), build-time population in the leaf writer, and tests. That's the bulk of the effort and the reason to confirm the win matters before doing it.
Decision input needed
Benchmark number-of-subjects / number-of-objects cold and warm on a large ledger after the dir-only + parallel changes. If cold runs are already acceptable (the dir walk is one small pread per leaf — thousands of preads, likely tens to low hundreds of ms), close this as not worth the format churn.
Activity
bplatz commentedon Nov 14, 2026
Decided 2026-11-14 (plan in #2394): v5 stores each leaflet's live first and last group in the leaflet directory. This issue's per-leaf summary builds on that field: a count plus the live first and last group per leaf in the branch manifest, combined with the same boundary rule. Requirements for the design: