Skip to content

Commit

Permalink
Increase regen historical state cache size (#5613)
Browse files Browse the repository at this point in the history
* Increase cache size and garbage collect

* Update beacon-chain/db/kv/regen_historical_states.go

* Use const
  • Loading branch information
terencechain authored Apr 25, 2020
1 parent 07f6894 commit 5fbf38c
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion beacon-chain/db/kv/regen_historical_states.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kv
import (
"context"
"fmt"
"runtime"

lru "github.com/hashicorp/golang-lru"
"github.com/pkg/errors"
Expand All @@ -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()
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 5fbf38c

Please sign in to comment.