Repository navigation
Conversation
The dataloader placed each returned doc by scanning the full keys array with findIndex, once per doc — O(n²) over the batch size. Build a key->index Map once (keys is fixed for the batch) and look up in O(1), making it O(n). First occurrence wins, matching findIndex.
|
Friendly ping — is there anything you'd like changed here? For the record, I re-audited this patch against the "index rebuilt per call" pitfall a reviewer raised on a similar PR elsewhere: |
|
Closing this to keep my open PR count down — it has been sitting well past this repo's usual review turnaround, and I would rather not leave unsolicited changes cluttering the queue. The change is self-contained and still applies; happy to reopen if a maintainer wants to take a look. |
What?
Replaces the per-doc
keys.findIndex(...)scan in the collections dataloader with a singleMap<string, number>lookup, turning the doc-placement loop from O(n²) into O(n) over the batch size.Why?
batchLoadFnreceives all cache keys requested in one tick (keys: readonly string[]). After the batchedfindreturns, the current code places each doc back into the result array by scanning the entirekeysarray withfindIndex, once per doc:Total docs across all batches ≈
keys.length, and each placement scans all ofkeys, so the work isO(keys.length²).keysgrows with the number of documents loaded in a single tick — which, with depth-populated relations, can be hundreds or thousands — so this is quadratic on a hot batching path.How?
keysis fixed for the batch, so I build akey → indexmap once up front and look up in O(1):Behaviour is unchanged:
findIndexreturns the first match, and the map is built first-occurrence-wins to preserve that. Keys are unique per load in practice, so the guard is just defensive.Targeting
main(v4) as an enhancement, per the PR template.Fixes #