From 64577a7404d47ca2955fe6fa3f035eded8df6625 Mon Sep 17 00:00:00 2001 From: Zach McCoy Date: Sat, 29 Aug 2026 14:04:41 -0500 Subject: [PATCH 1/2] Use fixed size vecs, add comments for cmsobject Signed-off-by: Zach McCoy --- src/cms/cms_command_handler.rs | 4 ++-- src/cms/utils.rs | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/cms/cms_command_handler.rs b/src/cms/cms_command_handler.rs index 077b88f2..c6c2f350 100644 --- a/src/cms/cms_command_handler.rs +++ b/src/cms/cms_command_handler.rs @@ -179,7 +179,7 @@ pub fn cms_increment_by(ctx: &Context, args: Vec) -> ValkeyResult } let mut i = 2; - let mut pairs: Vec<(&[u8], u64)> = Vec::new(); + let mut pairs: Vec<(&[u8], u64)> = Vec::with_capacity(args_left / 2); while i < args_count { let k = args[i].as_slice(); let v = args[i + 1] @@ -195,7 +195,7 @@ pub fn cms_increment_by(ctx: &Context, args: Vec) -> ValkeyResult Err(_) => return Err(ValkeyError::WrongType), }; - let mut results = Vec::new(); + let mut results = Vec::with_capacity(args_left); match value { None => Err(ValkeyError::nonexistent_key()), Some(v) => { diff --git a/src/cms/utils.rs b/src/cms/utils.rs index 240c10d7..85915705 100644 --- a/src/cms/utils.rs +++ b/src/cms/utils.rs @@ -45,6 +45,7 @@ pub struct CMSObject { impl CMSObject { //TODO: Are there any MAX ranges that we should have for configuration? + /// Create a new CMSObject based on the size requirements given. pub fn new_by_dimension(width: u64, depth: u64) -> Result { if width < 1 { return Err(CMSError::InvalidWidth); @@ -60,8 +61,8 @@ impl CMSObject { Ok(obj) } - //Error_tolerance is max variance of the count - // probability is the false positive rate + /// Create a new CMSObject based on the error tolerance, max variance of the count, + /// and probabiilty, probability of false positive rates, the caller is okay with. pub fn new_by_probability( error_tolerance: f64, probability: f64, @@ -91,15 +92,19 @@ impl CMSObject { self.depth } + /// Returns the total number of increments that have been done to the keys in the sketch pub fn total(&self) -> u64 { self.cms.sketch.total_count() } + /// Increases the count for the item given by the amount of the increment. + /// Note that this currently has to call estimate to get the count back for that item. pub fn increment_by(&mut self, item: &[u8], increment: u64) -> u64 { self.cms.increment_item(item, increment); self.cms.estimate_item(item) } + /// Gives back the estimated count, or frequency, for the given item. pub fn estimate(&self, item: &[u8]) -> u64 { self.cms.estimate_item(item) } From 724e523a68a8fd5d9df551d2c653958214a5e934 Mon Sep 17 00:00:00 2001 From: Zach McCoy Date: Sat, 29 Aug 2026 14:09:55 -0500 Subject: [PATCH 2/2] Fix size, calc once Signed-off-by: Zach McCoy --- src/cms/cms_command_handler.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cms/cms_command_handler.rs b/src/cms/cms_command_handler.rs index c6c2f350..43b1145a 100644 --- a/src/cms/cms_command_handler.rs +++ b/src/cms/cms_command_handler.rs @@ -179,7 +179,8 @@ pub fn cms_increment_by(ctx: &Context, args: Vec) -> ValkeyResult } let mut i = 2; - let mut pairs: Vec<(&[u8], u64)> = Vec::with_capacity(args_left / 2); + let pairs_count = args_left / 2; + let mut pairs: Vec<(&[u8], u64)> = Vec::with_capacity(pairs_count); while i < args_count { let k = args[i].as_slice(); let v = args[i + 1] @@ -195,7 +196,7 @@ pub fn cms_increment_by(ctx: &Context, args: Vec) -> ValkeyResult Err(_) => return Err(ValkeyError::WrongType), }; - let mut results = Vec::with_capacity(args_left); + let mut results = Vec::with_capacity(pairs_count); match value { None => Err(ValkeyError::nonexistent_key()), Some(v) => {