From 212fc48da34bff5538af0c70a701947c3418ce72 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 29 Aug 2025 00:12:11 +0000 Subject: [PATCH 1/2] Add UUID batch generation utility for efficient ID creation Co-authored-by: miles.frankel --- src/lib.rs | 1 + src/server/mod.rs | 5 +---- src/uuid_utils.rs | 35 +++++++++++++++++++++++++++++++++++ tests/uuid_utils.rs | 12 ++++++++++++ 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 src/uuid_utils.rs create mode 100644 tests/uuid_utils.rs diff --git a/src/lib.rs b/src/lib.rs index e252a60..b8a1254 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ pub mod errors; pub mod protocol; pub mod server; pub mod storage; +pub mod uuid_utils; // Re-export commonly used types pub use crate::storage::RetriedStorage; diff --git a/src/server/mod.rs b/src/server/mod.rs index 192542b..08ba9f6 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -126,10 +126,7 @@ impl crate::protocol::queue::Server for Server { // Generate ids upfront and copy request data into owned memory so we can move // it into a blocking task (capnp readers are not Send). // Store ids inline as [u8;16] to avoid per-id heap allocations. - let ids: Vec<[u8; 16]> = items - .iter() - .map(|_| uuid::Uuid::now_v7().into_bytes()) - .collect(); + let ids: Vec<[u8; 16]> = crate::uuid_utils::generate_uuidv7_batch(items.len() as usize); let items_owned = items .iter() diff --git a/src/uuid_utils.rs b/src/uuid_utils.rs new file mode 100644 index 0000000..6531965 --- /dev/null +++ b/src/uuid_utils.rs @@ -0,0 +1,35 @@ +use uuid::Uuid; + +/// Generate `count` UUIDv7 identifiers as raw 16-byte arrays. +/// +/// Optimizations: +/// - Pre-allocates the full vector capacity +/// - Tight for-loop to minimize iterator overhead +/// +/// Note: We rely on `Uuid::now_v7()` for correctness and monotonicity semantics. +/// If further optimization is needed, consider constructing v7s from a cached +/// timestamp source once the `uuid` crate exposes a stable builder API for v7. +pub fn generate_uuidv7_batch(count: usize) -> Vec<[u8; 16]> { + let mut ids: Vec<[u8; 16]> = Vec::with_capacity(count); + for _ in 0..count { + ids.push(Uuid::now_v7().into_bytes()); + } + ids +} + +#[cfg(test)] +mod tests { + use super::*; + use std::collections::HashSet; + + #[test] + fn batch_generates_correct_count_and_unique_ids() { + let n = 1024; + let ids = generate_uuidv7_batch(n); + assert_eq!(ids.len(), n); + let mut set: HashSet<[u8; 16]> = HashSet::with_capacity(n); + for id in ids { + assert!(set.insert(id), "duplicate id generated in batch"); + } + } +} diff --git a/tests/uuid_utils.rs b/tests/uuid_utils.rs new file mode 100644 index 0000000..e317beb --- /dev/null +++ b/tests/uuid_utils.rs @@ -0,0 +1,12 @@ +use std::collections::HashSet; + +#[test] +fn generates_batch_with_expected_size_and_uniqueness() { + let n = 4096; + let ids = queueber::uuid_utils::generate_uuidv7_batch(n); + assert_eq!(ids.len(), n); + let mut set: HashSet<[u8; 16]> = HashSet::with_capacity(n); + for id in ids { + assert!(set.insert(id), "duplicate id detected"); + } +} From b34182900f27a09b5828261e46cacc34a29605ac Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 29 Aug 2025 01:51:23 +0000 Subject: [PATCH 2/2] Remove uuid_utils module and inline UUID generation in server Co-authored-by: miles.frankel --- docs/perf-notes.md | 1 + src/lib.rs | 1 - src/server/mod.rs | 5 ++++- src/uuid_utils.rs | 35 ----------------------------------- tests/uuid_utils.rs | 12 ------------ 5 files changed, 5 insertions(+), 49 deletions(-) delete mode 100644 src/uuid_utils.rs delete mode 100644 tests/uuid_utils.rs diff --git a/docs/perf-notes.md b/docs/perf-notes.md index b4cab3d..ff9e1f4 100644 --- a/docs/perf-notes.md +++ b/docs/perf-notes.md @@ -98,6 +98,7 @@ Recommended fixes (incremental): - Prefer `Uuid::new_v4()` (or a fast 128-bit random) for leases unless you need time-sortable IDs; you already index lease expiry by timestamp. - Minimize logging in hot paths or gate behind `trace`. - Avoid rebuilding Cap’n Proto messages where a lightweight copy/reference suffices. + - UUID generation batching (tried, no win): We pre-generated uuidv7 values in a tight loop with preallocated buffers to reduce per-call overhead. Benchmarks and e2e tests showed no measurable throughput or CPU improvement, so this change was reverted. See PR: . ### Larger simplification (optional) diff --git a/src/lib.rs b/src/lib.rs index b8a1254..e252a60 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,6 @@ pub mod errors; pub mod protocol; pub mod server; pub mod storage; -pub mod uuid_utils; // Re-export commonly used types pub use crate::storage::RetriedStorage; diff --git a/src/server/mod.rs b/src/server/mod.rs index 08ba9f6..192542b 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -126,7 +126,10 @@ impl crate::protocol::queue::Server for Server { // Generate ids upfront and copy request data into owned memory so we can move // it into a blocking task (capnp readers are not Send). // Store ids inline as [u8;16] to avoid per-id heap allocations. - let ids: Vec<[u8; 16]> = crate::uuid_utils::generate_uuidv7_batch(items.len() as usize); + let ids: Vec<[u8; 16]> = items + .iter() + .map(|_| uuid::Uuid::now_v7().into_bytes()) + .collect(); let items_owned = items .iter() diff --git a/src/uuid_utils.rs b/src/uuid_utils.rs deleted file mode 100644 index 6531965..0000000 --- a/src/uuid_utils.rs +++ /dev/null @@ -1,35 +0,0 @@ -use uuid::Uuid; - -/// Generate `count` UUIDv7 identifiers as raw 16-byte arrays. -/// -/// Optimizations: -/// - Pre-allocates the full vector capacity -/// - Tight for-loop to minimize iterator overhead -/// -/// Note: We rely on `Uuid::now_v7()` for correctness and monotonicity semantics. -/// If further optimization is needed, consider constructing v7s from a cached -/// timestamp source once the `uuid` crate exposes a stable builder API for v7. -pub fn generate_uuidv7_batch(count: usize) -> Vec<[u8; 16]> { - let mut ids: Vec<[u8; 16]> = Vec::with_capacity(count); - for _ in 0..count { - ids.push(Uuid::now_v7().into_bytes()); - } - ids -} - -#[cfg(test)] -mod tests { - use super::*; - use std::collections::HashSet; - - #[test] - fn batch_generates_correct_count_and_unique_ids() { - let n = 1024; - let ids = generate_uuidv7_batch(n); - assert_eq!(ids.len(), n); - let mut set: HashSet<[u8; 16]> = HashSet::with_capacity(n); - for id in ids { - assert!(set.insert(id), "duplicate id generated in batch"); - } - } -} diff --git a/tests/uuid_utils.rs b/tests/uuid_utils.rs deleted file mode 100644 index e317beb..0000000 --- a/tests/uuid_utils.rs +++ /dev/null @@ -1,12 +0,0 @@ -use std::collections::HashSet; - -#[test] -fn generates_batch_with_expected_size_and_uniqueness() { - let n = 4096; - let ids = queueber::uuid_utils::generate_uuidv7_batch(n); - assert_eq!(ids.len(), n); - let mut set: HashSet<[u8; 16]> = HashSet::with_capacity(n); - for id in ids { - assert!(set.insert(id), "duplicate id detected"); - } -}