-
Notifications
You must be signed in to change notification settings - Fork 12
snapshot: use per-Snapshot PRNGs to reduce lock contention + cleanup #35
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
base: master
Are you sure you want to change the base?
Conversation
This commit changes the snapshot package to use per-Snapshot PRNGs instead of a single global PRNG - this reduces lock contention. Additionally, it cleans up the code a bit by changing map accesses to check if the result if nil instead of doing the "val, ok" check. These changes are pretty minor and are mostly a nit.
|
|
||
| func (s *Snapshot) FeatureEnabled(key string, defaultValue uint64) bool { | ||
| return defaultRandomGenerator.Random()%100 < min(s.GetInteger(key, defaultValue), 100) | ||
| return s.rand.Uint64()%100 < min(s.GetInteger(key, defaultValue), 100) |
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.
Should we use Intn(100) for readability? Is that the same thing?
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.
Yes.
| ret := make([]string, 0, len(s.entries)) | ||
| for key := range s.entries { | ||
| ret = append(ret, key) |
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.
| ret := make([]string, 0, len(s.entries)) | |
| for key := range s.entries { | |
| ret = append(ret, key) | |
| ret := make([]string, len(s.entries)) | |
| for i, key := range s.entries { | |
| ret[i] = key |
This is faster (?)
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.
It would be if s.entries we're a slice, but it's a map so this will actually fail to compile.
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.
Oops
| const a = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" | ||
| const key = a + a |
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.
Why not initialize key to be a literal "aaaaa....a"?
| r.mu.Lock() | ||
| x := r.rr.Int63() | ||
| r.mu.Unlock() |
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.
Should/could we use a threadsafe source instead of locking? Or will a threadsafe source also lock under the hood?
|
|
||
| func (n *Nil) FeatureEnabled(key string, defaultValue uint64) bool { | ||
| return defaultRandomGenerator.Random()%100 < min(defaultValue, 100) | ||
| return nilRandom.Uint64()%100 < min(defaultValue, 100) |
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.
Intn?
This commit changes the snapshot package to use per-Snapshot PRNGs
instead of a single global PRNG - this reduces lock contention.
Additionally, it cleans up the code a bit by changing map accesses to
check if the result if nil instead of doing the "val, ok" check.
These changes are pretty minor and are mostly a nit.