Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shard validator info handler map interface #3907

Merged

Conversation

mariusmihaic
Copy link
Contributor

@mariusmihaic mariusmihaic commented Mar 16, 2022

  • Added ShardValidatorsInfoMapHandler interface which shall be used to manage operations inside a <shardID,[]ValidatorInfoHandler> map in a concurrent-safe way. This is used to replace operations with pointers on a <shardID, []*ValidatorInfo> map. Integration PRs with this interface will follow. For now, basic functionalities have been provided, this interface being subject to change in future PRs
  • Added ValidatorInfoHandler (similar with HeaderHandler - getters + setters) interface to be used instead of *ValidatorInfo
  • Added full branch coverage + concurrency unit tests

@mariusmihaic mariusmihaic changed the title FEAT: Add files Shard validator info handler map interface Mar 16, 2022
@mariusmihaic mariusmihaic self-assigned this Mar 17, 2022
@mariusmihaic mariusmihaic marked this pull request as ready for review March 17, 2022 16:19
Copy link
Contributor

@iulianpascalau iulianpascalau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor stuff

state/validatorsInfoMap.go Outdated Show resolved Hide resolved
// CreateShardValidatorsMap creates an instance of shardValidatorsInfoMap which manages a shard validator
// info map internally.
func CreateShardValidatorsMap(input map[uint32][]*ValidatorInfo) *shardValidatorsInfoMap {
ret := &shardValidatorsInfoMap{valInfoMap: make(map[uint32][]ValidatorInfoHandler, len(input))}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here, we do not need the call to len(input)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here it is ok to call len to allocate memory for the map, since they will have the same size, right?


// GetShardValidatorsInfoMap returns a <shard, ValidatorInfoHandler> map copy of internally stored data
func (vi *shardValidatorsInfoMap) GetShardValidatorsInfoMap() map[uint32][]ValidatorInfoHandler {
ret := make(map[uint32][]ValidatorInfoHandler, len(vi.valInfoMap))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len not required

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above. This link also states something about it:

https://stackoverflow.com/questions/53388332/memory-efficient-implementation-of-go-map

Go maps are optimized for integer keys. Optimize the map allocation by giving the exact map size as a hint.

// Add adds a ValidatorInfoHandler in its corresponding shardID
func (vi *shardValidatorsInfoMap) Add(validator ValidatorInfoHandler) error {
if check.IfNil(validator) {
return ErrNilValidatorInfo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

vi.mutex.Lock()
defer vi.mutex.Unlock()

for idx, validator := range vi.valInfoMap[shardID] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slight optimization:

validatorSlice := vi.valInfoMap[shardID]
for idx, validator := range validatorSlice {
....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it seems like this is very slightly less efficient. I've run a benchmark on a map with 1,000,000 entries in which

  • Iterate1 iterates using for range vi.valInfoMap[shardID]
  • Iterate2 iterates using:
 vSlice := vi.valInfoMap[shardID]
	for range vSlice {
}

Results:

BenchmarkShardValidatorsInfoMap_IterateMap1/iterate_1
BenchmarkShardValidatorsInfoMap_IterateMap1/iterate_1-8         	1000000000	         0.0002455 ns/op
BenchmarkShardValidatorsInfoMap_IterateMap1/iterate_2
BenchmarkShardValidatorsInfoMap_IterateMap1/iterate_2-8         	1000000000	         0.0002704 ns/op

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

defer vi.mutex.RUnlock()

for shardID, validatorsInShard := range vi.valInfoMap {
validatorsCopy := make([]ValidatorInfoHandler, len(validatorsInShard))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 by always using copies of slices you prevented the wrongly usage of this component, by directly making a slice element to nil that would have altered the inner map's components

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, this was the intention !

state/validatorsInfoMap_test.go Outdated Show resolved Hide resolved
@bogdan-rosianu bogdan-rosianu requested review from bogdan-rosianu and removed request for bogdan-rosianu March 18, 2022 13:45
Base automatically changed from EN-11664-nodes-coordinator-staking-v4 to feat/liquid-staking March 24, 2022 10:48
@iulianpascalau iulianpascalau merged commit a841644 into feat/liquid-staking Mar 24, 2022
@iulianpascalau iulianpascalau deleted the validators-info-interface-defintion-v3 branch March 24, 2022 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants