-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Introduce Thread Safe Map Data Structure #11940
Conversation
container/thread-safe/map.go
Outdated
defer m.lock.RUnlock() | ||
r := make([]K, 0, len(m.items)) | ||
for k := range m.items { | ||
r = append(r, k) |
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.
Depending on what type K
is you might run into the for loop reference bug, where the reference changes with each iteration and in the slice. Since this is generic and to safeguard against all types, you should copy k
before appending it to r
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.
let's just get rid of this method because there is no way of copying k generically unless it implements some Copy method
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.
sorry not a deep copy, but just copy the value . Ex:
copiedK := k
r = append(r, copiedK)
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.
Pushed
This PR introduces a basic, thread-safe map data structure that is type-safe due to generics. It has a dead-simple API, and performance is very close to a concrete data structure. We should aim to use this anywhere where we create locks for the sole purpose of securing a lonely map in Prysm
Bench: