From 41cc93778e084b0e7be2172acdafdb973a185e9f Mon Sep 17 00:00:00 2001 From: phuslu Date: Tue, 2 Jan 2024 21:36:51 +0800 Subject: [PATCH] inline list move --- list.go | 46 +++++++++++++++++++++++++++------------------- shard.go | 5 +---- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/list.go b/list.go index edbc610..95a93fb 100644 --- a/list.go +++ b/list.go @@ -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) { + root := &l.nodes[0] + if root.next == i { return } - n, at := &l.nodes[i], &l.nodes[j] + node := &l.nodes[i] - l.nodes[n.prev].next = n.next - l.nodes[n.next].prev = n.prev + l.nodes[node.prev].next = node.next + l.nodes[node.next].prev = node.prev - n.prev = j - n.next = at.next + node.prev = 0 + node.next = root.next - l.nodes[j].next = i - l.nodes[n.next].prev = i -} - -func (l *list[K, V]) Back() uint32 { - return l.nodes[0].prev + root.next = i + l.nodes[node.next].prev = i } -func (l *list[K, V]) MoveToFront(i uint32) { - if l.nodes[0].next == i { +func (l *list[K, V]) MoveToBack(i uint32) { + j := l.nodes[0].prev + if i == j { return } - l.move(i, 0) -} -func (l *list[K, V]) MoveToBack(i uint32) { - l.move(i, l.nodes[0].prev) + node, at := &l.nodes[i], &l.nodes[j] + + l.nodes[node.prev].next = node.next + l.nodes[node.next].prev = node.prev + + node.prev = j + node.next = at.next + + l.nodes[j].next = i + l.nodes[node.next].prev = i } diff --git a/shard.go b/shard.go index 0619215..1f82fd8 100644 --- a/shard.go +++ b/shard.go @@ -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 {