Skip to content

Commit

Permalink
Merge pull request #27 from vimeo/swap_lru_cache_ll_fields
Browse files Browse the repository at this point in the history
lru: swap linked-list and map fields
  • Loading branch information
dfinkel authored Jul 29, 2022
2 parents 83c6207 + 2574e46 commit 9d0cb74
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ type Cache struct {
// executed when an entry is purged from the cache.
OnEvicted func(key Key, value interface{})

ll *list.List
// cache comes first so the GC enqueues marking the map-contents first
// (which will mark the contents of the linked-list much more
// efficiently than traversing the linked-list directly)
cache map[interface{}]*list.Element
ll *list.List
}

// A Key may be any value that is comparable. See http://golang.org/ref/spec#Comparison_operators
Expand Down
5 changes: 4 additions & 1 deletion lru/typed_lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ type TypedCache[K comparable, V any] struct {
// executed when an typedEntry is purged from the cache.
OnEvicted func(key K, value V)

ll linkedList[typedEntry[K, V]]
// cache comes first so the GC enqueues marking the map-contents first
// (which will mark the contents of the linked-list much more
// efficiently than traversing the linked-list directly)
cache map[K]*llElem[typedEntry[K, V]]
ll linkedList[typedEntry[K, V]]
}

type typedEntry[K comparable, V any] struct {
Expand Down

0 comments on commit 9d0cb74

Please sign in to comment.