Fixed bug that incorrectly used len comparison on first in chain

This commit is contained in:
Derek Collison
2012-11-13 19:03:33 -08:00
parent fd5d1973c8
commit 990de9556b
2 changed files with 21 additions and 4 deletions

View File

@@ -104,13 +104,15 @@ func (h *HashMap) Set(key []byte, data interface{}) {
func (h *HashMap) Get(key []byte) interface{} {
hk := h.Hash(key)
e := h.bkts[hk&h.msk]
// FIXME: Reorder on GET if chained?
// We unroll and optimize the comparison of keys.
for e != nil && len(key) == len(e.key) {
for e != nil {
i, klen := 0, len(key)
if klen != len(e.key) {
goto next
}
// We unroll and optimize the key comparison here.
// 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]))
@@ -182,7 +184,7 @@ func (h *HashMap) resize(nsz uint32) {
h.msk = nmsk
}
const maxBktSize = (1<<31)-1
const maxBktSize = (1 << 31) - 1
// grow the HashMap's buckets by 2
func (h *HashMap) grow() {

View File

@@ -147,6 +147,21 @@ func TestSetDoesReplaceOnExisting(t *testing.T) {
}
}
func TestCollision(t *testing.T) {
h := New()
k1 := []byte("999")
k2 := []byte("1000")
h.Set(k1, "foo")
h.Set(k2, "bar")
all := h.All()
if len(all) != 2 {
t.Fatalf("Expected 2 vs %d\n", len(all))
}
if h.Get(k1) == nil {
t.Fatalf("Failed to get '999'\n")
}
}
func TestHashMapStats(t *testing.T) {
h := New()
h.rsz = false