Skip to content

perf(query): a GRAPH scope's columns leave as they are when nothing decodes - #2453

Merged
bplatz merged 3 commits into
mainfrom
perf/graph-scope-leave-unchanged
Nov 21, 2026
Merged

bplatz merged 3 commits into
mainfrom
perf/graph-scope-leave-unchanged

Conversation

@aaj3f

@aaj3f aaj3f commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

A GRAPH block got about 0.5% more expensive when #2214 landed, and this gives it back.

GraphOperator::next_streamed hands each scope column back through ScopeCrossing::leave, which decodes a binding only when the outer context can't read the scope's store (a cross-ledger body, say). #2214 made leave fallible, so every binding of every scope column started going through it and a collect::<Result<..>> adapter (GenericShunt, which stays out of line per item). That happens even in the common case, where both sides read one store, the crossing has no decoder, and leave hands the binding back untouched.

So ScopeCrossing gets leaves_unchanged() and leave_column(). A column that leaves unchanged is cloned outright, the way enter_batch already short-circuits on the way in. In the batched path, where the scope owns the inner batch, the columns are moved out of it instead (ScopeCrossing::leave_batch, from review). Otherwise each binding still goes through leave, via try_collect_vec, the plain loop we use on the other per-row key paths. Both GraphOperator paths (batched, and per parent row) use it. The third leave site, SERVICE's row merge, pushes with ? per binding and has no adapter, so I left it alone.

Numbers

Single-thread instructions:u, per query, on a quiet c7i.8xlarge, fat-LTO release builds, 6 interleaved reps each. The shape is SELECT ?s ?n WHERE { GRAPH <urn:g1> { ?s ex:name ?n } } over 5,000 subjects.

per query vs b38268f33
main before #2214: fe5febcb9 / 4adc62e89 / f42ea3784 / b38268f33 153.50M / 149.42M / 149.40M / 149.33M
88c3cf134 (#2214's merge) 150.13M +0.54%
main 0824793ba 150.11M +0.52%
this PR on 0824793ba 148.99M −0.23%

So that's −0.73% against main. The three mains right before #2214 agree within 0.06%, and both after it sit at +0.5%, so the step is #2214's, not layout. In the profile, the GenericShunt and per-binding leave samples (1.2% of main's) are gone, replaced by leave_column's clone loop at 0.27%. The controls are flat: the subject probe at +0.01%, the count plan at 0.00% and the object probe at −0.05%. The subject probe's own +0.22% at #2214 sat inside the spread of the earlier mains, and today's main runs it 1.5% under them (I'd guess #2404's leaf skip), so there was nothing to fix there.

Tests

a_column_leaves_unchanged_only_when_nothing_decodes pins both sides. With no decoder, a column (whole or by rows) comes back as it is. With one, an encoded binding still goes through it, and over an empty store it fails rather than leaving encoded. With leave_column forced onto the clone path, the test fails. fluree-db-query's unit tests, plus grp_query, grp_graphsource (the cross-ledger GRAPH and SERVICE scopes) and it_multigraph_lanes, all pass. Clippy and fmt are clean.

…ecodes

A GRAPH block hands its rows back through `ScopeCrossing::leave`, which
decodes only when the outer context can't read the scope's store. Since
`leave` became fallible, every binding of every scope column went through
it and a `collect::<Result<..>>` adapter, even when both sides read one
store and nothing could change. That is the common case, and it cost a
GRAPH block over one ledger about 0.5% more instructions than before.

`leave_column` clones such a column outright, as `enter_batch` already
does on the way in, and decodes the others with `try_collect_vec`. Both
`GraphOperator` paths (batched, and per parent row) use it.
Pins both sides of `leave_column`: with no decoder a column (or rows of it)
comes back as it is; with one, an encoded binding still goes through it and
fails over an empty store instead of leaving encoded.
@aaj3f aaj3f added area:query Query execution, planning, fast paths, overlay, result formatting performance Performance improvement (release notes: Performance) labels Nov 18, 2026
@aaj3f
aaj3f requested a review from bplatz November 18, 2026 20:28

@bplatz bplatz 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.

Approving. Please review the inline comments before merging.

Comment thread fluree-db-query/src/graph.rs Resolved
… decodes

The batched GRAPH path built its output by copying every column of the
inner batch, which it owns and drops right after: the parent columns with
`to_vec`, and the scope's own through `leave_column`, which clones when
nothing decodes. When nothing decodes and the batch holds every column of
the scope's schema, `ScopeCrossing::leave_batch` now hands the batch to
`project_owned`, which moves the columns and drops the rest; otherwise it
builds the columns as before.
@bplatz
bplatz merged commit 2aa1ab1 into main Nov 21, 2026
21 checks passed
@bplatz
bplatz deleted the perf/graph-scope-leave-unchanged branch November 21, 2026 17:06
bplatz added a commit that referenced this pull request Nov 21, 2026
Brings #2453 (a GRAPH scope's columns leave as they are when nothing
decodes), #2455 (a governed graph source honors its model's override
control), #2467 (R2RML scan join emit; iceberg top-k read budget) and
#2468 (vector identity follow-up: the whole-graph distinct-object count
counts vectors by value, a cold identity-table build fetches its arena
in one round, and the build stops on cancellation).

Conflicts:
- fast_count.rs: main's vector count on next's lead walk.
  count_distinct_lead_groups_inner tallies vector-only leaflets apart
  from the rest, and the NumBig liveness helpers take an ArenaRange (a
  store's NumBig o_types, or the vector o_type alone), so they serve
  count_distinct_vector_objects as main's generalized helpers do.
  count_distinct_objects_for_predicate takes next's tracker and main's
  cancellation, as do its callers in fast_predicate_scalar_agg.rs and
  fast_whole_graph_agg.rs.
- fast_group_count_firsts.rs: next's pooled-graph guard with main's
  answer_or_decline.
- o_type.rs: next's NumBig wording plus main's paragraph on arena
  contents.
- fluree_ext.rs: next's SourcePolicy and model-instance check, with
  main's async config and the model's override control.
- core lib.rs: main's MemoryCharge re-export; annotation_index has no
  home on next.
- Cargo.toml, regression-budget.json: both benches.

Ports:
- vector.rs: register_missing asks ContentStore::get_local whether a
  shard is resident; next has no resolve_cached_bytes, and a shard's
  cid is not optional.
- Tests: the residency vector arena stores its shards through
  write_test_shard_in, and BinaryIndexStore::empty() takes no
  directory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:query Query execution, planning, fast paths, overlay, result formatting performance Performance improvement (release notes: Performance)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants