Replace segcache hashtable with lock-free N-choice implementation#10
Merged
thinkingfish merged 7 commits intopelikan-io:mainfrom Apr 16, 2026
Merged
Conversation
Replace the Twitter-copyrighted single-choice chaining hashtable with a new lock-free implementation based on crucible's MultiChoiceHashtable. Key changes: - N-choice hashing (2-choice default) replaces overflow chain buckets - All 8 slots per bucket are item slots (no metadata slot), enabling SIMD - SIMD tag scanning on AVX2 (x86_64) and NEON (aarch64) with scalar fallback - Lock-free AtomicU64 CAS operations on every slot (prep for dropping &mut self) - Bucket and segment data prefetch for latency hiding - In-table ghost entry support via Location::GHOST sentinel - Storage-agnostic design via KeyVerifier trait + opaque Location type - SegmentsVerifier adapter bridges existing Segments to new KeyVerifier This is Phase 1 of a multi-phase effort to replace all Twitter-copyrighted code with clean implementations. The hashtable module files are entirely new code. Files in segments/, ttl_buckets/, and the orchestration layer have minimal signature changes to work with the new hashtable API and will be fully replaced in subsequent phases. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The repo-level LICENSE-MIT and LICENSE-APACHE files cover all code. Per-file headers are unnecessary for new files that have no Twitter copyright to preserve. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add optional `loom` feature with 8 model-checked concurrency tests: - Concurrent insert of different keys - Concurrent insert of same key (upsert) - Concurrent lookup with frequency updates - Concurrent insert and remove - Concurrent CAS operations (2-way and 3-way) - Direct slot CAS contention - Three-way insert of different keys The sync module conditionally uses loom atomics when the feature is enabled, and SIMD tag scanning falls back to scalar under loom since loom replaces atomic operations. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The builder's hash_power(N) now means total capacity is 2^N item slots, matching the original API semantics. The MultiChoiceHashtable divides by 8 internally to get the bucket count. Minimum power is 7 (128 slots). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
thinkingfish
approved these changes
Apr 16, 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
AtomicU64CAS per slot — preparation for dropping&mut selfrequirementKeyVerifiertrait decouples hashtable from segment internalsLocation::GHOSTsentinelThe hashtable module files (
hashtable/location.rs,bucket.rs,table.rs,traits.rs,mod.rs, plussync.rs) are entirely new code. Remaining files (segments/,ttl_buckets/, orchestration) have minimal call-site changes and will be fully replaced in subsequent phases.Test plan
cargo build --workspace --all-featurescargo test --workspace— 88 tests passcargo test -p segcache --features debug— debug feature tests passcargo clippy -p segcache --all-features -- -D warnings— cleancargo fmt --all --check— clean🤖 Generated with Claude Code