Lazy join demand is recomputed over all demanded keys on every batch (O(N) per batch)
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 25/100
- Tipo di issue
- Refactoring
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- typescript
- Ambito
- database
Direzione di ricerca
Start in packages/db/src/query/compiler/joins.ts at the demand tap (around line 502), where the whole demanded key set is rebuilt and passed to setDemand on every batch. Then read setDemand, canonicalizeKeys and equalKeySets in packages/db/src/query/live/subset-demand-controller.ts. Done means a batch's demand cost scales with the keys it changes, which you can check by timing writes of about 21k rows in 2,000-row batches.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Validated against @tanstack/db 1.0.0-rc.1 and 1.0.0-rc.1.1.
The same code is on main (15d2d2d) in packages/db/src/query/compiler/joins.ts and packages/db/src/query/live/subset-demand-controller.ts.
This is a performance issue, not a correctness bug.
What happens
For a lazy join, the tap on the active pipeline (joins.ts, around line 502) runs on every batch the active side emits. After updating demandWeights for the batch, it rebuilds the whole demanded key set and passes it to setDemand:
const keys = new Set(
[...demandWeights.values()].filter(({ weight }) => weight > 0).map(({ key }) => key),
)
callbacks[target.sourceId]?.setDemand?.(demandPlans[index]!, keys)
SubsetDemandController.setDemand then makes several more passes over the whole set:
canonicalizeKeys(keys)serializes every key;equalKeySets(previous.keys, nextKeys);coveredKeysflattens every segment's keys;addedfilters every key, andremovedscans every previous key.
So each batch costs O(total demanded keys) per lazy join, even when the batch adds only a few keys.
Writing N rows into the active side in batches of size B costs about O(N² / B) per lazy join.
Measured
A local-first app writes ~21k rows into a collection that is the main source of a live query with several lazy left joins (all collections fully loaded locally, eager sync).
The rows go in batches of 2,000 (one begin/write/commit per batch) so the page stays responsive.
Chrome profile of the main thread while those batches are written (~2.9 s busy):
| Function | Self time |
|---|---|
canonicalizeKeys (subset-demand-controller.js:167 in dist) |
228 ms |
setDemand |
96 ms |
the demand tap in joins.js |
133 ms |
That is ~450 ms, about 16% of the work, spent recomputing demand that changed by one batch.
It also stops us from using smaller batches, which would keep input latency lower: with 500-row batches the same write takes ~4 s instead of ~1.6 s, because every batch pays the full O(N) demand cost again.
Possible fix
Make demand incremental, so a batch costs O(keys it changes):
- In the
tap, record which keys crossed from weight 0 to positive (added) and from positive to 0 (removed) in this batch. If neither set is empty, call the controller with them; if both are empty, skipsetDemandentirely. - Give
SubsetDemandControlleran incremental entry point, e.g.updateDemand(subscription, plan, added, removed), that keeps its canonical key map across calls, canonicalizes only the changed keys, and requests a segment only for added keys no live segment covers. Removals and failed segments can keep today's full path.
Happy to provide a reduced repro or a PR if that helps.
- Lingua principale
- TypeScript
- Stelle
- 4k
- Fork
- 277
- Merge medio
- 1g 7h
- PR unite (30g)
- 191
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di TanStack/db
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 88/100
I maintainer di solito rispondono entro 1 giorno
-
OfflineExecutor.dispose() never settles pendingTransactionPromisesForse già presa @Claire-ovo l’ha presa 6 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
TanStack/db#2254 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Two @tanstack/db tests time out under CPU load because a cold module import counts against the 5 s default timeoutForse già presa @jp-knj l’ha presa 4 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno
-
Disabled useLiveQuery fails SSR hydration with error 253Forse già presa @Jaime02 l’ha presa 1 giorno fa. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 42/100
I maintainer di solito rispondono entro 1 giorno
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
ehmpathy/rhachet-roles-ehmpathy#1231 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
Equal-Vote/bettervoting#1754 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
Doist/todoist-sdk-typescript#740 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 62/100
privatenumber/cleye#47 ·
-
spec-feedback
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100