mirror of
https://github.com/mailgun/groupcache.git
synced 2024-10-02 21:02:08 +00:00
Merge pull request #26 from dgryski/consistenthash-binary-search
consistenthash: replace linear search with binary search
This commit is contained in:
commit
a531d51b7f
@ -69,13 +69,13 @@ func (m *Map) Get(key string) string {
|
|||||||
|
|
||||||
hash := int(m.hash([]byte(key)))
|
hash := int(m.hash([]byte(key)))
|
||||||
|
|
||||||
// Linear search for appropriate replica.
|
// Binary search for appropriate replica.
|
||||||
for _, v := range m.keys {
|
idx := sort.Search(len(m.keys), func(i int) bool { return m.keys[i] >= hash })
|
||||||
if v >= hash {
|
|
||||||
return m.hashMap[v]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Means we have cycled back to the first replica.
|
// Means we have cycled back to the first replica.
|
||||||
return m.hashMap[m.keys[0]]
|
if idx == len(m.keys) {
|
||||||
|
idx = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.hashMap[m.keys[idx]]
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package consistenthash
|
package consistenthash
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -84,3 +85,26 @@ func TestConsistency(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkGet8(b *testing.B) { benchmarkGet(b, 8) }
|
||||||
|
func BenchmarkGet32(b *testing.B) { benchmarkGet(b, 32) }
|
||||||
|
func BenchmarkGet128(b *testing.B) { benchmarkGet(b, 128) }
|
||||||
|
func BenchmarkGet512(b *testing.B) { benchmarkGet(b, 512) }
|
||||||
|
|
||||||
|
func benchmarkGet(b *testing.B, shards int) {
|
||||||
|
|
||||||
|
hash := New(shards, nil)
|
||||||
|
|
||||||
|
var buckets []string
|
||||||
|
for i := 0; i < shards; i++ {
|
||||||
|
buckets = append(buckets, fmt.Sprintf("shard-%d", i))
|
||||||
|
}
|
||||||
|
|
||||||
|
hash.Add(buckets...)
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
hash.Get(buckets[i&(shards-1)])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user