diff --git a/TODO.md b/TODO.md index 30b02df..fb512f1 100644 --- a/TODO.md +++ b/TODO.md @@ -35,7 +35,7 @@ - [ ] (minor) rename keys to ids in `LeaseEntry`, or actually put keys in there. either way - [X] (minor) make `add_available_item_from_parts` wrap `add_available_items_from_parts`, not the other way around - [ ] (minor) use a mockable clock when generating uuidv7s -- [ ] (perf) reduce unnecessary allocs, such as when copying data or allocating buffers. some is called out in code comments +- [X] (perf) reduce unnecessary allocs, such as when copying data or allocating buffers. some is called out in code comments - [ ] (perf) sort the keys in `LeaseEntry` so we can do bsearch on them - [ ] (major) ensure `extend` doesnt create multiple index entries for the same lease. - [ ] (perf) add lease expiry index key to `LeaseEntry` so we don't have to do scans to find it when extending diff --git a/src/server.rs b/src/server.rs index f393022..25d0d40 100644 --- a/src/server.rs +++ b/src/server.rs @@ -116,9 +116,9 @@ 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). - let ids: Vec> = items + let ids: Vec<[u8; 16]> = items .iter() - .map(|_| uuid::Uuid::now_v7().as_bytes().to_vec()) + .map(|_| uuid::Uuid::now_v7().into_bytes()) .collect(); let items_owned = items @@ -135,8 +135,6 @@ impl crate::protocol::queue::Server for Server { let storage = Arc::clone(&self.storage); let notify = Arc::clone(&self.notify); - let ids_for_resp = ids.clone(); - Promise::from_future(async move { // Offload RocksDB work to the blocking thread pool. let iter = ids @@ -150,11 +148,8 @@ impl crate::protocol::queue::Server for Server { } // Build the response on the RPC thread. - let mut ids_builder = results - .get() - .init_resp() - .init_ids(ids_for_resp.len() as u32); - for (i, id) in ids_for_resp.iter().enumerate() { + let mut ids_builder = results.get().init_resp().init_ids(ids.len() as u32); + for (i, id) in ids.iter().enumerate() { ids_builder.set(i as u32, id); } Ok(()) diff --git a/src/storage.rs b/src/storage.rs index c71c6e4..02863d1 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -265,11 +265,10 @@ impl Storage { // Build the lease entry. let lease_entry = build_lease_entry_message(lease_validity_secs, &polled_items)?; - let mut lease_entry_bs = Vec::with_capacity(lease_entry.size_in_words() * 8); // TODO: avoid allocation - serialize::write_message(&mut lease_entry_bs, &lease_entry)?; - + let mut lease_buf = Vec::with_capacity(lease_entry.size_in_words() * 8); + serialize::write_message(&mut lease_buf, &lease_entry)?; // Write the lease entry and its expiry index - txn.put(lease_key.as_ref(), &lease_entry_bs)?; + txn.put(lease_key.as_ref(), &lease_buf)?; txn.put(lease_expiry_index_key.as_ref(), lease_key.as_ref())?; drop(snapshot); @@ -343,7 +342,7 @@ impl Storage { out_keys.set(new_idx as u32, k); new_idx += 1; } - let mut buf = Vec::with_capacity(msg.size_in_words() * 8); // TODO: reduce allocs + let mut buf = Vec::with_capacity(msg.size_in_words() * 8); serialize::write_message(&mut buf, &msg)?; txn.put(lease_key.as_ref(), &buf)?; }