Skip to content

Commit 3cb50e7

Browse files
perf(sei-db/view): release a cache-miss reader before installing its value (#4764)
**Problem** - A FlatKV read that misses the view's read cache waits for `injectValueUnlocked` to take the cache's write lock and install the value before it gets its result. Under read-heavy load that lock is contended, so every cache miss also pays for the lock wait. **Change** - A successful miss result is sent to its waiter before the value is installed. A reader that arrives in between still finds the entry scheduled and takes the value from the same channel. - A failed read is still released only after the cache is taken out of service. **Impact** - Removes the install's lock wait from every cache-miss read on the execution path. Benchmarked on CLOB together with #4765 and the off-loop overlay index, the three changes raised throughput by about 8%. **Risk** - Ordering only: the waiter gets the value slightly before later readers can see it in the cache. Later readers already handle that state (scheduled entry, value on the channel), and `ResolveBatchUnlocked` uses the same ordering. **Tests** - `sei-db/db_engine/view/...` and `sei-db/state_db/sc/flatkv/...` pass with `-race`. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 9ef78ac commit 3cb50e7

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

‎sei-db/db_engine/view/read_cache.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,13 @@ func (c *readCache) ResolveBatchUnlocked(pending []pendingRead, results map[stri
398398
// bound at scheduling time (see Resolve), which is the one every waiter on this read is blocked
399399
// on; e.valueChan may already have been detached by then.
400400
func (e *cacheEntry) injectValueUnlocked(key []byte, ch chan readResult, result readResult) {
401+
// A successful read is released before it is installed; a reader arriving in between finds the
402+
// entry still scheduled and takes the value from ch. A failed read is released below, once the
403+
// cache is out of service.
404+
if result.err == nil {
405+
ch <- result
406+
}
407+
401408
c := e.cache
402409
c.lock.Lock()
403410

@@ -430,7 +437,9 @@ func (e *cacheEntry) injectValueUnlocked(key []byte, ch chan readResult, result
430437

431438
// Release the waiter before bricking. reportReadFailure acquires the manager's versionLock, and
432439
// nobody may be blocked on us while we wait for it.
433-
ch <- result
440+
if result.err != nil {
441+
ch <- result
442+
}
434443

435444
if failure != nil {
436445
c.reportReadFailure(failure)

0 commit comments

Comments
 (0)