fast-uuid-v7 - creating uuidv7 faster than allocating a string

189 points marco-mq 37 comments

The uuid crate is great. But I was testing network traffic and wanted to give each package a unique uuidv7. I always hit a "wall" of 300k msg/sec. Then, I repeated the test with memory channels and still got 300k msg/sec on my m1. Turned out it was uuid uuidv7 that has a very secure but inefficient random number generator.

I replaced the secure random number generator with SmallRng, moved string generation to a stack buffer and avoided to call get_time syscalls by using the CPU ticks, if available.

Result: uuidv7 got 165x faster on my m1. Same string format. Same u128 format. Still 8 times faster than using uuid with feature fast-rng. Generating a u128 took now just 8ns. A &str still takes 22ns. As String takes about 80ns.

Disclaimer: Don't use it to prime passwords or tokens. Just for network, DB ids or non-security related ids. Also, it cannot parse uuidv7 - use the normal uuid crate for parsing or to create secure random uuids.

https://github.com/marcomq/fast-uuid-v7