Skip to content

Commit

Permalink
inlining list move for get
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Jan 1, 2024
1 parent 9543f90 commit f84ca04
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,23 @@ func (s *shard[K, V]) Get(hash uint32, key K) (value V, ok bool) {
s.mu.Lock()

if index, exists := s.table.Get(hash, key); exists {
if timestamp := s.list.nodes[index].expires; timestamp == 0 || atomic.LoadInt64(&now) < timestamp {
// s.list.MoveToFront(index)
if s.list.nodes[0].next != index {
s.list.move(index, 0)
node := &s.list.nodes[index]
if timestamp := node.expires; timestamp == 0 || atomic.LoadInt64(&now) < timestamp {
// inlining for "s.list.MoveToFront(index)"
root := &s.list.nodes[0]
if root.next != index {
s.list.nodes[node.prev].next = node.next
s.list.nodes[node.next].prev = node.prev
node.prev = 0
node.next = root.next
root.next = index
s.list.nodes[node.next].prev = index
}
value = s.list.nodes[index].value
value = node.value
ok = true
} else {
s.list.MoveToBack(index)
s.list.nodes[index].value = value
node.value = value
s.table.Delete(hash, key)
}
}
Expand Down

0 comments on commit f84ca04

Please sign in to comment.