Skip to content

perf(binary-index): resolve a persisted subject's Sid without a reverse lookup - #2119

Merged
bplatz merged 3 commits into
mainfrom
perf/subject-sid-without-reverse-lookup
Oct 19, 2026
Merged

bplatz merged 3 commits into
mainfrom
perf/subject-sid-without-reverse-lookup

Conversation

@bplatz

@bplatz bplatz commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Turning a persisted subject id into a Sid read the subject's IRI from its forward pack, then searched the subject reverse dictionary for that IRI to recover the Sid it 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_parts skips 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_sid derives 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 take find_subject_sid, with the encode_iri fallback, as before:

  • full-IRI namespaces (EMPTY, OVERFLOW);
  • a subject whose IRI the current split rules would split differently;
  • a prefix the reverse namespace map assigns to another code.

Novel subjects still resolve from DictNovelty first; that path is unchanged. BinaryGraphView::resolve_subject_sid_uncached and ExportResolver::resolve_subject_sid now 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 next at 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 matched next on every workload. Unencrypted, 256 MiB cache, full 8-workload suite, mean of passes 3–4, three runs, this change ÷ fast path off:

Run 1 Run 2 Run 3
Suite total 0.92 0.98 0.82
dict_city (output-heavy) 0.93 0.96 0.79
output_bio (output-heavy) 0.87 0.96 0.83
point 1.66 1.87 1.65
hop 1.07 1.17 1.02

In ms, point was 102–118 against 62–63.

point and hop alone (same conditions, no output-heavy workload between passes), this branch against next, three runs: point 9.9 ms against 10.6 ms, hop 11.2 ms against 11.7 ms.

No query result differed between builds in any run.

…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.
@bplatz bplatz added the performance Performance improvement (release notes: Performance) label Oct 14, 2026
@bplatz
bplatz requested a review from a team October 14, 2026 22:01

@aaj3f aaj3f left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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 BinaryIndexStore method now owns the rule and both named call sites delegate to it. It sits on the canonical-encoding invariants in ns_encoding.rs rather than adding a cache. One copy is left in binary_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 (CI wasm32 / wasm-smoke green).
  • Testing: ✔ The new unit test runs, and each of its three guards goes red independently under mutation. CI's test (nextest, --all-features), clippy, fmt and testsuite-sparql are 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> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 81dce70

// 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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 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.

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.
@bplatz
bplatz merged commit 1d06ae2 into main Oct 19, 2026
19 of 20 checks passed
@bplatz
bplatz deleted the perf/subject-sid-without-reverse-lookup branch October 19, 2026 16:37
aaj3f added a commit that referenced this pull request Oct 20, 2026
aaj3f added a commit that referenced this pull request Oct 20, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance improvement (release notes: Performance)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants