BeaconState: Use copy on write for validator index map (#4713)

* Use copy on write for validator index map
* Merge refs/heads/master into copy-on-write-2
This commit is contained in:
Preston Van Loon 2020-02-01 17:21:50 -08:00 committed by GitHub
parent b7e6012628
commit f432f7851e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 9 deletions

View File

@ -324,6 +324,15 @@ func (b *BeaconState) ValidatorIndexByPubkey(key [48]byte) (uint64, bool) {
return idx, ok
}
func (b *BeaconState) validatorIndexMap() map[[48]byte]uint64 {
m := make(map[[48]byte]uint64, len(b.valIdxMap))
for k, v := range b.valIdxMap {
m[k] = v
}
return m
}
// PubkeyAtIndex returns the pubkey at the given
// validator index.
func (b *BeaconState) PubkeyAtIndex(idx uint64) [48]byte {

View File

@ -228,10 +228,10 @@ func (b *BeaconState) UpdateValidatorAtIndex(idx uint64, val *ethpb.Validator) e
// SetValidatorIndexByPubkey updates the validator index mapping maintained internally to
// a given input 48-byte, public key.
func (b *BeaconState) SetValidatorIndexByPubkey(pubKey [48]byte, validatorIdx uint64) {
b.lock.Lock()
b.valIdxMap[pubKey] = validatorIdx
b.markFieldAsDirty(validators)
b.lock.Unlock()
// Copy on write since this is a shared map.
m := b.validatorIndexMap()
m[pubKey] = validatorIdx
b.valIdxMap = m
}
// SetBalances for the beacon state. This PR updates the entire

View File

@ -84,17 +84,15 @@ func (b *BeaconState) Copy() *BeaconState {
FinalizedCheckpoint: b.FinalizedCheckpoint(),
},
dirtyFields: make(map[fieldIndex]interface{}, 20),
valIdxMap: make(map[[48]byte]uint64, len(b.valIdxMap)),
// Copy on write validator index map.
valIdxMap: b.valIdxMap,
}
for i := range b.dirtyFields {
dst.dirtyFields[i] = true
}
for i := range b.valIdxMap {
dst.valIdxMap[i] = b.valIdxMap[i]
}
dst.merkleLayers = make([][][]byte, len(b.merkleLayers))
for i, layer := range b.merkleLayers {
dst.merkleLayers[i] = make([][]byte, len(layer))