diff --git a/cache.go b/cache.go index 45fd473..483c255 100644 --- a/cache.go +++ b/cache.go @@ -1,10 +1,9 @@ package smolcache import ( + "fmt" "sync" "sync/atomic" - - "hash/maphash" ) // CLOCK based approximate LRU storing mappings from strings to @@ -16,137 +15,162 @@ 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 + lock sync.RWMutex + // stores indexes into storage + elements map[interface{}]*Element + storage []Element - // CLOCK sweep state, guarded by clockLock - clock uint8 + // CLOCK sweep state, must have write lock + next int } -type block struct { - lock sync.RWMutex - // guarded by lock - elements map[string]*Element +type Element struct { + // The value stored with this element. + key interface{} + Value interface{} - // only safe to not use a pointer since blocks never move - sweep List + //CLOCK marker if this is recently used + used uint32 - // CLOCK sweep state, guarded by clockLock - next *Element - // pad blocks out to be cache aligned - _padding [16]byte + // pad Elements out to be cache aligned + _padding [24]byte } func WithMax(max uint64) *Interner { + if max < 1 { + panic("must have max greater than 0") + } return &Interner{ - max: max, - seed: maphash.MakeSeed(), + elements: make(map[interface{}]*Element, max), + storage: make([]Element, 0, max), } } -func (i *Interner) Insert(key string, value int64) { - newSize := atomic.AddUint64(&i.count, 1) - needsEvict := newSize > i.max - if needsEvict { - i.evict() - } +func WithMaxAndShards(max uint64, shards int) *Interner { + return WithMax(max) +} + +// Insert a key/value mapping into the cache if the key is not already present +// returns the value present in the map, and true if it is newley inserted +func (i *Interner) Insert(key interface{}, value interface{}) (canonicalValue interface{}, inserted bool) { + i.lock.Lock() + defer i.lock.Unlock() - h := maphash.Hash{} - h.SetSeed(i.seed) - h.WriteString(key) - blockNum := h.Sum64() % 127 - block := &i.maps[blockNum] - block.insert(key, value) + _, canonicalValue, inserted = i.insert(key, value) + return } -func (b *block) insert(key string, value int64) bool { - b.lock.Lock() - defer b.lock.Unlock() - if b.elements == nil { - b.elements = make(map[string]*Element) +// Insert a bactch of keys with their corresponding values. +// This function will _overwrite_ the keys and values slices with their +// canonical versions. +func (i *Interner) InsertBatch(keys []interface{}, values []interface{}) { + if len(keys) != len(values) { + panic(fmt.Sprintf("keys and values are not the same len. %d keys, %d values", len(keys), len(values))) } - _, present := b.elements[key] - if present { - return false + values = values[:len(keys)] + i.lock.Lock() + defer i.lock.Unlock() + + for idx := range keys { + keys[idx], values[idx], _ = i.insert(keys[idx], values[idx]) } - elem := b.sweep.PushBack(key, value) - b.elements[key] = elem - return true + return } -func (i *Interner) evict() { - i.clockLock.Lock() - defer i.clockLock.Unlock() - if i.count == 0 { - return +func (i *Interner) insert(key interface{}, value interface{}) (canonicalKey interface{}, canonicalValue interface{}, inserted bool) { + elem, present := i.elements[key] + if present { + return elem.key, elem.Value, false + } + + var insertLocation *Element + if len(i.storage) >= cap(i.storage) { + insertLocation = i.evict() + *insertLocation = Element{key: key, Value: value} + } else { + i.storage = append(i.storage, Element{key: key, Value: value}) + insertLocation = &i.storage[len(i.storage)-1] } + + i.elements[key] = insertLocation + return key, value, true +} + +func (i *Interner) evict() (insertPtr *Element) { for { - block := &i.maps[i.clock%127] - evicted, reachedEnd := block.tryEvict() - if reachedEnd { - i.clock += 1 - } + insertLocation, evicted := i.tryEvict() if evicted { - atomic.AddUint64(&i.count, ^uint64(0)) - break + return insertLocation } } } -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 - } +func (i *Interner) tryEvict() (insertPtr *Element, evicted bool) { + if i.next >= len(i.storage) { + i.next = 0 } evicted = false - reachedEnd = false + reachedEnd := false for !evicted && !reachedEnd { - elem := b.next - b.next = elem.Next() - reachedEnd = b.next == nil + elem := &i.storage[i.next] if elem.used != 0 { elem.used = 0 } else { - key, _ := b.sweep.Remove(elem) - delete(b.elements, key) + insertPtr = elem + key := elem.key + delete(i.elements, key) evicted = true } + i.next += 1 + reachedEnd = i.next >= len(i.storage) } - return evicted, reachedEnd + return } -func (i *Interner) Get(key string) (int64, bool) { - h := maphash.Hash{} - h.SetSeed(i.seed) - h.WriteString(key) - blockNum := h.Sum64() % 127 - block := &i.maps[blockNum] - return block.get(key) +// tries to get a batch of keys and store the corresponding values is valuesOut +// returns the number of keys that were actually found. +// NOTE: this function does _not_ preserve the order of keys; the first numFound +// keys will be the keys whose values are present, while the remainder +// will be the keys not present in the cache +func (i *Interner) GetValues(keys []interface{}, valuesOut []interface{}) (numFound int) { + if len(keys) != len(valuesOut) { + panic(fmt.Sprintf("keys and values are not the same len. %d keys, %d values", len(keys), len(valuesOut))) + } + valuesOut = valuesOut[:len(keys)] + n := len(keys) + idx := 0 + + i.lock.RLock() + defer i.lock.RUnlock() + + for idx < n { + value, found := i.get(keys[idx]) + if !found { + if n == 0 { + return 0 + } + // no value found for key, swap the key with the last element, and shrink n + n -= 1 + keys[n], keys[idx] = keys[idx], keys[n] + continue + } + valuesOut[idx] = value + idx += 1 + } + return n } -func (b *block) get(key string) (int64, bool) { - b.lock.RLock() - defer b.lock.RUnlock() - if b.elements == nil { - return 0, false - } - elem, present := b.elements[key] +func (i *Interner) Get(key interface{}) (interface{}, bool) { + i.lock.RLock() + defer i.lock.RUnlock() + return i.get(key) +} + +func (i *Interner) get(key interface{}) (interface{}, bool) { + + elem, present := i.elements[key] if !present { return 0, false } @@ -159,21 +183,10 @@ func (b *block) get(key string) (int64, 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) -} + i.lock.RLock() + defer i.lock.RUnlock() -func (b *block) unmark(key string) bool { - b.lock.RLock() - defer b.lock.RUnlock() - if b.elements == nil { - return false - } - elem, present := b.elements[key] + elem, present := i.elements[key] if !present { return false } @@ -185,6 +198,8 @@ func (b *block) unmark(key string) bool { return true } -func (i *Interner) Len() uint64 { - return atomic.LoadUint64(&i.count) +func (i *Interner) Len() int { + i.lock.RLock() + defer i.lock.RUnlock() + return len(i.storage) } diff --git a/cache_test.go b/cache_test.go index 56fa0a8..3d63043 100644 --- a/cache_test.go +++ b/cache_test.go @@ -3,6 +3,7 @@ package smolcache import ( "fmt" "math/rand" + "reflect" "sync" "testing" "unsafe" @@ -99,6 +100,14 @@ func TestEviction(t *testing.T) { } } +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() @@ -127,10 +136,36 @@ 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 TestBatch(t *testing.T) { + t.Parallel() + + cache := WithMax(10) + + cache.InsertBatch([]interface{}{3, 6, 9, 12}, []interface{}{4, 7, 10, 13}) + + keys := []interface{}{1, 2, 3, 6, 9, 12, 13} + vals := make([]interface{}, len(keys)) + numFound := cache.GetValues(keys, vals) + + if numFound != 4 { + t.Errorf("found incorrect number of values: expected 4, found %d\n\tkeys: %v\n\t%v", numFound, keys, vals) + } + + expectedKeys := []interface{}{12, 9, 3, 6, 2, 13, 1} + if !reflect.DeepEqual(keys, expectedKeys) { + t.Errorf("unexpected keys:\nexpected\n\t%v\nfound\n\t%v", keys, expectedKeys) + } + + expectedVals := []interface{}{13, 10, 4, 7, nil, nil, nil} + if !reflect.DeepEqual(vals, expectedVals) { + t.Errorf("unexpected values:\nexpected\n\t%v\nfound\n\t%v", expectedVals, vals) } } @@ -139,12 +174,21 @@ func TestElementCacheAligned(t *testing.T) { if elementSize%64 != 0 { t.Errorf("unaligned element size: %d", elementSize) } -} - -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) + if elementSize != 64 { + t.Errorf("unexpected element size: %d", elementSize) } } + +// The entire cache fits on one cache line, but since +// we have a contended write to the lock anyway, it +// doesn't seem that bad that we fetch everything else +// as well +// func TestCountOffset(t *testing.T) { +// 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 deleted file mode 100644 index e06c3a7..0000000 --- a/list.go +++ /dev/null @@ -1,146 +0,0 @@ -// 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 - - // Next and previous pointers in the doubly-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 - - //CLOCK marker if this is recently used - used uint32 -} - -// 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 -} - -// List represents a doubly linked list. -// The zero value for List is an empty list ready to use. -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 -} - -// Init initializes or clears list l. -func (l *List) Init() *List { - l.root.next = &l.root - l.root.prev = &l.root - l.len = 0 - 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 { - l.Init() - } -} - -// 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 { - 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 -} - -// 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) - } - return e.key, e.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) -}