| Crates.io | fast-uuid-v7 |
| lib.rs | fast-uuid-v7 |
| version | 0.1.1 |
| created_at | 2026-01-22 23:11:24.759533+00 |
| updated_at | 2026-01-23 07:53:30.623277+00 |
| description | A high-performance Rust library for generating UUID v7 compatible identifiers. |
| homepage | |
| repository | https://github.com/marcomq/fast-uuid-v7 |
| max_upload_size | |
| id | 2062868 |
| size | 39,550 |
A high-performance Rust library for generating UUID v7 compatible identifiers.
This implementation focuses on speed. It uses thread-local storage and a seeded SmallRng to generate IDs without lock contention, making it suitable for high-throughput applications.
I was testing the performance of network streams and used the original uuid v7 to generate messages
with unique IDs. I wondered why there was a maximum of 400,000 messages per
second, even though the rest of the code looked like it could handle millions.
I figured out that 400,000 is mostly the limit of the standard uuid crate when
using v7. Even v4 was not significantly faster. This happened due to the strong
random number generator used in uuid. While a strong RNG is correct and avoids potential issues
in security or crypto contexts, it is otherwise just very slow.
uuid crateCompared to the standard uuid crate (which may take up to ~1.4µs / 1440ns per ID):
fast-uuid-v7 can be up to ~130x faster (11ns vs 1440ns).As the potential throughput is much higher, the internal counter was increased from 12 bits to 18 bits, and the random part was reduced from 64 bits to 56 bits.
The 128-bit ID is fully compatible with UUID v7. It is composed of:
use fast_uuid_v7::{gen_id, gen_id_string, gen_id_str};
fn main() {
// Get ID as u128, takes about 11-50ns
let id = gen_id();
println!("Generated ID: {:032x}", id);
// Get ID as canonical string (allocates String, takes about 90-130ns)
let id_string = gen_id_string();
println!("Generated ID string: {}", id_string);
// Get ID as stack-allocated string (zero allocation, implements Deref<Target=str>, takes about 23-60ns)
let stack_str = gen_id_str();
println!("Generated ID stack string: {}", stack_str);
}
On a modern machine (e.g., Apple M1 or recent x86_64), you can expect:
gen_id: ~11-50 nsgen_id_str: ~23-60 ns (zero-allocation)gen_id_string: ~90-130 ns (includes heap allocation)Generating 10 million IDs takes approximately 120ms on a single core.
SystemTime::now() is expensive (~20-40ns). We use the internal CPU clock/tick (if available) to check for time passage, calling the actual system time only periodically.rdtsc on x86, cntvct_el0 on ARM) to detect thread sleeps or long pauses cheaply.gen_id_str formats the UUID directly into a stack buffer, avoiding malloc.uuid crate.SystemTime::now(). We still need to call SystemTime::now() from time to time, for example if the previous call was 1ms ago. In that case, we still need to call SystemTime::now() and the performance drops to about 50ns. This is still much faster than the original uuid crate.To check performance on your machine:
# measure time to generate 10 million ids:
cargo test --release -- test_next_id_performance --nocapture
# or - for the alternative bench test that measures the time per ID generation
cargo bench
This generator is designed for database keys and sorting, not for cryptography. The random component is optimized for speed, not unpredictability.
MIT