From 990de9556b604677b1282bd9baa8f2c9422bea93 Mon Sep 17 00:00:00 2001 From: Derek Collison Date: Tue, 13 Nov 2012 19:03:33 -0800 Subject: [PATCH] Fixed bug that incorrectly used len comparison on first in chain --- hashmap/hashmap.go | 10 ++++++---- hashmap/hashmap_test.go | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/hashmap/hashmap.go b/hashmap/hashmap.go index b318d06e..8eee0e64 100644 --- a/hashmap/hashmap.go +++ b/hashmap/hashmap.go @@ -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() { diff --git a/hashmap/hashmap_test.go b/hashmap/hashmap_test.go index a053f6d3..943f8d3c 100644 --- a/hashmap/hashmap_test.go +++ b/hashmap/hashmap_test.go @@ -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