Add caches to lotus-stats and splitcode

This commit is contained in:
Travis Person
2021-11-01 09:05:14 +00:00
parent e0a9cae386
commit 2d4f5958e2
25 changed files with 1392 additions and 924 deletions
+56
View File
@@ -0,0 +1,56 @@
package headbuffer
import (
"container/list"
"github.com/filecoin-project/lotus/api"
)
type HeadChangeStackBuffer struct {
buffer *list.List
size int
}
// NewHeadChangeStackBuffer buffer HeadChange events to avoid having to
// deal with revert changes. Initialized size should be the average reorg
// size + 1
func NewHeadChangeStackBuffer(size int) *HeadChangeStackBuffer {
buffer := list.New()
buffer.Init()
return &HeadChangeStackBuffer{
buffer: buffer,
size: size,
}
}
// Push adds a HeadChange to stack buffer. If the length of
// the stack buffer grows larger than the initizlized size, the
// oldest HeadChange is returned.
func (h *HeadChangeStackBuffer) Push(hc *api.HeadChange) (rethc *api.HeadChange) {
if h.buffer.Len() >= h.size {
var ok bool
el := h.buffer.Front()
rethc, ok = el.Value.(*api.HeadChange)
if !ok {
// This shouldn't be possible, this method is typed and is the only place data
// pushed to the buffer.
panic("A cosmic ray made me do it")
}
h.buffer.Remove(el)
}
h.buffer.PushBack(hc)
return
}
// Pop removes the last added HeadChange
func (h *HeadChangeStackBuffer) Pop() {
el := h.buffer.Back()
if el != nil {
h.buffer.Remove(el)
}
}
@@ -0,0 +1,42 @@
package headbuffer
import (
"testing"
"github.com/filecoin-project/lotus/api"
"github.com/stretchr/testify/require"
)
func TestHeadBuffer(t *testing.T) {
t.Run("Straight Push through", func(t *testing.T) {
hb := NewHeadChangeStackBuffer(5)
require.Nil(t, hb.Push(&api.HeadChange{Type: "1"}))
require.Nil(t, hb.Push(&api.HeadChange{Type: "2"}))
require.Nil(t, hb.Push(&api.HeadChange{Type: "3"}))
require.Nil(t, hb.Push(&api.HeadChange{Type: "4"}))
require.Nil(t, hb.Push(&api.HeadChange{Type: "5"}))
hc := hb.Push(&api.HeadChange{Type: "6"})
require.Equal(t, hc.Type, "1")
})
t.Run("Reverts", func(t *testing.T) {
hb := NewHeadChangeStackBuffer(5)
require.Nil(t, hb.Push(&api.HeadChange{Type: "1"}))
require.Nil(t, hb.Push(&api.HeadChange{Type: "2"}))
require.Nil(t, hb.Push(&api.HeadChange{Type: "3"}))
hb.Pop()
require.Nil(t, hb.Push(&api.HeadChange{Type: "3a"}))
hb.Pop()
require.Nil(t, hb.Push(&api.HeadChange{Type: "3b"}))
require.Nil(t, hb.Push(&api.HeadChange{Type: "4"}))
require.Nil(t, hb.Push(&api.HeadChange{Type: "5"}))
hc := hb.Push(&api.HeadChange{Type: "6"})
require.Equal(t, hc.Type, "1")
hc = hb.Push(&api.HeadChange{Type: "7"})
require.Equal(t, hc.Type, "2")
hc = hb.Push(&api.HeadChange{Type: "8"})
require.Equal(t, hc.Type, "3b")
})
}