perf(server): eliminate implicit full-collection clones by removing Store::read() in favor of read_with()
This commit is contained in:
1 parent
84ad64156c
commit
1d5e4bd00d
8 files changed
+80
-23
No files matched your search
+4
-9
@@ -59,11 +59,6 @@ impl<T: DeserializeOwned + Default + Serialize + Clone + Send + Sync + 'static>
|
||||
T::default()
|
||||
}
|
||||
|
||||
pub fn read(&self) -> T {
|
||||
let lock = self.cache.read().unwrap_or_else(|e| e.into_inner());
|
||||
lock.clone()
|
||||
}
|
||||
|
||||
pub fn read_with<F, R>(&self, f: F) -> R
|
||||
where
|
||||
F: FnOnce(&T) -> R,
|
||||
@@ -106,7 +101,7 @@ mod tests {
|
||||
let db = Arc::new(db);
|
||||
let store = Store::<TestData>::new("test_key", db.clone());
|
||||
|
||||
assert_eq!(store.read(), TestData::default());
|
||||
assert_eq!(store.read_with(|s| s.clone()), TestData::default());
|
||||
|
||||
store.modify(|data| {
|
||||
data.name = "Hello".to_string();
|
||||
@@ -117,7 +112,7 @@ mod tests {
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
|
||||
|
||||
assert_eq!(
|
||||
store.read(),
|
||||
store.read_with(|s| s.clone()),
|
||||
TestData {
|
||||
name: "Hello".to_string(),
|
||||
value: 42
|
||||
@@ -127,7 +122,7 @@ mod tests {
|
||||
// Load again to verify persistence
|
||||
let store2 = Store::<TestData>::new("test_key", db.clone());
|
||||
assert_eq!(
|
||||
store2.read(),
|
||||
store2.read_with(|s| s.clone()),
|
||||
TestData {
|
||||
name: "Hello".to_string(),
|
||||
value: 42
|
||||
@@ -166,6 +161,6 @@ mod tests {
|
||||
// Wait for all blocking writes to flush
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
|
||||
|
||||
assert_eq!(store.read().value, 50);
|
||||
assert_eq!(store.read_with(|s| s.value), 50);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user