Skip to content

Commit

Permalink
Minor State Management Improvements (#8742)
Browse files Browse the repository at this point in the history
* add improvement

* change to bool

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
nisdas and prylabs-bulldozer[bot] authored Apr 13, 2021
1 parent 45d2df1 commit 131a14e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 2 additions & 0 deletions beacon-chain/state/stateV0/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ go_library(
"@com_github_dgraph_io_ristretto//:go_default_library",
"@com_github_gogo_protobuf//proto: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_ethereumapis//eth/v1:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
Expand Down
28 changes: 24 additions & 4 deletions beacon-chain/state/stateV0/state_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
v1 "github.com/prysmaticlabs/ethereumapis/eth/v1"
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
Expand All @@ -20,6 +22,13 @@ import (
"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.
func InitializeFromProto(st *pbp2p.BeaconState) (*BeaconState, error) {
return InitializeFromProtoUnsafe(proto.Clone(st).(*pbp2p.BeaconState))
Expand All @@ -35,7 +44,7 @@ func InitializeFromProtoUnsafe(st *pbp2p.BeaconState) (*BeaconState, error) {
fieldCount := params.BeaconConfig().BeaconStateFieldCount
b := &BeaconState{
state: st,
dirtyFields: make(map[fieldIndex]interface{}, fieldCount),
dirtyFields: make(map[fieldIndex]bool, fieldCount),
dirtyIndices: make(map[fieldIndex][]uint64, fieldCount),
stateFieldLeaves: make(map[fieldIndex]*FieldTrie, fieldCount),
sharedFieldReferences: make(map[fieldIndex]*stateutil.Reference, 10),
Expand Down Expand Up @@ -66,6 +75,7 @@ func InitializeFromProtoUnsafe(st *pbp2p.BeaconState) (*BeaconState, error) {
b.sharedFieldReferences[balances] = stateutil.NewRef(1)
b.sharedFieldReferences[historicalRoots] = stateutil.NewRef(1)

stateCount.Inc()
return b, nil
}

Expand Down Expand Up @@ -109,7 +119,7 @@ func (b *BeaconState) Copy() iface.BeaconState {
FinalizedCheckpoint: b.finalizedCheckpoint(),
GenesisValidatorsRoot: b.genesisValidatorRoot(),
},
dirtyFields: make(map[fieldIndex]interface{}, fieldCount),
dirtyFields: make(map[fieldIndex]bool, fieldCount),
dirtyIndices: make(map[fieldIndex][]uint64, fieldCount),
rebuildTrie: make(map[fieldIndex]bool, fieldCount),
sharedFieldReferences: make(map[fieldIndex]*stateutil.Reference, 10),
Expand Down Expand Up @@ -161,16 +171,26 @@ func (b *BeaconState) Copy() iface.BeaconState {
}
}

stateCount.Inc()
// Finalizer runs when dst is being destroyed in garbage collection.
runtime.SetFinalizer(dst, func(b *BeaconState) {
for field, v := range b.sharedFieldReferences {
v.MinusRef()
if b.stateFieldLeaves[field].reference != nil {
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
}

Expand All @@ -190,7 +210,7 @@ func (b *BeaconState) HashTreeRoot(ctx context.Context) ([32]byte, error) {
}
layers := stateutil.Merkleize(fieldRoots)
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 {
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/state/stateV0/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ var ErrNilInnerState = errors.New("nil inner state")
type BeaconState struct {
state *pbp2p.BeaconState
lock sync.RWMutex
dirtyFields map[fieldIndex]interface{}
dirtyFields map[fieldIndex]bool
dirtyIndices map[fieldIndex][]uint64
stateFieldLeaves map[fieldIndex]*FieldTrie
rebuildTrie map[fieldIndex]bool
Expand Down

0 comments on commit 131a14e

Please sign in to comment.