Short-cut common memChunk operations

memChunk is a linked list, speed up some common operations when there's no need to iterate all elements on the list.

Signed-off-by: Łukasz Mierzwa <l.mierzwa@gmail.com>
This commit is contained in:
Łukasz Mierzwa 2023-08-07 16:55:17 +01:00
parent 8d47b3d497
commit 7c9999d010

View file

@ -2103,6 +2103,10 @@ type memChunk struct {
// len returns the length of memChunk list, including the element it was called on.
func (mc *memChunk) len() (count int) {
if mc.prev == nil {
return 1
}
elem := mc
for elem != nil {
count++
@ -2114,6 +2118,9 @@ func (mc *memChunk) len() (count int) {
// oldest returns the oldest element on the list.
// For single element list this will be the same memChunk oldest() was called on.
func (mc *memChunk) oldest() (elem *memChunk) {
if mc.prev == nil {
return mc
}
elem = mc
for elem.prev != nil {
elem = elem.prev
@ -2126,6 +2133,9 @@ func (mc *memChunk) atOffset(offset int) (elem *memChunk) {
if offset == 0 {
return mc
}
if offset == 1 {
return mc.prev
}
if offset < 0 {
return nil
}
@ -2139,7 +2149,6 @@ func (mc *memChunk) atOffset(offset int) (elem *memChunk) {
break
}
}
return elem
}