Skip to content

Conversation

@abhishek-singla-97
Copy link

Summary

This PR optimizes the consistent hash picker by eliminating the hashMap map lookup in the Get() method as suggested in #13

Changes

  • Added entry struct that stores both the hash value and client reference together
  • Replaced keys []int + hashMap map[int]Client with keys []entry
  • Updated Add() to use sort.Slice instead of sort.Ints
  • Updated Get() to access client directly from entry.client

Benchmark Results (Apple M1 Pro)

Benchmark Before (ns/op) After (ns/op) Improvement
BenchmarkGet8 25.81 23.10 10.5%
BenchmarkGet32 31.43 24.14 23.2%
BenchmarkGet128 29.67 27.33 7.9%
BenchmarkGet512 37.17 33.81 9.0%

Replace hashMap with entry struct that stores hash and client together,
eliminating the map lookup after binary search in Get().

This optimization was ported from groupcache2 where it showed ~15-25%
improvement in Get() operations.
@abhishek-singla-97 abhishek-singla-97 force-pushed the optimize-consistent-hash-picker branch from fbec36a to 8af89bb Compare December 21, 2025 11:19
@abhishek-singla-97
Copy link
Author

@thrawn01 can you take a look at this? Pretty small change as in the issue discussion, didn't do anything new. Hope it was still required as I saw it was tagged "help wanted".

Copy link
Contributor

@thrawn01 thrawn01 left a comment

Choose a reason for hiding this comment

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

Thank you for making this PR! ❤️

@thrawn01 thrawn01 merged commit 48ae696 into groupcache:main Dec 25, 2025
0 of 4 checks passed
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.

2 participants