From f3d8232e1424e2e1ff9b2de695a05b58ef3f92b7 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 6 Oct 2021 22:01:17 -0700 Subject: [PATCH] reduce one redis lookup on hot path --- weed/filer/redis3/ItemList.go | 31 +++++++++++++------------------ weed/util/skiplist/skiplist.go | 3 +++ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/weed/filer/redis3/ItemList.go b/weed/filer/redis3/ItemList.go index bad3d2efb..33855d22d 100644 --- a/weed/filer/redis3/ItemList.go +++ b/weed/filer/redis3/ItemList.go @@ -96,22 +96,17 @@ func (nl *ItemList) WriteName(name string) error { return nil } + var prevNodeReference *skiplist.SkipListElementReference if !found { - prevNode, err = nl.skipList.GetLargestNode() - if err != nil { - return err - } + prevNodeReference = nl.skipList.GetLargestNodeReference() } if nextNode != nil && prevNode == nil { - prevNode, err = nl.skipList.LoadElement(nextNode.Prev) - if err != nil { - return err - } + prevNodeReference = nextNode.Prev } - if prevNode != nil { - alreadyContains, nodeSize, err := nl.canAddMember(prevNode.Reference(), name) + if prevNodeReference != nil { + alreadyContains, nodeSize, err := nl.canAddMember(prevNodeReference, name) if err != nil { return err } @@ -122,11 +117,11 @@ func (nl *ItemList) WriteName(name string) error { // case 2.2 if nodeSize < nl.batchSize { - return nl.NodeAddMember(prevNode.Reference(), name) + return nl.NodeAddMember(prevNodeReference, name) } // case 2.3 - x := nl.NodeInnerPosition(prevNode.Reference(), name) + x := nl.NodeInnerPosition(prevNodeReference, name) y := nodeSize - x addToX := x <= y // add to a new node @@ -138,12 +133,12 @@ func (nl *ItemList) WriteName(name string) error { } if addToX { // collect names before name, add them to X - namesToX, err := nl.NodeRangeBeforeExclusive(prevNode.Reference(), name) + namesToX, err := nl.NodeRangeBeforeExclusive(prevNodeReference, name) if err != nil { return nil } // delete skiplist reference to old node - if _, err := nl.skipList.DeleteByKey(prevNode.Key); err != nil { + if _, err := nl.skipList.DeleteByKey(prevNodeReference.Key); err != nil { return err } // add namesToY and name to a new X @@ -152,18 +147,18 @@ func (nl *ItemList) WriteName(name string) error { return nil } // remove names less than name from current Y - if err := nl.NodeDeleteBeforeExclusive(prevNode.Reference(), name); err != nil { + if err := nl.NodeDeleteBeforeExclusive(prevNodeReference, name); err != nil { return nil } // point skip list to current Y - if err := nl.ItemAdd(lookupKey, prevNode.Id); err != nil { + if err := nl.ItemAdd(lookupKey, prevNodeReference.ElementPointer); err != nil { return nil } return nil } else { // collect names after name, add them to Y - namesToY, err := nl.NodeRangeAfterExclusive(prevNode.Reference(), name) + namesToY, err := nl.NodeRangeAfterExclusive(prevNodeReference, name) if err != nil { return nil } @@ -173,7 +168,7 @@ func (nl *ItemList) WriteName(name string) error { return nil } // remove names after name from current X - if err := nl.NodeDeleteAfterExclusive(prevNode.Reference(), name); err != nil { + if err := nl.NodeDeleteAfterExclusive(prevNodeReference, name); err != nil { return nil } return nil diff --git a/weed/util/skiplist/skiplist.go b/weed/util/skiplist/skiplist.go index f42ec23cd..2a0262c69 100644 --- a/weed/util/skiplist/skiplist.go +++ b/weed/util/skiplist/skiplist.go @@ -467,6 +467,9 @@ func (t *SkipList) GetSmallestNode() (*SkipListElement, error) { func (t *SkipList) GetLargestNode() (*SkipListElement, error) { return t.LoadElement(t.EndLevels[0]) } +func (t *SkipList) GetLargestNodeReference() (*SkipListElementReference) { + return t.EndLevels[0] +} // Next returns the next element based on the given node. // Next will loop around to the first node, if you call it on the last!