Minor State Management Improvements (#8742)

* add improvement

* change to bool

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
Nishant Das 2021-04-13 21:23:06 +08:00 committed by GitHub
parent 45d2df1af7
commit 131a14ee2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 6 deletions

View File

@ -56,6 +56,8 @@ go_library(
"@com_github_dgraph_io_ristretto//:go_default_library", "@com_github_dgraph_io_ristretto//:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library", "@com_github_gogo_protobuf//proto:go_default_library",
"@com_github_pkg_errors//:go_default_library", "@com_github_pkg_errors//:go_default_library",
"@com_github_prometheus_client_golang//prometheus:go_default_library",
"@com_github_prometheus_client_golang//prometheus/promauto:go_default_library",
"@com_github_prysmaticlabs_eth2_types//:go_default_library", "@com_github_prysmaticlabs_eth2_types//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library", "@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",

View File

@ -8,6 +8,8 @@ import (
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
v1 "github.com/prysmaticlabs/ethereumapis/eth/v1" v1 "github.com/prysmaticlabs/ethereumapis/eth/v1"
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil" "github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
@ -20,6 +22,13 @@ import (
"go.opencensus.io/trace" "go.opencensus.io/trace"
) )
var (
stateCount = promauto.NewGauge(prometheus.GaugeOpts{
Name: "beacon_state_count",
Help: "Count the number of active beacon state objects.",
})
)
// InitializeFromProto the beacon state from a protobuf representation. // InitializeFromProto the beacon state from a protobuf representation.
func InitializeFromProto(st *pbp2p.BeaconState) (*BeaconState, error) { func InitializeFromProto(st *pbp2p.BeaconState) (*BeaconState, error) {
return InitializeFromProtoUnsafe(proto.Clone(st).(*pbp2p.BeaconState)) return InitializeFromProtoUnsafe(proto.Clone(st).(*pbp2p.BeaconState))
@ -35,7 +44,7 @@ func InitializeFromProtoUnsafe(st *pbp2p.BeaconState) (*BeaconState, error) {
fieldCount := params.BeaconConfig().BeaconStateFieldCount fieldCount := params.BeaconConfig().BeaconStateFieldCount
b := &BeaconState{ b := &BeaconState{
state: st, state: st,
dirtyFields: make(map[fieldIndex]interface{}, fieldCount), dirtyFields: make(map[fieldIndex]bool, fieldCount),
dirtyIndices: make(map[fieldIndex][]uint64, fieldCount), dirtyIndices: make(map[fieldIndex][]uint64, fieldCount),
stateFieldLeaves: make(map[fieldIndex]*FieldTrie, fieldCount), stateFieldLeaves: make(map[fieldIndex]*FieldTrie, fieldCount),
sharedFieldReferences: make(map[fieldIndex]*stateutil.Reference, 10), sharedFieldReferences: make(map[fieldIndex]*stateutil.Reference, 10),
@ -66,6 +75,7 @@ func InitializeFromProtoUnsafe(st *pbp2p.BeaconState) (*BeaconState, error) {
b.sharedFieldReferences[balances] = stateutil.NewRef(1) b.sharedFieldReferences[balances] = stateutil.NewRef(1)
b.sharedFieldReferences[historicalRoots] = stateutil.NewRef(1) b.sharedFieldReferences[historicalRoots] = stateutil.NewRef(1)
stateCount.Inc()
return b, nil return b, nil
} }
@ -109,7 +119,7 @@ func (b *BeaconState) Copy() iface.BeaconState {
FinalizedCheckpoint: b.finalizedCheckpoint(), FinalizedCheckpoint: b.finalizedCheckpoint(),
GenesisValidatorsRoot: b.genesisValidatorRoot(), GenesisValidatorsRoot: b.genesisValidatorRoot(),
}, },
dirtyFields: make(map[fieldIndex]interface{}, fieldCount), dirtyFields: make(map[fieldIndex]bool, fieldCount),
dirtyIndices: make(map[fieldIndex][]uint64, fieldCount), dirtyIndices: make(map[fieldIndex][]uint64, fieldCount),
rebuildTrie: make(map[fieldIndex]bool, fieldCount), rebuildTrie: make(map[fieldIndex]bool, fieldCount),
sharedFieldReferences: make(map[fieldIndex]*stateutil.Reference, 10), sharedFieldReferences: make(map[fieldIndex]*stateutil.Reference, 10),
@ -161,6 +171,7 @@ func (b *BeaconState) Copy() iface.BeaconState {
} }
} }
stateCount.Inc()
// Finalizer runs when dst is being destroyed in garbage collection. // Finalizer runs when dst is being destroyed in garbage collection.
runtime.SetFinalizer(dst, func(b *BeaconState) { runtime.SetFinalizer(dst, func(b *BeaconState) {
for field, v := range b.sharedFieldReferences { for field, v := range b.sharedFieldReferences {
@ -168,9 +179,18 @@ func (b *BeaconState) Copy() iface.BeaconState {
if b.stateFieldLeaves[field].reference != nil { if b.stateFieldLeaves[field].reference != nil {
b.stateFieldLeaves[field].reference.MinusRef() b.stateFieldLeaves[field].reference.MinusRef()
} }
}
})
}
for i := 0; i < fieldCount; i++ {
field := fieldIndex(i)
delete(b.stateFieldLeaves, field)
delete(b.dirtyIndices, field)
delete(b.dirtyFields, field)
delete(b.sharedFieldReferences, field)
delete(b.stateFieldLeaves, field)
}
stateCount.Sub(1)
})
return dst return dst
} }
@ -190,7 +210,7 @@ func (b *BeaconState) HashTreeRoot(ctx context.Context) ([32]byte, error) {
} }
layers := stateutil.Merkleize(fieldRoots) layers := stateutil.Merkleize(fieldRoots)
b.merkleLayers = layers b.merkleLayers = layers
b.dirtyFields = make(map[fieldIndex]interface{}, params.BeaconConfig().BeaconStateFieldCount) b.dirtyFields = make(map[fieldIndex]bool, params.BeaconConfig().BeaconStateFieldCount)
} }
for field := range b.dirtyFields { for field := range b.dirtyFields {

View File

@ -81,7 +81,7 @@ var ErrNilInnerState = errors.New("nil inner state")
type BeaconState struct { type BeaconState struct {
state *pbp2p.BeaconState state *pbp2p.BeaconState
lock sync.RWMutex lock sync.RWMutex
dirtyFields map[fieldIndex]interface{} dirtyFields map[fieldIndex]bool
dirtyIndices map[fieldIndex][]uint64 dirtyIndices map[fieldIndex][]uint64
stateFieldLeaves map[fieldIndex]*FieldTrie stateFieldLeaves map[fieldIndex]*FieldTrie
rebuildTrie map[fieldIndex]bool rebuildTrie map[fieldIndex]bool