Specialize TreeWalker with struct key comparers - #31
Open
hadashiA wants to merge 1 commit into
Open
Conversation
This was referenced Aug 8, 2026
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
Replaces the central guarded-devirtualization hack (
KeyCompare) with value-type generic specialization:TreeWalkeris now an abstract non-generic base plussealed TreeWalker<TComparer> where TComparer : struct, IKeyComparer. Each comparer gets its own runtime instantiation, so every comparison in the B+Tree search loops is a direct, inlinable call — on JIT and on AOT targets (IL2CPP) alike — andSupportsKeyDigestfolds to a JIT-time constant, removing the digest branch from the binary search loops.IKeyComparer+ struct comparers (Int64KeyComparer,AsciiKeyComparer,Uuidv7KeyCompareron .NET 9+,FallbackKeyComparer,DuplicateKeyComparer) inInternal/KeyComparers.cs. Built-in comparers delegate to the sealed encoding singletons, so comparison logic stays in one place.LeafNodeReader/InternalNodeReadersearch methods takeTComparerinstead ofIKeyEncoding.TreeWalker.Createfactory, so IL2CPP generates them statically. Holders (tables, index queries, iterators) keep referencing the non-generic base — public API is unchanged; virtual dispatch is one call per query instead of one per comparison.FallbackKeyComparerand perform exactly as before. Non-unique secondary indexes get an inlined composite comparer instead of the previous double dispatch.Benchmark
ReadBenchmark (Release, solo run, Apple M-series / .NET 10), vs current README numbers:
RandomKeys (-11%) is the most reliable signal since it defeats the branch predictor — consistent with removing per-comparison branches. Parallel variants are within their usual noise. All read paths remain allocation-free; 75/75 tests pass on net8.0/net10.0/netstandard2.1.
🤖 Generated with Claude Code