Optimize Store persistence by eliminating deep clone of internal lock, and optimize relation merging by using retain deduplication.

This commit is contained in:
Riz Ashraf committed 2026-09-21 06:22:05 +01:00
1 parent a9885a65d7
commit e098c257c2
3 files changed
+20 -27

No files matched your search

+14 -16
View File
@@ -30,29 +30,27 @@ impl<T: DeserializeOwned + Default + Serialize + Clone + Send + 'static> Store<T
T::default()
}
fn save_to_db(key: &str, db: &Database, data: &T) {
let write_txn = db.begin_write().unwrap();
{
let mut table = write_txn.open_table(STORE_TABLE).unwrap();
let json_data = serde_json::to_vec(data).unwrap();
table.insert(key, json_data.as_slice()).unwrap();
}
write_txn.commit().unwrap();
}
pub fn read(&self) -> T {
let lock = self.cache.read().unwrap();
lock.clone()
}
pub fn modify<F: FnOnce(&mut T)>(&self, f: F) {
let mut lock = self.cache.write().unwrap();
f(&mut lock);
let key = self.key.clone();
let db = self.db.clone();
let data = lock.clone();
let (key, db, json_data) = {
let mut lock = self.cache.write().unwrap();
f(&mut lock);
// Serialize while holding lock to avoid expensive deep clone of T
let json = serde_json::to_vec(&*lock).unwrap();
(self.key.clone(), self.db.clone(), json)
};
tokio::task::spawn_blocking(move || {
Self::save_to_db(&key, &db, &data);
let write_txn = db.begin_write().unwrap();
{
let mut table = write_txn.open_table(STORE_TABLE).unwrap();
table.insert(key.as_str(), json_data.as_slice()).unwrap();
}
write_txn.commit().unwrap();
});
}
}