Speed up ranges, async reads and database open - #29
Merged
Conversation
This reverts commit a7d7127.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up read-path optimizations, all measurement-driven (BenchmarkDotNet, Apple M-series, 10k rows / 4 KB pages):
(1 op = 1000 point queries / 100 range queries.)
Retain range result pages once per leaf
RangeResult.Addretained the page per row; rows arrive leaf by leaf, so a 100-row scan paid ~200 interlocked ops on the same hot page. Retaining once per consecutive page run cuts that to one pair per page.Sync completion fast path for GetAsync
Async lookups paid the async state machine even when every page was cached — plus an
ArrayPoolrent/return per call in the typedReadOnlyTable.GetAsyncoverloads.GetAsyncnow attempts the whole walk against cached pages first (TreeWalker.TryGetFromCache), completing synchronously in the common case; the typed overloads encode the key on the stack and only fall back to the pooled-buffer async path on a cache miss.Clamp page cache capacity by data length
The S3-FIFO eviction queues and ghost table were sized by
CacheSize / PageSize(512k entries with the 2 GB default), preallocating ~20 MB per open regardless of database size. The capacity is now additionally bounded by the number of pages the file can possibly contain (length / 32). Startup latency drops 62x for small databases; the test suite got ~7x faster as a side effect (every test opens a database).Benchmarks added
Async point lookup, random-key point lookup (defeats the branch predictor, unlike the fixed-key variants), and open + first query.
Note: branchless binary search was tried and rejected
Converting the node binary searches to conditional-select form made random-key lookups slower (35 → 46 µs): it trades branch mispredictions for a serialized load-address dependency chain, losing speculative load-ahead. Kept in history (
a7d7127+ revert) as a measurement record.All 73 tests pass.
🤖 Generated with Claude Code