Repository navigation
feat(core): BatchStep — the generic N+1 eliminator - #5
Merged
Merged
Conversation
Grafast's central trick is that a child field does not fetch per parent: it
declares what it needs per KEY, the engine collects every key in the batch,
issues ONE load, and hands each parent its own slice back. `PgSelectStep`
does that for Postgres tables it writes SQL against. This is the same idea
with the transport removed — the caller supplies the loader.
That gap mattered. An embedder whose reads must go through its own typed
seam (fw-api's fw-store, an HTTP backend, a cache) cannot use a step that
builds its own SQL, so batching — the single biggest win the planner was
supposed to deliver — was unreachable for exactly the consumers most likely
to embed the engine. They can now supply an async `Vec<K> -> Vec<V>`.
SHAPE
parent values [p0,p1,p2] --key_of--> keys, DEDUPLICATED
--load(keys)--> ONE call
--group_by--> bucketed per key
output values [Vec<V> for p0, Vec<V> for p1, ...]
GUARANTEES, each pinned by a test
* N parents cost ONE load, not N.
* Output is POSITIONAL: values[i] belongs to parent i. A parent with no
matching rows keeps an EMPTY slot rather than being dropped — dropping
would shift every later parent's children onto the wrong parent.
* Duplicate keys are requested once and answered for every parent naming
them. Buckets are SHARED via Arc rather than moved out of the map: the
obvious `remove()` hands the first parent the rows and every later one
an empty vec, silently, and only in the duplicate case. Sharing also
drops the `V: Clone` bound and copies nothing.
* An empty batch issues no load at all.
* Rows returned for keys nobody asked about are discarded rather than
attached to an arbitrary parent.
This deduplication is WITHIN one step's batch and is distinct from the
optimizer's step-level dedup (which merges whole steps with equal
fingerprints). Neither subsumes the other.
`config_hash` carries whatever parameterizes the load beyond the parent
dependency (a limit, a language, a tier). Two BatchSteps over the same
parent with the same config_hash WILL be merged, so a value that changes
the rows must be represented there — documented on the constructor.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Grafast's central trick is that a child field doesn't fetch per parent — it declares what it needs per key, the engine collects every key in the batch, issues one load, and hands each parent its own slice back.
PgSelectStepdoes this for Postgres tables it writes SQL against.BatchStepis the same idea with the transport removed: the caller supplies the loader.Why this gap mattered
An embedder whose reads must go through its own typed seam — fw-api's
fw-store, an HTTP backend, a cache — cannot use a step that builds its own SQL. So batching, the single biggest win the planner was supposed to deliver, was unreachable for exactly the consumers most likely to embed the engine.They can now supply an async
Vec<K> → Vec<V>.Shape
Guarantees, each pinned by a test
values[i]belongs to parenti. A parent with no matching rows keeps an empty slot rather than being dropped; dropping would shift every later parent's children onto the wrong parent.Arcrather than moved out of the map — the obviousremove()hands the first parent the rows and every later one an empty vec, silently, and only in the duplicate case. (My first draft had exactly that bug; the test caught it.) Sharing also drops theV: Clonebound and copies nothing.This deduplication is within one step's batch and is distinct from the optimizer's step-level dedup (which merges whole steps with equal fingerprints). Neither subsumes the other.
One thing to review carefully
config_hashcarries whatever parameterizes the load beyond the parent dependency — a limit, a language, a tier. TwoBatchSteps over the same parent with the sameconfig_hashwill be merged by the optimizer, so any value that changes the rows must be represented there. Documented on the constructor, but it's the same footgun class as thePgSelectSteptweak-fingerprint bug fixed in #3.Workspace: 412 passed, 0 failed.
First consumer: fw-graph's lemma→senses fan-out, where the naive shape is N+1 across 113k sense rows.
🤖 Generated with Claude Code