From 5fbf38c98f46957997269b08201082bcba3e10f3 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sat, 25 Apr 2020 07:59:29 -0700 Subject: [PATCH] Increase regen historical state cache size (#5613) * Increase cache size and garbage collect * Update beacon-chain/db/kv/regen_historical_states.go * Use const --- beacon-chain/db/kv/regen_historical_states.go | 12 +++++++++++- 1 file changed, 11 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..d49e6747f3a6 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" @@ -18,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() @@ -56,7 +62,8 @@ func (kv *Store) regenHistoricalStates(ctx context.Context) error { if err != nil { return err } - cacheState, err := lru.New(int(params.BeaconConfig().SlotsPerEpoch) * 2) + + cacheState, err := lru.New(historicalStatesSize) if err != nil { return err } @@ -125,6 +132,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 }