Repository navigation
Cache B-tree nodes while applying an async batch - #306
Conversation
79dc8ca to
220c518
Compare
8ef5a12 to
791bdb6
Compare
commit: |
6b09aeb to
f80b0e1
Compare
4f93c01 to
dcbf793
Compare
26ee7df to
e363966
Compare
Most reads in a batch re-read a node the batch already read, since consecutive writes to a tree share its root and internal nodes. Route the B-tree's node reads and writes through a typed NodeStore, and give the batch worker one that keeps nodes in memory for the batch so those reads skip the database. The worker stops a cycle once the cache holds 8 MiB of nodes, which keeps it well under the JavaScript heap limit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
e363966 to
1168555
Compare
|
This seems reasonable, but i'm curious how much of this is already happening automatically? Given that multiple db.get requests will short-circuit and db.patch just changes the local proposed data on each change. And we mark resource limits as de-duped fetches, so I don't think this will keep them from hitting that limit. |
|
Or is the thesis that the syscall overhead contributing to the 1s timeout? |
|
Yeah, the thesis is that the syscall overhead contributes to the timeout, especially repeated db.get calls to the same node (and copying the objects over into JS each time). I had an agent benchmark it and the caching helps a lot with throughput |
| await this.db.replace( | ||
| table as TableName, | ||
| id as Id<TableName>, | ||
| value as WithOptionalSystemFields<Doc<TableName>>, | ||
| ); |
There was a problem hiding this comment.
why aren't we updating the cache in this scenario?
There was a problem hiding this comment.
ah we're skipping all replaces on this table, nm
Gives the batch worker a
NodeCachingDatabaseWriter, a wrapper aroundctx.dbthat keeps B-tree nodes in memory for the batch and passes other tables through, since most reads in a batch re-read a root or internal node that an earlier write already read. On top of #305, this cut document reads per 1024-op batch from about 7.4k to 4.2k in the same load test.Stack
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
🤖 Generated with Claude Code