Skip to content

Commit

Permalink
inline list move
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Jan 2, 2024
1 parent 1456638 commit 341602f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
40 changes: 24 additions & 16 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,42 @@ func (l *list[K, V]) Init(size uint32, value func(index uint32) (K, V)) {
}
}

func (l *list[K, V]) move(i, j uint32) {
if i == j {
func (l *list[K, V]) Back() uint32 {
return l.nodes[0].prev
}

func (l *list[K, V]) MoveToFront(i uint32) {
n, root := &l.nodes[i], &l.nodes[0]

if root.next == i {
return
}

n, at := &l.nodes[i], &l.nodes[j]

l.nodes[n.prev].next = n.next
l.nodes[n.next].prev = n.prev

n.prev = j
n.next = at.next
n.prev = 0
n.next = root.next

l.nodes[j].next = i
l.nodes[0].next = i
l.nodes[n.next].prev = i
}

func (l *list[K, V]) Back() uint32 {
return l.nodes[0].prev
}
func (l *list[K, V]) MoveToBack(i uint32) {
j := l.nodes[0].prev

func (l *list[K, V]) MoveToFront(i uint32) {
if l.nodes[0].next == i {
if i == j {
return
}
l.move(i, 0)
}

func (l *list[K, V]) MoveToBack(i uint32) {
l.move(i, l.nodes[0].prev)
n, at := &l.nodes[i], &l.nodes[j]

l.nodes[n.prev].next = n.next
l.nodes[n.next].prev = n.prev

n.prev = j
n.next = at.next

l.nodes[j].next = i
l.nodes[n.next].prev = i
}
5 changes: 1 addition & 4 deletions shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ func (s *shard[K, V]) Get(hash uint32, key K) (value V, ok bool) {

if index, exists := s.table.Get(hash, key); exists {
if expires := s.list.nodes[index].expires; expires == 0 || atomic.LoadInt64(&clock) < expires {
// s.list.MoveToFront(index)
if s.list.nodes[0].next != index {
s.list.move(index, 0)
}
s.list.MoveToFront(index)
value = s.list.nodes[index].value
ok = true
} else {
Expand Down

0 comments on commit 341602f

Please sign in to comment.