Skip to content

Fix cache-thrash livelock and speed up reads and counts - #28

Merged
hadashiA merged 7 commits into
mainfrom
optimize-gc-page-reclamation
Jul 11, 2026
Merged

Fix cache-thrash livelock and speed up reads and counts#28
hadashiA merged 7 commits into
mainfrom
optimize-gc-page-reclamation

Conversation

@hadashiA

Copy link
Copy Markdown
Owner

Summary

Read-path improvements that keep the original design (reference counting + deterministic buffer pooling, on every platform including Unity):

Case Before After
Point lookup (single thread) 20.2 µs 19.1 µs
Point lookup (8 threads, spread keys) 185.6 µs
Point lookup (8 threads, same key — worst case) 1,440.9 µs 827.2 µs (1.7x)
CountRange (8,000 rows) 1,524.6 µs 160.3 µs (9.5x)
GetRange (100 rows) 140.0 µs 100.9 µs (1.4x)

(1 op = 1000 queries for point lookups, 100 for range/count. All read paths remain zero-allocation.)

PageCache.GetOrLoad — fixes a latent cache-thrash livelock

The old miss protocol (while (!TryGet(pn)) Load(pn)) is unsound under thrash: a loaded page can be evicted by concurrent loaders before the loading thread looks it up again, so concurrent loaders keep evicting each other's pages and nobody makes progress. A new stress test (8-page cache, concurrent point reads + range scans) spins forever on main.

GetOrLoad / GetOrLoadAsync now publish the entry with the caller's reference already taken (RefCount = 2), so the returned page stays valid even if it is evicted immediately after. All ~15 retry loops across TreeWalker / RangeIterator / secondary index queries are gone, which also simplifies the tree-walk code considerably. TreeWalker.TrySearch's miss/resume protocol is replaced by self-sufficient Search / SearchAsync.

Two more hazards fixed along the way:

  • The S3-FIFO frequency bump must stay a CAS (a plain write cancels the evictor's second-chance decrement, making hot entries unevictable — another livelock).
  • The evict loop is now bounded per pass instead of spinning until under capacity.

Optimistic fetch-add refcount

TryRetainIfAlive used a CAS retry loop, which degrades badly under contention (retry storms on the hot page's cache line). It is now a single Interlocked.Increment with a dead-entry bias: a result ≥ 1 means alive; once the count reaches zero, Release stamps the entry with int.MinValue / 2 via a one-shot CAS before disposing, so late optimistic increments can never resurrect a dead entry. This is sound because the Entry object itself is GC-managed — only the buffer must not be touched after dispose.

Same-key parallel reads improve 1.7x; with realistically spread keys the refcount cost is within ~14% of having no refcount at all (a GC-based no-refcount mode was prototyped and measured, then dropped in favor of keeping one simple code path — see commit history).

Per-leaf binary search for range bounds

CountRange compared every entry against the end key; since leaf entries are sorted, the bound is now located with one binary search per leaf and whole leaves are counted by subtraction (9.5x). GetRange (both directions, sync and async) uses the same bound computation, removing the per-row key comparison.

Also

  • ReadOnlyDatabase.Dispose now releases the pinned root pages (fixes a native-memory leak of one page per tree with the Unity NativeArray loader).
  • PageCache no longer preallocates dictionary buckets for the full capacity (~MBs at open with the default 2 GB cache size).
  • New benchmarks: spread-key parallel lookup; eviction-pressure concurrency stress test.

All 73 tests pass, including the new stress test that deadlocks on main.

🤖 Generated with Claude Code

@hadashiA
hadashiA merged commit 3fdd4ce into main Jul 11, 2026
1 check passed
@hadashiA
hadashiA deleted the optimize-gc-page-reclamation branch July 11, 2026 23:58
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