From a9205327def2e915485a0c509bfe85dc71ab8fba Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 29 Aug 2025 00:06:02 +0000 Subject: [PATCH] Optimize batch enqueue by computing base time once per batch Co-authored-by: miles.frankel --- src/storage.rs | 9 +++++---- tests/enqueue_visibility.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 tests/enqueue_visibility.rs diff --git a/src/storage.rs b/src/storage.rs index 61cb6af..45147f6 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -187,15 +187,16 @@ impl Storage { where I: IntoIterator, { + // Compute base time once per batch to avoid per-item syscalls/conversions. + let base_secs = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH)? + .as_secs(); let mut batch = WriteBatchWithTransaction::::default(); // Reuse a byte buffer for capnp serialization across items to avoid repeated allocations. let mut stored_contents: Vec = Vec::new(); for (id, (contents, visibility_timeout_secs)) in items.into_iter() { let main_key = AvailableKey::from_id(id); - let now = std::time::SystemTime::now(); - let visible_ts_secs = (now + std::time::Duration::from_secs(visibility_timeout_secs)) - .duration_since(std::time::UNIX_EPOCH)? - .as_secs(); + let visible_ts_secs = base_secs.saturating_add(visibility_timeout_secs); let visibility_index_key = VisibilityIndexKey::from_visible_ts_and_id(visible_ts_secs, id); diff --git a/tests/enqueue_visibility.rs b/tests/enqueue_visibility.rs new file mode 100644 index 0000000..24e8a21 --- /dev/null +++ b/tests/enqueue_visibility.rs @@ -0,0 +1,35 @@ +use queueber::storage::Storage; + +#[test] +fn enqueue_uses_batched_time_for_visibility_index() { + let tmp = tempfile::tempdir().expect("tempdir"); + let storage = Storage::new(tmp.path()).expect("storage open"); + + // Enqueue three items with increasing visibility offsets + storage + .add_available_items_from_parts([ + (b"id-a".as_ref(), (b"a".as_ref(), 0u64)), + (b"id-b".as_ref(), (b"b".as_ref(), 1u64)), + (b"id-c".as_ref(), (b"c".as_ref(), 2u64)), + ]) + .expect("enqueue batch"); + + // Peek the earliest visibility timestamp; it should be >= now and <= now + 2 + let now_secs = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs(); + + let first_ts = storage + .peek_next_visibility_ts_secs() + .expect("peek ok") + .expect("some ts"); + + // Allow small slop since the test itself takes a little time + assert!( + first_ts >= now_secs && first_ts <= now_secs + 2, + "first_ts={}, now={}", + first_ts, + now_secs + ); +}