Skip to content

[BUGFIX] shard distribution bias due to the key length and its stucture#148

Open
kpango wants to merge 1 commit intomainfrom
bugfix/shard-distribution-bias
Open

[BUGFIX] shard distribution bias due to the key length and its stucture#148
kpango wants to merge 1 commit intomainfrom
bugfix/shard-distribution-bias

Conversation

@kpango
Copy link
Owner

@kpango kpango commented Feb 25, 2026

This pull request introduces several improvements and refactors across the codebase, focusing on optimizing iteration patterns, enhancing type usage, and improving hashing strategies in the main cache implementation. The most significant changes include replacing traditional for loops with for range for better readability and performance, updating type declarations to use any, and refining the hashing logic for cache sharding.

Refactoring and Optimization:

Type Improvements:

  • Updated map and function signatures in gache_benchmark_test.go to use any instead of interface{} for better Go 1.18+ compatibility and clarity.
  • Changed callback parameter types in example/main.go from interface{} to any.

Hashing and Sharding Enhancements:

  • Introduced maxKeyLength in the gache struct and changed the shard ID calculation to use maphash for keys up to 32 bytes and xxh3 for longer keys, improving hashing performance and flexibility. [1] [2] [3]
  • Updated cache operations (get, set, Delete) to use the new shard ID calculation with maxKeyLength. [1] [2] [3]

Concurrency Improvements:

  • Refactored goroutine management in ToMap and benchmarks to use wg.Go instead of manual wg.Add and wg.Done, simplifying concurrency handling. [1] [2] [3] [4] [5]

Standard Library Usage:

  • Used the new maps.Copy function in map_reference_test.go to simplify copying maps. [1] [2]

Copilot AI review requested due to automatic review settings February 25, 2026 03:17
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request attempts to fix shard distribution bias in the cache by introducing configurable key length hashing and optimizing the hash function selection. The PR also includes widespread refactoring to modernize Go syntax patterns (using for range N and any instead of interface{}), simplify concurrency patterns, and utilize standard library improvements like maps.Copy.

Changes:

  • Introduced maxKeyLength option to control how many bytes of keys are used for shard distribution hashing, with dynamic selection between maphash (for ≤32 bytes) and xxh3 (for longer keys)
  • Refactored iteration patterns from for i := 0; i < N; i++ to for range N throughout the codebase
  • Attempted to simplify concurrent operations by replacing wg.Add(1) and wg.Done() with non-existent wg.Go() method

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
gache.go Core changes to shard ID calculation with new maxKeyLength field and modified getShardID function; attempted refactoring of ToMap concurrency pattern
option.go Added WithMaxKeyLength option for configuring hash key length
gache_benchmark_test.go Modernized type signatures (interface{} to any) and attempted concurrency refactoring
map_bench_test.go Refactored loops to use for range N pattern
map_size_noswiss_test.go Refactored loops to use for range N pattern
map_size_noswiss.go Refactored loops to use for range len(slice) pattern
map_reference_test.go Simplified map copying using maps.Copy from standard library
example/main.go Updated type signatures and loop patterns for consistency

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@kpango kpango force-pushed the bugfix/shard-distribution-bias branch from b81381a to 1875383 Compare February 25, 2026 03:23
Copy link

Copilot AI commented Feb 25, 2026

@kpango I've opened a new pull request, #149, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 7 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@kpango
Copy link
Owner Author

kpango commented Feb 25, 2026

@copilot open a new pull request to apply changes based on the comments in this thread

Copy link

Copilot AI commented Feb 25, 2026

@kpango I've opened a new pull request, #150, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 14 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@kpango kpango force-pushed the bugfix/shard-distribution-bias branch from 9692fb0 to e6ed2a3 Compare February 25, 2026 04:08
@kpango kpango force-pushed the bugfix/shard-distribution-bias branch from e6ed2a3 to 25c9cc2 Compare February 25, 2026 04:08
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.

3 participants