diff --git a/cache.go b/cache.go index 45fd473..6e42e55 100644 --- a/cache.go +++ b/cache.go @@ -3,8 +3,6 @@ package smolcache import ( "sync" "sync/atomic" - - "hash/maphash" ) // CLOCK based approximate LRU storing mappings from strings to @@ -16,137 +14,108 @@ 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 + elements map[interface{}]*Element + max uint64 + // only safe to not use a pointer since blocks never move + sweep List - // 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 + // CLOCK sweep state, must have write lock + prev *Element + lock sync.RWMutex count uint64 - - clockLock sync.Mutex - - // CLOCK sweep state, guarded by clockLock - clock uint8 } -type block struct { - lock sync.RWMutex - // guarded by lock - elements map[string]*Element - - // only safe to not use a pointer since blocks never move - sweep List - - // CLOCK sweep state, guarded by clockLock - next *Element - // pad blocks out to be cache aligned - _padding [16]byte +func WithMax(max uint64) *Interner { + return &Interner{ + max: max, + } } -func WithMax(max uint64) *Interner { +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) Insert(key string, value int64) { +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) } - h := maphash.Hash{} - h.SetSeed(i.seed) - h.WriteString(key) - blockNum := h.Sum64() % 127 - block := &i.maps[blockNum] - block.insert(key, value) -} - -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) + 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 + i.sweep.PushBack(elem) + i.elements[key] = elem + return value, true } -func (i *Interner) evict() { - i.clockLock.Lock() - defer i.clockLock.Unlock() +func (i *Interner) evict() *Element { if i.count == 0 { - return + return nil } for { - block := &i.maps[i.clock%127] - evicted, reachedEnd := block.tryEvict() - if reachedEnd { - i.clock += 1 - } + elem, evicted := i.tryEvict() if evicted { atomic.AddUint64(&i.count, ^uint64(0)) - break + return elem } } } -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() (elem *Element, evicted bool) { + if i.prev == nil || i.prev.Next() == nil { + i.prev = i.sweep.Root() + } + + elem = i.prev.Next() + if elem == nil { + return nil, false } evicted = false - reachedEnd = false + reachedEnd := false for !evicted && !reachedEnd { - elem := b.next - b.next = elem.Next() - reachedEnd = b.next == nil if elem.used != 0 { elem.used = 0 + i.prev = elem + elem = i.prev.Next() + reachedEnd = elem == nil } else { - key, _ := b.sweep.Remove(elem) - delete(b.elements, key) + key, _ := i.sweep.RemoveNext(i.prev) + delete(i.elements, key) evicted = true } } - 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) -} - -func (b *block) get(key string) (int64, 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 } @@ -159,21 +128,12 @@ 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) -} - -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 56fa0a8..f81cc71 100644 --- a/cache_test.go +++ b/cache_test.go @@ -99,6 +99,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,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 e06c3a7..c471b36 100644 --- a/list.go +++ b/list.go @@ -1,146 +1,96 @@ -// 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 { + //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 string - Value int64 + key interface{} + Value interface{} - // pad Elements out to be cache aligned - _padding [8]byte - - // 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 + _padding1 [1]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 + return e.next } -// 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. +// 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 { - 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 + } + next := l.removeNext(e) + if next == l.last { + l.last = e } - return e.key, e.Value + 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(e *Element) { + if l.last == nil { + l.Init() + } + 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} }