snapshot: use per-Snapshot PRNGs to reduce lock contention + cleanup#35
Open
charlievieth wants to merge 1 commit intomasterfrom
Open
snapshot: use per-Snapshot PRNGs to reduce lock contention + cleanup#35charlievieth wants to merge 1 commit intomasterfrom
charlievieth wants to merge 1 commit intomasterfrom
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.
mxr
approved these changes
Aug 19, 2020
|
|
||
| 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.
Should we use Intn(100) for readability? Is that the same thing?
Comment on lines
+85
to
87
| ret := make([]string, 0, len(s.entries)) | ||
| for key := range s.entries { | ||
| ret = append(ret, key) |
There was a problem hiding this comment.
Suggested change
| 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 (?)
Contributor
Author
There was a problem hiding this comment.
It would be if s.entries we're a slice, but it's a map so this will actually fail to compile.
Comment on lines
+67
to
+68
| const a = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" | ||
| const key = a + a |
There was a problem hiding this comment.
Why not initialize key to be a literal "aaaaa....a"?
Comment on lines
+19
to
+21
| r.mu.Lock() | ||
| x := r.rr.Int63() | ||
| r.mu.Unlock() |
There was a problem hiding this comment.
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.