reduce one redis lookup on hot path

This commit is contained in:
Chris Lu 2021-10-06 22:01:17 -07:00
parent 03bb82043c
commit f3d8232e14
2 changed files with 16 additions and 18 deletions

View file

@ -96,22 +96,17 @@ func (nl *ItemList) WriteName(name string) error {
return nil return nil
} }
var prevNodeReference *skiplist.SkipListElementReference
if !found { if !found {
prevNode, err = nl.skipList.GetLargestNode() prevNodeReference = nl.skipList.GetLargestNodeReference()
if err != nil {
return err
}
} }
if nextNode != nil && prevNode == nil { if nextNode != nil && prevNode == nil {
prevNode, err = nl.skipList.LoadElement(nextNode.Prev) prevNodeReference = nextNode.Prev
if err != nil {
return err
}
} }
if prevNode != nil { if prevNodeReference != nil {
alreadyContains, nodeSize, err := nl.canAddMember(prevNode.Reference(), name) alreadyContains, nodeSize, err := nl.canAddMember(prevNodeReference, name)
if err != nil { if err != nil {
return err return err
} }
@ -122,11 +117,11 @@ func (nl *ItemList) WriteName(name string) error {
// case 2.2 // case 2.2
if nodeSize < nl.batchSize { if nodeSize < nl.batchSize {
return nl.NodeAddMember(prevNode.Reference(), name) return nl.NodeAddMember(prevNodeReference, name)
} }
// case 2.3 // case 2.3
x := nl.NodeInnerPosition(prevNode.Reference(), name) x := nl.NodeInnerPosition(prevNodeReference, name)
y := nodeSize - x y := nodeSize - x
addToX := x <= y addToX := x <= y
// add to a new node // add to a new node
@ -138,12 +133,12 @@ func (nl *ItemList) WriteName(name string) error {
} }
if addToX { if addToX {
// collect names before name, add them to X // collect names before name, add them to X
namesToX, err := nl.NodeRangeBeforeExclusive(prevNode.Reference(), name) namesToX, err := nl.NodeRangeBeforeExclusive(prevNodeReference, name)
if err != nil { if err != nil {
return nil return nil
} }
// delete skiplist reference to old node // 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 return err
} }
// add namesToY and name to a new X // add namesToY and name to a new X
@ -152,18 +147,18 @@ func (nl *ItemList) WriteName(name string) error {
return nil return nil
} }
// remove names less than name from current Y // 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 return nil
} }
// point skip list to current Y // 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
} }
return nil return nil
} else { } else {
// collect names after name, add them to Y // collect names after name, add them to Y
namesToY, err := nl.NodeRangeAfterExclusive(prevNode.Reference(), name) namesToY, err := nl.NodeRangeAfterExclusive(prevNodeReference, name)
if err != nil { if err != nil {
return nil return nil
} }
@ -173,7 +168,7 @@ func (nl *ItemList) WriteName(name string) error {
return nil return nil
} }
// remove names after name from current X // 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
} }
return nil return nil

View file

@ -467,6 +467,9 @@ func (t *SkipList) GetSmallestNode() (*SkipListElement, error) {
func (t *SkipList) GetLargestNode() (*SkipListElement, error) { func (t *SkipList) GetLargestNode() (*SkipListElement, error) {
return t.LoadElement(t.EndLevels[0]) 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 returns the next element based on the given node.
// Next will loop around to the first node, if you call it on the last! // Next will loop around to the first node, if you call it on the last!