From 595a5bbe29eb72d769f34199d13bff55f359e246 Mon Sep 17 00:00:00 2001 From: Chen Chen Date: Thu, 15 Aug 2024 14:44:30 -0500 Subject: [PATCH] store: use optimized get/set in scc Squashed commit of the following: commit 181b1e4a1478254e96b8ab41dd99eecd6a2a7954 Author: Changgyoo Park Date: Thu Aug 15 19:39:35 2024 +0200 Fix `set` commit c963e2863fe82a47c9f3a7a23d953c8f308bf8ca Author: Changgyoo Park Date: Thu Aug 15 17:36:14 2024 +0200 More fix commit 0c2fad2c97a67091f684b2998573d2a03a97cbd5 Author: Changgyoo Park Date: Thu Aug 15 17:14:37 2024 +0200 store: use a more optimized get method in scc.rs Replace scc::HashMap::get with scc::HashMap::read as scc::HashMap::get acquires exclusive locks for entry modification whereas scc::HashMap::read acquires shared locks and is more suitable for read-heavy workloads. Co-authored-by: Changgyoo Park --- src/stores/scc.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/stores/scc.rs b/src/stores/scc.rs index fb9246c..4634ca7 100644 --- a/src/stores/scc.rs +++ b/src/stores/scc.rs @@ -34,16 +34,18 @@ impl KVMap for SccHashMap { impl KVMapHandle for SccHashMap { fn set(&mut self, key: &[u8], value: &[u8]) { - if let Err(_) = self.0.insert(key.into(), value.into()) { - assert!(self.0.update(key, |_, v| *v = value.into()).is_some()); + match self.0.entry(key.into()) { + scc::hash_map::Entry::Occupied(mut o) => { + *o.get_mut() = value.into(); + } + scc::hash_map::Entry::Vacant(v) => { + v.insert_entry(value.into()); + } } } fn get(&mut self, key: &[u8]) -> Option> { - match self.0.get(key) { - Some(r) => Some(r.clone()), - None => None, - } + self.0.read(key, |_, r| r.clone()) } fn delete(&mut self, key: &[u8]) {