-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Optimize generating UUIDs from strings #7030
Conversation
There is even more room for improvement. The conversion of hex pairs is duplicatesdin the main method and |
Awesome! I don't know how those hardcoded arrays passed a previous review. By the way, for those who wondered about a use case for |
(though the current code will probably be faster than to_u8 on a slice because it goes straight to the point, but this is just a guess) |
Or, since strings are immutable, maybe |
@ysbaddaden The problem is that a String knows its bytesize and utf-8 size, and that's right at the beginning of a String. Another idea would be to have I think for performance it's better to go to |
This improves performance by another 2x, which makes sense because it's doing the conversion once instead of twice.
This is actually kinda where I was trying to go with this, but I figured I'd go with the smaller change. :-)
I found so many roadblocks trying to do that the first time (basically, I felt I had to choose between two potential
|
Another optimization could be to work directly with the return value from |
@ysbaddaden One of the main downsides with this is that it silently can use up a lot of memory, if the substrings have a longer life time than the parent string. Java used to have substrings behave like you suggests here but moved away from it. |
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.
Thank you @jgaskins 👍
I'm building a driver for Neo4j and wanted to check on the overhead of generating UUIDs from strings since it's common to store UUIDs as strings in Neo4j. While it was still really good (1M UUIDs/sec), I noticed that
UUID
incurred a bunch of heap allocations for hardcoded arrays and intermediate strings, so I wanted to see if it could be improved.In total, generating a UUID from a 36-char string involved creating 2 intermediate arrays and 32 intermediate strings (each hex pair in the hardcoded array has one within the loop and another in
string_has_hex_pair_at!
). Moving these to use stack-based values instead of heap, I was able to get a 4x performance boost.Benchmark code