From d7aa0fd2a9ac1bc72ef6d8a1e99db329ece608ab Mon Sep 17 00:00:00 2001 From: terence tsao Date: Fri, 24 Apr 2020 14:00:53 -0700 Subject: [PATCH 1/3] Increase cache size and garbage collect --- beacon-chain/db/kv/regen_historical_states.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/beacon-chain/db/kv/regen_historical_states.go b/beacon-chain/db/kv/regen_historical_states.go index 176bfb048270..2b3202bbaa4f 100644 --- a/beacon-chain/db/kv/regen_historical_states.go +++ b/beacon-chain/db/kv/regen_historical_states.go @@ -3,6 +3,7 @@ package kv import ( "context" "fmt" + "runtime" lru "github.com/hashicorp/golang-lru" "github.com/pkg/errors" @@ -56,7 +57,10 @@ func (kv *Store) regenHistoricalStates(ctx context.Context) error { if err != nil { return err } - cacheState, err := lru.New(int(params.BeaconConfig().SlotsPerEpoch) * 2) + // Using max possible size to avoid using DB to save and retrieve pre state (slow) + // The size is 80 because block at slot 43772 built on top of block at slot 43693. + // That is the worst case. + cacheState, err := lru.New(80) if err != nil { return err } @@ -125,6 +129,9 @@ func (kv *Store) regenHistoricalStates(ctx context.Context) error { // Flush the cache, the cached states never be used again. cacheState.Purge() + // Manually garbage collect as previous cache will never be used again.. + runtime.GC() + return nil } From ac58d804d8afd26ceda40f5b31aa2c504e00e30d Mon Sep 17 00:00:00 2001 From: terence tsao Date: Fri, 24 Apr 2020 14:04:44 -0700 Subject: [PATCH 2/3] Update beacon-chain/db/kv/regen_historical_states.go --- beacon-chain/db/kv/regen_historical_states.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beacon-chain/db/kv/regen_historical_states.go b/beacon-chain/db/kv/regen_historical_states.go index 2b3202bbaa4f..7919ed63d0dd 100644 --- a/beacon-chain/db/kv/regen_historical_states.go +++ b/beacon-chain/db/kv/regen_historical_states.go @@ -129,7 +129,7 @@ func (kv *Store) regenHistoricalStates(ctx context.Context) error { // Flush the cache, the cached states never be used again. cacheState.Purge() - // Manually garbage collect as previous cache will never be used again.. + // Manually garbage collect as previous cache will never be used again. runtime.GC() return nil From f7fa56b3d6c7bdc2402dc53ba64c2a732f41ecbf Mon Sep 17 00:00:00 2001 From: terence tsao Date: Fri, 24 Apr 2020 14:34:52 -0700 Subject: [PATCH 3/3] Use const --- beacon-chain/db/kv/regen_historical_states.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/beacon-chain/db/kv/regen_historical_states.go b/beacon-chain/db/kv/regen_historical_states.go index 2b3202bbaa4f..d8f10bb969a5 100644 --- a/beacon-chain/db/kv/regen_historical_states.go +++ b/beacon-chain/db/kv/regen_historical_states.go @@ -19,6 +19,11 @@ import ( "go.opencensus.io/trace" ) +// Using max possible size to avoid using DB to save and retrieve pre state (slow) +// The size is 80 because block at slot 43772 built on top of block at slot 43693. +// That is the worst case. +const historicalStatesSize = 80 + func (kv *Store) regenHistoricalStates(ctx context.Context) error { ctx, span := trace.StartSpan(ctx, "db.regenHistoricalStates") defer span.End() @@ -57,10 +62,8 @@ func (kv *Store) regenHistoricalStates(ctx context.Context) error { if err != nil { return err } - // Using max possible size to avoid using DB to save and retrieve pre state (slow) - // The size is 80 because block at slot 43772 built on top of block at slot 43693. - // That is the worst case. - cacheState, err := lru.New(80) + + cacheState, err := lru.New(historicalStatesSize) if err != nil { return err }