mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-02 03:38:42 -07:00
Fixed bug that incorrectly used len comparison on first in chain
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user