Skip to content
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

uuid: avoid using deprecated math/rand.Read #110656

Closed

Conversation

rafiss
Copy link
Collaborator

@rafiss rafiss commented Sep 14, 2023

Now we generate uint64s instead and populate them into the UUID array.

goos: darwin
goarch: arm64
              │ /var/folders/p3/c61_z_vd3r7dr1_hnztm3ryr0000gq/T/tmp.BIfUofSiKJ/bench.0cde11b885a │ /var/folders/p3/c61_z_vd3r7dr1_hnztm3ryr0000gq/T/tmp.BIfUofSiKJ/bench.fbea7746013 │
              │                                      sec/op                                       │                          sec/op                            vs base                │
FastMakeV4-10                                                                         22.20n ± 0%                                                 28.34n ± 1%  +27.69% (p=0.000 n=10)

              │ /var/folders/p3/c61_z_vd3r7dr1_hnztm3ryr0000gq/T/tmp.BIfUofSiKJ/bench.0cde11b885a │ /var/folders/p3/c61_z_vd3r7dr1_hnztm3ryr0000gq/T/tmp.BIfUofSiKJ/bench.fbea7746013 │
              │                                       B/op                                        │                             B/op                               vs base            │
FastMakeV4-10                                                                          0.000 ± 0%                                                      0.000 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal

              │ /var/folders/p3/c61_z_vd3r7dr1_hnztm3ryr0000gq/T/tmp.BIfUofSiKJ/bench.0cde11b885a │ /var/folders/p3/c61_z_vd3r7dr1_hnztm3ryr0000gq/T/tmp.BIfUofSiKJ/bench.fbea7746013 │
              │                                     allocs/op                                     │                           allocs/op                            vs base            │
FastMakeV4-10                                                                          0.000 ± 0%                                                      0.000 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal

fixes #110597
Release note: None

Using Int63 as before is a mistake, since that only generates positive
numbers, meaning the sign bit is never random.

Release note: None
Now we generate uint64s instead and populate them into the UUID array.

Release note: None
@rafiss rafiss requested a review from bdarnell September 14, 2023 17:36
@blathers-crl
Copy link

blathers-crl bot commented Sep 14, 2023

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rafiss
Copy link
Collaborator Author

rafiss commented Sep 14, 2023

@bdarnell I gave this a shot, and it made the function ~28% slower. Is there a faster way to populate the backing UUID array?

@bdarnell
Copy link
Contributor

I don't think there's a faster way to populate the array; I checked on godbolt.org and the encoding/binary functions appear to get inlined away. It must be a difference between the two calls to Uint64 vs one call to Read. Maybe because there are two lock acquisitions now? (Maybe. I also see references to a non-locking fast path in the math/rand source code but I haven't dug down to exactly what's happening)

Anyway, that's a pretty steep performance penalty just to avoid a deprecated function so I think I'd just leave it alone for now. Maybe a non-deprecated Read method (with a different name) will come back in the revised math/rand API.

@rafiss
Copy link
Collaborator Author

rafiss commented Sep 14, 2023

I also see references to a non-locking fast path in the math/rand source code but I haven't dug down to exactly what's happening

Were you referring to this change: golang/go@0b9974d? If so, that's only available in go1.21.


Anyway, for now I will close this out and leave the linked issue open.

@rafiss rafiss closed this Sep 14, 2023
@bdarnell
Copy link
Contributor

Ah yes, that's what I saw.

@rafiss
Copy link
Collaborator Author

rafiss commented Sep 14, 2023

I created #110670 since working in this area made me see other worthwhile cleanups

@rafiss rafiss deleted the use-non-deprecated-rand-uuid branch September 14, 2023 19:03
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.

util/uuid: move to a non-deprecated randomness source
3 participants