bugfix for key comparisons > DWSZ

This commit is contained in:
Derek Collison
2012-11-07 13:36:15 -08:00
parent 566251b09e
commit c68ca0ff8a
2 changed files with 30 additions and 3 deletions

View File

@@ -99,8 +99,9 @@ func (h *HashMap) Get(key []byte) interface{} {
// We unroll and optimize the comparison of keys.
for e != nil && len(key) == len(e.key) {
// We unroll and optimize the key comparison here.
klen := len(key)
for i := 0; klen >= _DWSZ; klen -= _DWSZ {
// Compare _DWSZ at a time
i, klen := 0, len(key)
for ; klen >= _DWSZ; klen -= _DWSZ {
k1 := *(*uint64)(unsafe.Pointer(&key[i]))
k2 := *(*uint64)(unsafe.Pointer(&e.key[i]))
if k1 != k2 {
@@ -108,7 +109,17 @@ func (h *HashMap) Get(key []byte) interface{} {
}
i += _DWSZ
}
for i := 0; i < klen; i++ {
// Check by _WSZ if applicable
if (klen & _WSZ) > 0 {
k1 := *(*uint32)(unsafe.Pointer(&key[i]))
k2 := *(*uint32)(unsafe.Pointer(&e.key[i]))
if k1 != k2 {
goto next
}
i += _WSZ
}
// Compare what is left over, byte by byte
for ; i < len(key); i++ {
if key[i] != e.key[i] {
goto next
}

View File

@@ -190,6 +190,22 @@ func TestShrink(t *testing.T) {
}
}
func TestFalseLookup(t *testing.T) {
h := New()
// DW + W
h.Set([]byte("cache.test.0"), "foo")
v := h.Get([]byte("cache.test.1"))
if v != nil {
t.Fatalf("Had a match when did not expect one!\n")
}
// DW + W + 3
h.Set([]byte("cache.test.1234"), "foo")
v = h.Get([]byte("cache.test.0000"))
if v != nil {
t.Fatalf("Had a match when did not expect one!\n")
}
}
func Benchmark_GoMap___GetSmallKey(b *testing.B) {
b.StopTimer()
b.SetBytes(1)