Bläddra i källkod

Remove synchronization overhead by swapping Mutex for RefCell.

Kestrel 2 år sedan
förälder
incheckning
6582831fb8
1 ändrade filer med 3 tillägg och 4 borttagningar
  1. 3 4
      microrm/src/query.rs

+ 3 - 4
microrm/src/query.rs

@@ -78,7 +78,7 @@ type CacheIndex = u64;
 pub struct QueryInterface<'l> {
     db: &'l crate::DB,
 
-    cache: std::sync::Mutex<std::collections::HashMap<CacheIndex, sqlite::Statement<'l>>>,
+    cache: std::cell::RefCell<std::collections::HashMap<CacheIndex, sqlite::Statement<'l>>>,
 
     // use a phantom non-Send-able type to implement !Send for QueryInterface
     prevent_send: std::marker::PhantomData<*mut ()>,
@@ -90,7 +90,7 @@ impl<'l> QueryInterface<'l> {
     pub fn new(db: &'l crate::DB) -> Self {
         Self {
             db,
-            cache: std::sync::Mutex::new(std::collections::HashMap::new()),
+            cache: std::cell::RefCell::new(std::collections::HashMap::new()),
             prevent_send: std::marker::PhantomData,
         }
     }
@@ -104,7 +104,6 @@ impl<'l> QueryInterface<'l> {
         let state = stmt.next()?;
         if state != sqlite::State::Row {
             return Ok(None);
-            // return Err(crate::Error::ExecFailure)
         }
 
         Ok(Some(with_result(stmt)?))
@@ -134,7 +133,7 @@ impl<'l> QueryInterface<'l> {
         mut with: With,
     ) -> Return
 where {
-        let mut cache = self.cache.lock().expect("Couldn't acquire cache?");
+        let mut cache = self.cache.borrow_mut();
         let query = cache.entry(hash).or_insert_with(create);
         query.reset().expect("Couldn't reset query");
         with(query)