Repository navigation
perf(binary-index): resolve a persisted subject's Sid without a reverse lookup - #2119
Conversation
…se lookup Turning a persisted subject id into a Sid read the subject's IRI from its forward pack, then searched the reverse dictionary for that IRI to find the Sid it is stored under. Every subject a query materializes paid both lookups; under cache pressure each reverse lookup could decrypt or reload a 2 MiB reverse leaf. When the canonical split of the IRI is exactly the parts the subject is stored under, those parts are the answer the reverse dictionary would give, so they are returned directly. A full-IRI namespace (EMPTY, OVERFLOW) or a split the current rules do not produce still takes the lookup. The graph view and the export resolver share the one rule.
aaj3f
left a comment
There was a problem hiding this comment.
@bplatz nice, tight change, and at a design level no pushback from me. The canonical-split check is exactly the condition under which the reverse probe can only hand back the parts we just read, and I couldn't make the two disagree on any real ledger I tried: cold, incrementally indexed, or the v4.2.3 fixtures as written and reindexed. Just one small note below about a third copy of the same resolution that's still outside the new method. Good to merge whenever you're ready.
Full review below:
✅ Approve with nits.
This is a nice, carefully scoped perf change, @bplatz. The guard is exactly the condition under which the reverse dictionary can only return the stored parts, so skipping the probe can't change an answer. Everything else (full-IRI namespaces, legacy splits, a prefix mapped to another code) keeps the old lookup verbatim. I checked it the empirical way too. A scratch test compared the old lookup, subject_sid_of_parts, and BinaryGraphView::resolve_subject_sid for every persisted subject of:
- a fresh file-backed ledger reloaded cold, with 36 subjects covering blank nodes, named-graph-only subjects,
urn:/did:/mailto:/tag:IRIs, trailing/and#, unicode hosts, and subjects added in novelty and then incrementally indexed; - the v4.2.3 bulk-import fixture as written and after a reindex;
- the v4.2.3 vector fixture.
All three agreed on every subject, and every real subject took the fast path. The only note is optional: one leftover copy of the old resolution in binary_scan.rs.
Adherence to repo commitments:
- Patterns/abstractions: ✔ One
BinaryIndexStoremethod now owns the rule and both named call sites delegate to it. It sits on the canonical-encoding invariants inns_encoding.rsrather than adding a cache. One copy is left inbinary_scan.rs:1167(🟡 above). - Performance (speed first, memory second): ✔ Removes one reverse-tree probe (and its possible leaf reload) per materialized persisted subject. The remaining work equals what the old path already did, and the slow branch adds one split and one hash probe, which no real subject I checked reached. Memory is unchanged. I couldn't reproduce the 4–7% number (that harness isn't in the repo), but the unit test pins the skipped probe. No performance-degradation risk.
- Deployment targets: ✔ n/a. This is an in-process read of persisted dictionaries with no new I/O, caches or background work, so it behaves the same embedded, in
fluree-db-server, and on wasm32 (CIwasm32/wasm-smokegreen). - Testing: ✔ The new unit test runs, and each of its three guards goes red independently under mutation. CI's
test(nextest,--all-features),clippy,fmtandtestsuite-sparqlare green on this head. - Conventions: ✔ The multi-line commit body explains the invariant and the fallbacks, and the doc comments match the code.
Verified locally at 09aea1bad: the new unit test by name; three guard mutations each red and then restored; a cold-index old-vs-new comparison over every persisted subject of a fresh file-backed ledger and both v4.2.3 fixtures (0 mismatches); both call sites and the remaining find_subject_sid callers traced.
Approving for whenever you're ready. The binary_scan.rs copy is worth folding in if you agree, but it doesn't need to hold anything.
| /// key, so the lookup `find_subject_sid` would make returns them. A | ||
| /// full-IRI namespace, or a split the current rules do not produce, takes | ||
| /// the lookup. | ||
| pub fn subject_sid_of_parts(&self, ns_code: u16, suffix: String) -> io::Result<Sid> { |
There was a problem hiding this comment.
🟡 Optional: there's still a third copy of the old s_id → Sid resolution in fluree-db-query/src/binary_scan.rs:1167-1182.
BinaryScanOperator::resolve_s_id still does resolve_subject_iri + find_subject_sid + encode_iri, the exact sequence this PR replaces in the graph view and the export resolver. It isn't hot: its only callers are the repeated-variable checks in within_row_var_equality_ok (?s ?s ?o, ?s ?p ?p), cached per scan. So this is about having one rule rather than about speed. Routing it through resolve_subject_parts + subject_sid_of_parts would make the "graph view and export resolver share the one rule" true for every persisted-subject site. Minor and non-blocking, but if you agree it's right, I'd rather see it folded in now than lost in the backlog. Commenting here because fluree-db-query/src/binary_scan.rs is not in this diff.
| // Same prefix as EX; the reverse map names EX for it. | ||
| const DUP: u16 = 102; | ||
| // Its IRI's canonical prefix maps back to it, with another suffix. | ||
| const EX_C: u16 = 103; |
There was a problem hiding this comment.
👍 Nice test design: each guard is pinned by its own case. I mutated them one at a time and the test went red at a different subject each time. Always taking the fast path fails at b/c, dropping the namespace_reverse check fails at carol, and dropping canonical_suffix == suffix fails at /d, the case this constant sets up.
…out-reverse-lookup
A pattern that repeats a variable across subject or object and predicate (`?x ?x ?o`, `?s ?p ?p`) compares the two as Sids. The scan resolved the subject side with its own store-only lookup, so on an indexed ledger a subject first written in novelty, whose id sits above the persisted watermark, failed the query with "subject local_id N not found". The check now takes the batch's BinaryGraphView and calls resolve_subject_sid, which reads novel ids from DictNovelty and persisted ones through subject_sid_of_parts. That was the last s_id -> Sid copy outside the shared rule; the scan's own sid cache goes with it, and the equality check and the subject binding now share the view's memo.
…ferences Brings in #2009 and #2119. The two PRs each grew a typed graph-name module and an answer for the ledger's own address in a graph position; the merge keeps one of each: - fluree-db-core dataset_ref: #2007's module, a superset of #2009's GraphIri/GraphSel, with #2009's tests. GraphSel::bare_keyword is the keyword table as a JSON-LD graph name is written (`urn:default` is an IRI there); GraphSel::keyword adds `urn:default`. - transact: #2009's lexical GraphScope/WriteGraphs and its JSON-LD parser, without #1997's template-default flag (staging reads every scope through the graph-position table); the parser keeps a ledger address under strict compact-IRI checking and notes it, and reads `from`/`fromNamed` as dataset references. The reifier anchor follows its edge to the graph the table reads the scope as. - A top-level `values` cell reads through #2009's ledger encoding; this branch's IriMatch carrier is dropped, as planned for whichever landed second.
Turning a persisted subject id into a
Sidread the subject's IRI from its forward pack, then searched the subject reverse dictionary for that IRI to recover theSidit is stored under. Every subject a query materializes paid both lookups — result formatting and the export resolver both go through this path — and under cache pressure each reverse lookup can reload a reverse-dictionary leaf.BinaryIndexStore::subject_sid_of_partsskips the reverse lookup when it cannot change the answer: when the canonical split of the subject's IRI is exactly the(ns_code, suffix)the subject is stored under, and the canonical prefix maps back to that code.find_subject_sidderives its reverse key from that same split, and every persisted subject has its reverse entry, so the lookup would return those parts. These cases still takefind_subject_sid, with theencode_irifallback, as before:EMPTY,OVERFLOW);Novel subjects still resolve from
DictNoveltyfirst; that path is unchanged.BinaryGraphView::resolve_subject_sid_uncachedandExportResolver::resolve_subject_sidnow share the method.The new unit test resolves one subject of each kind. It checks each result against what the reverse lookup gives, and that only the canonical subject skipped the lookup.
Removing these redundant reverse lookups improves output-heavy queries, but it also removes incidental warming of reverse-dictionary leaves. Under a constrained cache, point and hop queries that follow output-heavy ones in a mixed workload can therefore be slower, although isolated point and hop runs show no regression. The measured suite improves overall. Restoring the lookups for their warming side effect is not the fix; if representative workloads justify warming reverse leaves, that should be an explicit, separately measured optimization.
Validation
All on a 3M-subject synthetic graph in a file store, m7a.4xlarge, each run in a fresh process.
First measurement (OS page cache warm, no memory limit): neutral with a large cache; 4–7% faster per workload pass under a 256 MiB cache, where reverse leaves were being evicted and reloaded.
Mixed suite under a constrained cache. Measured since on a branch built on
nextat ddbfb75 that includes this change, under a 1 GiB memory cgroup with the OS page cache dropped first. The change's effect is isolated by running that branch with only this fast path turned off; with it off, the branch matchednexton every workload. Unencrypted, 256 MiB cache, full 8-workload suite, mean of passes 3–4, three runs, this change ÷ fast path off:dict_city(output-heavy)output_bio(output-heavy)pointhopIn ms,
pointwas 102–118 against 62–63.pointandhopalone (same conditions, no output-heavy workload between passes), this branch againstnext, three runs:point9.9 ms against 10.6 ms,hop11.2 ms against 11.7 ms.No query result differed between builds in any run.