fix: replace global math/rand with per-filter RNG for thread safety#30
Merged
Conversation
Replace the global math/rand package with math/rand/v2 and use a per-filter RNG instance to eliminate mutex contention under concurrent access. Changes to filter.go: - Upgrade from math/rand to math/rand/v2 - Add rng field to simdFilter struct - Initialize RNG with PCG algorithm in New() - Update relocate() to use f.rng.IntN() instead of rand.Intn() Changes to filter_test.go: - TestFilterRNGInitialization: Verify each filter has its own RNG - TestFilterConcurrentMultipleFilters: Test concurrent ops on multiple filters - TestFilterConcurrentInsertWithRelocation: Stress test RNG in relocate() - TestFilterRNGIndependence: Verify filters produce independent sequences - TestFilterConcurrentMixedOperations: Test mixed insert/lookup/delete concurrency The previous implementation used the global rand.Intn() function which holds a global mutex, causing contention when multiple goroutines access different filter instances concurrently. Each filter now maintains its own RNG instance, eliminating this bottleneck while maintaining proper randomization for cuckoo hashing relocations. All tests pass including race detector.
There was a problem hiding this comment.
Pull request overview
This PR improves thread safety and concurrent performance by replacing the global math/rand package with math/rand/v2 and introducing per-filter RNG instances. This eliminates mutex contention that occurred when multiple goroutines accessed different filter instances concurrently.
Key Changes:
- Upgraded from
math/randtomath/rand/v2with PCG algorithm for better randomness - Added
rngfield tosimdFilterstruct with per-instance initialization - Added five comprehensive concurrent test cases verifying RNG independence and thread safety
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/filter/filter.go | Added per-filter RNG field, initialized with PCG algorithm in New(), and updated relocate() to use instance RNG instead of global rand |
| internal/filter/filter_test.go | Added comprehensive concurrent tests covering RNG initialization, multi-filter operations, relocation stress testing, RNG independence, and mixed concurrent operations |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Replace the global math/rand package with math/rand/v2 and use a per-filter RNG instance to eliminate mutex contention under concurrent access.
Changes to filter.go:
Changes to filter_test.go:
The previous implementation used the global rand.Intn() function which holds a global mutex, causing contention when multiple goroutines access different filter instances concurrently. Each filter now maintains its own RNG instance, eliminating this bottleneck while maintaining proper randomization for cuckoo hashing relocations.
All tests pass including race detector.