Skip to content

feat(core): BatchStep — the generic N+1 eliminator - #5

Merged
srmcguirt merged 1 commit into
mainfrom
feat/generic-batch-step
Aug 1, 2026
Merged

srmcguirt merged 1 commit into
mainfrom
feat/generic-batch-step

Conversation

@srmcguirt

Copy link
Copy Markdown
Member

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. PgSelectStep does this for Postgres tables it writes SQL against. BatchStep is 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

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. (My first draft had exactly that bug; the test caught it.) 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.

One thing to review carefully

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 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 the PgSelectStep tweak-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

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>
@changeset-bot

changeset-bot Bot commented Aug 1, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 27108a5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@srmcguirt
srmcguirt merged commit f77948d into main Aug 1, 2026
2 of 4 checks passed
@srmcguirt
srmcguirt deleted the feat/generic-batch-step branch August 1, 2026 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant