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
Shard validator info handler map interface #3907
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor stuff
// 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))} |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
len not required
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
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] { |
There was a problem hiding this comment.
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 {
....
There was a problem hiding this comment.
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 usingfor 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
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 !
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 PRsValidatorInfoHandler
(similar withHeaderHandler
- getters + setters) interface to be used instead of*ValidatorInfo