From ad5873cf0b8935f1d7756c5019ae2a0dc111677f Mon Sep 17 00:00:00 2001 From: Joshua Lockerman <> Date: Wed, 8 Jul 2020 20:09:36 -0400 Subject: [PATCH 1/3] Genericize the cache by using interfaces --- cache.go | 81 ++++++++++++++++++++++++------- cache_test.go | 36 +++++++------- list.go | 130 ++++++++++++++------------------------------------ 3 files changed, 118 insertions(+), 129 deletions(-) diff --git a/cache.go b/cache.go index 45fd473..0dddea4 100644 --- a/cache.go +++ b/cache.go @@ -1,6 +1,7 @@ package smolcache import ( + "encoding/binary" "sync" "sync/atomic" @@ -36,13 +37,13 @@ type Interner struct { type block struct { lock sync.RWMutex // guarded by lock - elements map[string]*Element + elements map[interface{}]*Element // only safe to not use a pointer since blocks never move sweep List // CLOCK sweep state, guarded by clockLock - next *Element + prev *Element // pad blocks out to be cache aligned _padding [16]byte } @@ -54,7 +55,19 @@ func WithMax(max uint64) *Interner { } } -func (i *Interner) Insert(key string, value int64) { +func WithMaxAndShards(max uint64, shards int) *Interner { + //TODO variable number of shards + return &Interner{ + max: max, + seed: maphash.MakeSeed(), + } +} + +func (i *Interner) GetSeed() maphash.Seed { + return i.seed +} + +func (i *Interner) InsertString(key string, value interface{}) { newSize := atomic.AddUint64(&i.count, 1) needsEvict := newSize > i.max if needsEvict { @@ -64,16 +77,34 @@ func (i *Interner) Insert(key string, value int64) { h := maphash.Hash{} h.SetSeed(i.seed) h.WriteString(key) - blockNum := h.Sum64() % 127 + i.InsertWithHash(key, value, h.Sum64()) +} + +func (i *Interner) InsertInt(key uint64, value interface{}) { + newSize := atomic.AddUint64(&i.count, 1) + needsEvict := newSize > i.max + if needsEvict { + i.evict() + } + + h := maphash.Hash{} + h.SetSeed(i.seed) + b := [8]byte{} + binary.LittleEndian.PutUint64(b[:], key) + i.InsertWithHash(key, value, h.Sum64()) +} + +func (i *Interner) InsertWithHash(key interface{}, value interface{}, hash uint64) { + blockNum := hash % 127 block := &i.maps[blockNum] block.insert(key, value) } -func (b *block) insert(key string, value int64) bool { +func (b *block) insert(key interface{}, value interface{}) bool { b.lock.Lock() defer b.lock.Unlock() if b.elements == nil { - b.elements = make(map[string]*Element) + b.elements = make(map[interface{}]*Element) } _, present := b.elements[key] if present { @@ -106,23 +137,25 @@ func (i *Interner) evict() { func (b *block) tryEvict() (evicted bool, reachedEnd bool) { b.lock.Lock() defer b.lock.Unlock() - if b.next == nil { - b.next = b.sweep.Front() - if b.next == nil { - return false, true - } + if b.prev == nil { + b.prev = b.sweep.Root() + } + + elem := b.prev.Next() + if elem == nil { + return false, true } evicted = false reachedEnd = false for !evicted && !reachedEnd { - elem := b.next - b.next = elem.Next() - reachedEnd = b.next == nil if elem.used != 0 { elem.used = 0 + b.prev = elem + elem = b.prev.Next() + reachedEnd = elem == nil } else { - key, _ := b.sweep.Remove(elem) + key, _ := b.sweep.RemoveNext(b.prev) delete(b.elements, key) evicted = true } @@ -131,16 +164,28 @@ func (b *block) tryEvict() (evicted bool, reachedEnd bool) { return evicted, reachedEnd } -func (i *Interner) Get(key string) (int64, bool) { +func (i *Interner) GetString(key string) (interface{}, bool) { h := maphash.Hash{} h.SetSeed(i.seed) h.WriteString(key) - blockNum := h.Sum64() % 127 + return i.GetWithHash(key, h.Sum64()) +} + +func (i *Interner) GetInt(key uint64) (interface{}, bool) { + h := maphash.Hash{} + h.SetSeed(i.seed) + b := [8]byte{} + binary.LittleEndian.PutUint64(b[:], key) + return i.GetWithHash(key, h.Sum64()) +} + +func (i *Interner) GetWithHash(key interface{}, hash uint64) (interface{}, bool) { + blockNum := hash % 127 block := &i.maps[blockNum] return block.get(key) } -func (b *block) get(key string) (int64, bool) { +func (b *block) get(key interface{}) (interface{}, bool) { b.lock.RLock() defer b.lock.RUnlock() if b.elements == nil { diff --git a/cache_test.go b/cache_test.go index 56fa0a8..42e579b 100644 --- a/cache_test.go +++ b/cache_test.go @@ -13,8 +13,8 @@ func TestWriteAndGetOnCache(t *testing.T) { cache := WithMax(100) - cache.Insert("1", 1) - val, found := cache.Get("1") + cache.InsertString("1", 1) + val, found := cache.GetString("1") // then if !found { @@ -30,14 +30,14 @@ func TestEntryNotFound(t *testing.T) { cache := WithMax(100) - val, found := cache.Get("nonExistingKey") + val, found := cache.GetString("nonExistingKey") if found { t.Errorf("found %d for noexistent key", val) } - cache.Insert("key", 1) + cache.InsertString("key", 1) - val, found = cache.Get("nonExistingKey") + val, found = cache.GetString("nonExistingKey") if found { t.Errorf("found %d for noexistent key", val) } @@ -49,18 +49,18 @@ func TestEviction(t *testing.T) { cache := WithMax(10) for i := 0; i < 10; i++ { key := fmt.Sprintf("%d", i) - cache.Insert(key, int64(i)) + cache.InsertString(key, int64(i)) if i != 5 { - cache.Get(key) + cache.GetString(key) } } - cache.Insert("100", 100) - cache.Get("100") + cache.InsertString("100", 100) + cache.GetString("100") for i := 0; i < 10; i++ { key := fmt.Sprintf("%d", i) - val, found := cache.Get(key) + val, found := cache.GetString(key) if i != 5 && (!found || val != int64(i)) { t.Errorf("missing value %d, got %d", i, val) } else if i == 5 && found { @@ -71,17 +71,17 @@ func TestEviction(t *testing.T) { } } - val, found := cache.Get("100") + val, found := cache.GetString("100") if !found || val != 100 { t.Errorf("missing value 100, got %d", val) } - cache.Insert("101", 101) - cache.Get("101") + cache.InsertString("101", 101) + cache.GetString("101") for i := 0; i < 10; i++ { key := fmt.Sprintf("%d", i) - val, found := cache.Get(key) + val, found := cache.GetString(key) if i != 5 && i != 2 && (!found || val != int64(i)) { t.Errorf("missing value %d, (found: %v) got %d", i, found, val) } else if (i == 5 || i == 2) && found { @@ -89,11 +89,11 @@ func TestEviction(t *testing.T) { } } - val, found = cache.Get("100") + val, found = cache.GetString("100") if !found || val != 100 { t.Errorf("missing value 100, got %d", val) } - val, found = cache.Get("101") + val, found = cache.GetString("101") if !found || val != 101 { t.Errorf("missing value 101, got %d", val) } @@ -110,7 +110,7 @@ func TestCacheGetRandomly(t *testing.T) { for i := 0; i < ntest; i++ { r := rand.Int63() % 20000 key := fmt.Sprintf("%d", r) - cache.Insert(key, r+1) + cache.InsertString(key, r+1) } wg.Done() }() @@ -118,7 +118,7 @@ func TestCacheGetRandomly(t *testing.T) { for i := 0; i < ntest; i++ { r := rand.Int63() key := fmt.Sprintf("%d", r) - if val, found := cache.Get(key); found && val != r+1 { + if val, found := cache.GetString(key); found && val != r+1 { t.Errorf("got %s ->\n %x\n expected:\n %x\n ", key, val, r+1) } } diff --git a/list.go b/list.go index e06c3a7..e133fb7 100644 --- a/list.go +++ b/list.go @@ -1,146 +1,90 @@ -// based on go std package "container/list", specialized to our -// usecase. original license: -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - package smolcache // Element is an element of a linked list. type Element struct { // The value stored with this element. - key string - Value int64 - - // pad Elements out to be cache aligned - _padding [8]byte + key interface{} + Value interface{} - // Next and previous pointers in the doubly-linked list of elements. + // Next and previous pointers in the singly-linked list of elements. // To simplify the implementation, internally a list l is implemented - // as a ring, such that &l.root is both the next element of the last - // list element (l.Back()) and the previous element of the first list - // element (l.Front()). - next, prev *Element - - // The list to which this element belongs. - list *List - + // as a ring + next *Element //CLOCK marker if this is recently used used uint32 + + // pad Elements out to be cache aligned + _padding [16]byte } // Next returns the next list element or nil. func (e *Element) Next() *Element { - if p := e.next; e.list != nil && p != &e.list.root { - return p - } - return nil -} - -// Prev returns the previous list element or nil. -func (e *Element) Prev() *Element { - if p := e.prev; e.list != nil && p != &e.list.root { - return p - } - return nil + return e.next } -// List represents a doubly linked list. -// The zero value for List is an empty list ready to use. +// List represents a singly linked list. type List struct { - root Element // sentinel list element, only &root, root.prev, and root.next are used - len int // current list length excluding (this) sentinel element + root Element // sentinel list element, only &root and root.next are used + last *Element } // Init initializes or clears list l. func (l *List) Init() *List { - l.root.next = &l.root - l.root.prev = &l.root - l.len = 0 + l.root.next = nil + l.last = &l.root return l } // New returns an initialized list. func New() *List { return new(List).Init() } -// Len returns the number of elements of list l. -// The complexity is O(1). -func (l *List) Len() int { return l.len } - -// Front returns the first element of list l or nil if the list is empty. -func (l *List) Front() *Element { - if l.len == 0 { - return nil - } - return l.root.next -} - -// lazyInit lazily initializes a zero List value. -func (l *List) lazyInit() { - if l.root.next == nil { +// Front returns the root element of list l. +func (l *List) Root() *Element { + if l.last == nil { l.Init() } + return &l.root } // insert inserts e after at, increments l.len, and returns e. func (l *List) insert(e, at *Element) *Element { n := at.next at.next = e - e.prev = at e.next = n - n.prev = e - e.list = l - l.len++ return e } // insertValue is a convenience wrapper for insert(&Element{Value: v}, at). -func (l *List) insertValue(k string, v int64, at *Element) *Element { +func (l *List) insertValue(k interface{}, v interface{}, at *Element) *Element { return l.insert(&Element{key: k, Value: v}, at) } // remove removes e from its list, decrements l.len, and returns e. -func (l *List) remove(e *Element) *Element { - e.prev.next = e.next - e.next.prev = e.prev - e.next = nil // avoid memory leaks - e.prev = nil // avoid memory leaks - e.list = nil - l.len-- - return e -} - -// move moves e to next to at and returns e. -func (l *List) move(e, at *Element) *Element { - if e == at { - return e - } - e.prev.next = e.next - e.next.prev = e.prev - - n := at.next - at.next = e - e.prev = at - e.next = n - n.prev = e - - return e +func (l *List) removeNext(e *Element) *Element { + next := e.next + e.next = e.next.next + next.next = nil // avoid memory leaks + return next } // Remove removes e from l if e is an element of list l. // It returns the element value e.Value. // The element must not be nil. -func (l *List) Remove(e *Element) (string, int64) { - if e.list == l { - // if e.list == l, l must have been initialized when e was inserted - // in l or l == nil (e is a zero Element) and l.remove will crash - l.remove(e) +func (l *List) RemoveNext(e *Element) (key interface{}, val interface{}) { + if e.next == nil { + return } - return e.key, e.Value + next := l.removeNext(e) + if next == l.last { + l.last = e + } + return next.key, next.Value } // PushBack inserts a new element e with value v at the back of list l and returns e. -func (l *List) PushBack(k string, v int64) *Element { - l.lazyInit() - return l.insertValue(k, v, l.root.prev) +func (l *List) PushBack(k interface{}, v interface{}) *Element { + if l.last == nil { + l.Init() + } + return l.insertValue(k, v, l.last) } From bed950439278f784117b1a12f4a8a641f239bf8d Mon Sep 17 00:00:00 2001 From: Joshua Lockerman <> Date: Thu, 9 Jul 2020 13:12:41 -0400 Subject: [PATCH 2/3] Non-sharded version of the cache. This version has a higher write variance, and more contention between the writers and everyone else --- cache.go | 172 ++++++++++++-------------------------------------- cache_test.go | 66 +++++++++++-------- list.go | 11 ++-- 3 files changed, 86 insertions(+), 163 deletions(-) diff --git a/cache.go b/cache.go index 0dddea4..738c02e 100644 --- a/cache.go +++ b/cache.go @@ -1,11 +1,8 @@ package smolcache import ( - "encoding/binary" "sync" "sync/atomic" - - "hash/maphash" ) // CLOCK based approximate LRU storing mappings from strings to @@ -17,116 +14,61 @@ import ( // block. Eviction locks blocks one at a time looking for a value // that's valid to evict/ type Interner struct { - maps [127]block - - max uint64 - seed maphash.Seed - - // padding so that the count, which changes frequently, doesn't - // share a cache line with the max and seed, which are read only - _padding [48]byte - - count uint64 - - clockLock sync.Mutex - - // CLOCK sweep state, guarded by clockLock - clock uint8 -} - -type block struct { - lock sync.RWMutex - // guarded by lock elements map[interface{}]*Element - + max uint64 // only safe to not use a pointer since blocks never move sweep List - // CLOCK sweep state, guarded by clockLock + // CLOCK sweep state, must have write lock prev *Element - // pad blocks out to be cache aligned - _padding [16]byte + + lock sync.RWMutex + count uint64 } func WithMax(max uint64) *Interner { return &Interner{ - max: max, - seed: maphash.MakeSeed(), + max: max, } } func WithMaxAndShards(max uint64, shards int) *Interner { + if max < 1 { + panic("must have max greater than 0") + } //TODO variable number of shards return &Interner{ - max: max, - seed: maphash.MakeSeed(), + max: max, } } -func (i *Interner) GetSeed() maphash.Seed { - return i.seed -} - -func (i *Interner) InsertString(key string, value interface{}) { +func (i *Interner) Insert(key interface{}, value interface{}) (interface{}, bool) { newSize := atomic.AddUint64(&i.count, 1) needsEvict := newSize > i.max + i.lock.Lock() + defer i.lock.Unlock() if needsEvict { i.evict() } - h := maphash.Hash{} - h.SetSeed(i.seed) - h.WriteString(key) - i.InsertWithHash(key, value, h.Sum64()) -} - -func (i *Interner) InsertInt(key uint64, value interface{}) { - newSize := atomic.AddUint64(&i.count, 1) - needsEvict := newSize > i.max - if needsEvict { - i.evict() - } - - h := maphash.Hash{} - h.SetSeed(i.seed) - b := [8]byte{} - binary.LittleEndian.PutUint64(b[:], key) - i.InsertWithHash(key, value, h.Sum64()) -} - -func (i *Interner) InsertWithHash(key interface{}, value interface{}, hash uint64) { - blockNum := hash % 127 - block := &i.maps[blockNum] - block.insert(key, value) -} - -func (b *block) insert(key interface{}, value interface{}) bool { - b.lock.Lock() - defer b.lock.Unlock() - if b.elements == nil { - b.elements = make(map[interface{}]*Element) + if i.elements == nil { + i.elements = make(map[interface{}]*Element) } - _, present := b.elements[key] + val, present := i.elements[key] if present { - return false + return val.Value, false } - elem := b.sweep.PushBack(key, value) - b.elements[key] = elem - return true + elem := i.sweep.PushBack(key, value) + i.elements[key] = elem + return value, true } func (i *Interner) evict() { - i.clockLock.Lock() - defer i.clockLock.Unlock() if i.count == 0 { return } for { - block := &i.maps[i.clock%127] - evicted, reachedEnd := block.tryEvict() - if reachedEnd { - i.clock += 1 - } + evicted := i.tryEvict() if evicted { atomic.AddUint64(&i.count, ^uint64(0)) break @@ -134,64 +76,41 @@ func (i *Interner) evict() { } } -func (b *block) tryEvict() (evicted bool, reachedEnd bool) { - b.lock.Lock() - defer b.lock.Unlock() - if b.prev == nil { - b.prev = b.sweep.Root() +func (i *Interner) tryEvict() (evicted bool) { + if i.prev == nil || i.prev.Next() == nil { + i.prev = i.sweep.Root() } - elem := b.prev.Next() + elem := i.prev.Next() if elem == nil { - return false, true + return false } evicted = false - reachedEnd = false + reachedEnd := false for !evicted && !reachedEnd { if elem.used != 0 { elem.used = 0 - b.prev = elem - elem = b.prev.Next() + i.prev = elem + elem = i.prev.Next() reachedEnd = elem == nil } else { - key, _ := b.sweep.RemoveNext(b.prev) - delete(b.elements, key) + key, _ := i.sweep.RemoveNext(i.prev) + delete(i.elements, key) evicted = true } } - return evicted, reachedEnd -} - -func (i *Interner) GetString(key string) (interface{}, bool) { - h := maphash.Hash{} - h.SetSeed(i.seed) - h.WriteString(key) - return i.GetWithHash(key, h.Sum64()) -} - -func (i *Interner) GetInt(key uint64) (interface{}, bool) { - h := maphash.Hash{} - h.SetSeed(i.seed) - b := [8]byte{} - binary.LittleEndian.PutUint64(b[:], key) - return i.GetWithHash(key, h.Sum64()) + return evicted } -func (i *Interner) GetWithHash(key interface{}, hash uint64) (interface{}, bool) { - blockNum := hash % 127 - block := &i.maps[blockNum] - return block.get(key) -} - -func (b *block) get(key interface{}) (interface{}, bool) { - b.lock.RLock() - defer b.lock.RUnlock() - if b.elements == nil { +func (i *Interner) Get(key interface{}) (interface{}, bool) { + i.lock.RLock() + defer i.lock.RUnlock() + if i.elements == nil { return 0, false } - elem, present := b.elements[key] + elem, present := i.elements[key] if !present { return 0, false } @@ -204,21 +123,12 @@ func (b *block) get(key interface{}) (interface{}, bool) { } func (i *Interner) Unmark(key string) bool { - h := maphash.Hash{} - h.SetSeed(i.seed) - h.WriteString(key) - blockNum := h.Sum64() % 127 - block := &i.maps[blockNum] - return block.unmark(key) -} - -func (b *block) unmark(key string) bool { - b.lock.RLock() - defer b.lock.RUnlock() - if b.elements == nil { + i.lock.RLock() + defer i.lock.RUnlock() + if i.elements == nil { return false } - elem, present := b.elements[key] + elem, present := i.elements[key] if !present { return false } diff --git a/cache_test.go b/cache_test.go index 42e579b..f81cc71 100644 --- a/cache_test.go +++ b/cache_test.go @@ -13,8 +13,8 @@ func TestWriteAndGetOnCache(t *testing.T) { cache := WithMax(100) - cache.InsertString("1", 1) - val, found := cache.GetString("1") + cache.Insert("1", 1) + val, found := cache.Get("1") // then if !found { @@ -30,14 +30,14 @@ func TestEntryNotFound(t *testing.T) { cache := WithMax(100) - val, found := cache.GetString("nonExistingKey") + val, found := cache.Get("nonExistingKey") if found { t.Errorf("found %d for noexistent key", val) } - cache.InsertString("key", 1) + cache.Insert("key", 1) - val, found = cache.GetString("nonExistingKey") + val, found = cache.Get("nonExistingKey") if found { t.Errorf("found %d for noexistent key", val) } @@ -49,18 +49,18 @@ func TestEviction(t *testing.T) { cache := WithMax(10) for i := 0; i < 10; i++ { key := fmt.Sprintf("%d", i) - cache.InsertString(key, int64(i)) + cache.Insert(key, int64(i)) if i != 5 { - cache.GetString(key) + cache.Get(key) } } - cache.InsertString("100", 100) - cache.GetString("100") + cache.Insert("100", 100) + cache.Get("100") for i := 0; i < 10; i++ { key := fmt.Sprintf("%d", i) - val, found := cache.GetString(key) + val, found := cache.Get(key) if i != 5 && (!found || val != int64(i)) { t.Errorf("missing value %d, got %d", i, val) } else if i == 5 && found { @@ -71,17 +71,17 @@ func TestEviction(t *testing.T) { } } - val, found := cache.GetString("100") + val, found := cache.Get("100") if !found || val != 100 { t.Errorf("missing value 100, got %d", val) } - cache.InsertString("101", 101) - cache.GetString("101") + cache.Insert("101", 101) + cache.Get("101") for i := 0; i < 10; i++ { key := fmt.Sprintf("%d", i) - val, found := cache.GetString(key) + val, found := cache.Get(key) if i != 5 && i != 2 && (!found || val != int64(i)) { t.Errorf("missing value %d, (found: %v) got %d", i, found, val) } else if (i == 5 || i == 2) && found { @@ -89,16 +89,24 @@ func TestEviction(t *testing.T) { } } - val, found = cache.GetString("100") + val, found = cache.Get("100") if !found || val != 100 { t.Errorf("missing value 100, got %d", val) } - val, found = cache.GetString("101") + val, found = cache.Get("101") if !found || val != 101 { t.Errorf("missing value 101, got %d", val) } } +func printCache(cache *Interner, t *testing.T) { + str := "[" + for k, v := range cache.elements { + str = fmt.Sprintf("%s\n\t%v: %v, ", str, k, v) + } + t.Logf("%s]", str) +} + func TestCacheGetRandomly(t *testing.T) { t.Parallel() @@ -110,7 +118,7 @@ func TestCacheGetRandomly(t *testing.T) { for i := 0; i < ntest; i++ { r := rand.Int63() % 20000 key := fmt.Sprintf("%d", r) - cache.InsertString(key, r+1) + cache.Insert(key, r+1) } wg.Done() }() @@ -118,7 +126,7 @@ func TestCacheGetRandomly(t *testing.T) { for i := 0; i < ntest; i++ { r := rand.Int63() key := fmt.Sprintf("%d", r) - if val, found := cache.GetString(key); found && val != r+1 { + if val, found := cache.Get(key); found && val != r+1 { t.Errorf("got %s ->\n %x\n expected:\n %x\n ", key, val, r+1) } } @@ -127,12 +135,12 @@ func TestCacheGetRandomly(t *testing.T) { wg.Wait() } -func TestBlockCacheAligned(t *testing.T) { - blockSize := unsafe.Sizeof(block{}) - if blockSize%64 != 0 { - t.Errorf("unaligned block size: %d", blockSize) - } -} +// func TestBlockCacheAligned(t *testing.T) { +// blockSize := unsafe.Sizeof(block{}) +// if blockSize%64 != 0 { +// t.Errorf("unaligned block size: %d", blockSize) +// } +// } func TestElementCacheAligned(t *testing.T) { elementSize := unsafe.Sizeof(Element{}) @@ -142,9 +150,11 @@ func TestElementCacheAligned(t *testing.T) { } func TestCountOffset(t *testing.T) { - seedOffset := unsafe.Offsetof(Interner{}.seed) - countOffset := unsafe.Offsetof(Interner{}.count) - if seedOffset/64 == countOffset/64 { - t.Errorf("seed and count on same cache line\nseed @ %d (%d)\noffset @ %d (%d)", seedOffset, seedOffset/64, countOffset, countOffset/64) + elementsOffset := unsafe.Offsetof(Interner{}.elements) + lockOffset := unsafe.Offsetof(Interner{}.lock) + t.Logf("elem offset %d", elementsOffset) + t.Logf("lock offset %d", lockOffset) + if elementsOffset/64 == lockOffset/64 { + t.Errorf("read-mostly and mutable on the same line\nseed @ %d (%d)\noffset @ %d (%d)", elementsOffset, elementsOffset/64, lockOffset, elementsOffset/64) } } diff --git a/list.go b/list.go index e133fb7..4980a50 100644 --- a/list.go +++ b/list.go @@ -2,6 +2,10 @@ package smolcache // Element is an element of a linked list. type Element struct { + //CLOCK marker if this is recently used, must be first for atomic alignment + used uint32 + //pad key out to 8 bytes + _padding0 [8]byte // The value stored with this element. key interface{} Value interface{} @@ -10,11 +14,9 @@ type Element struct { // To simplify the implementation, internally a list l is implemented // as a ring next *Element - //CLOCK marker if this is recently used - used uint32 // pad Elements out to be cache aligned - _padding [16]byte + _padding1 [1]byte } // Next returns the next list element or nil. @@ -86,5 +88,6 @@ func (l *List) PushBack(k interface{}, v interface{}) *Element { if l.last == nil { l.Init() } - return l.insertValue(k, v, l.last) + l.last = l.insertValue(k, v, l.last) + return l.last } From 6ecd1f279c69bca2103bf80a718749524a20e241 Mon Sep 17 00:00:00 2001 From: Joshua Lockerman <> Date: Thu, 9 Jul 2020 14:30:21 -0400 Subject: [PATCH 3/3] Reuse Elements whenever possible, and allocate outside the loop --- cache.go | 25 +++++++++++++++---------- list.go | 19 +++++++++++-------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/cache.go b/cache.go index 738c02e..6e42e55 100644 --- a/cache.go +++ b/cache.go @@ -45,10 +45,15 @@ func WithMaxAndShards(max uint64, shards int) *Interner { func (i *Interner) Insert(key interface{}, value interface{}) (interface{}, bool) { newSize := atomic.AddUint64(&i.count, 1) needsEvict := newSize > i.max + var elem *Element + if !needsEvict { + elem = makeElement(key, value) + } i.lock.Lock() defer i.lock.Unlock() if needsEvict { - i.evict() + elem = i.evict() + elem.set(key, value) } if i.elements == nil { @@ -58,32 +63,32 @@ func (i *Interner) Insert(key interface{}, value interface{}) (interface{}, bool if present { return val.Value, false } - elem := i.sweep.PushBack(key, value) + i.sweep.PushBack(elem) i.elements[key] = elem return value, true } -func (i *Interner) evict() { +func (i *Interner) evict() *Element { if i.count == 0 { - return + return nil } for { - evicted := i.tryEvict() + elem, evicted := i.tryEvict() if evicted { atomic.AddUint64(&i.count, ^uint64(0)) - break + return elem } } } -func (i *Interner) tryEvict() (evicted bool) { +func (i *Interner) tryEvict() (elem *Element, evicted bool) { if i.prev == nil || i.prev.Next() == nil { i.prev = i.sweep.Root() } - elem := i.prev.Next() + elem = i.prev.Next() if elem == nil { - return false + return nil, false } evicted = false @@ -101,7 +106,7 @@ func (i *Interner) tryEvict() (evicted bool) { } } - return evicted + return } func (i *Interner) Get(key interface{}) (interface{}, bool) { diff --git a/list.go b/list.go index 4980a50..c471b36 100644 --- a/list.go +++ b/list.go @@ -56,11 +56,6 @@ func (l *List) insert(e, at *Element) *Element { return e } -// insertValue is a convenience wrapper for insert(&Element{Value: v}, at). -func (l *List) insertValue(k interface{}, v interface{}, at *Element) *Element { - return l.insert(&Element{key: k, Value: v}, at) -} - // remove removes e from its list, decrements l.len, and returns e. func (l *List) removeNext(e *Element) *Element { next := e.next @@ -84,10 +79,18 @@ func (l *List) RemoveNext(e *Element) (key interface{}, val interface{}) { } // PushBack inserts a new element e with value v at the back of list l and returns e. -func (l *List) PushBack(k interface{}, v interface{}) *Element { +func (l *List) PushBack(e *Element) { if l.last == nil { l.Init() } - l.last = l.insertValue(k, v, l.last) - return l.last + l.last = l.insert(e, l.last) + return +} + +func makeElement(key interface{}, value interface{}) *Element { + return &Element{key: key, Value: value} +} + +func (e *Element) set(key interface{}, value interface{}) { + *e = Element{key: key, Value: value} }