Skip to content

Conversation

@charlievieth
Copy link
Contributor

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.

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)
Copy link

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.

Comment on lines +85 to 87
ret := make([]string, 0, len(s.entries))
for key := range s.entries {
ret = append(ret, key)
Copy link

Choose a reason for hiding this comment

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

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 (?)

Copy link
Contributor Author

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.

Copy link

Choose a reason for hiding this comment

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

Oops

Comment on lines +67 to +68
const a = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
const key = a + a
Copy link

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"?

Comment on lines +19 to +21
r.mu.Lock()
x := r.rr.Int63()
r.mu.Unlock()
Copy link

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)
Copy link

Choose a reason for hiding this comment

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

Intn?

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.

3 participants