Forráskód Böngészése

Improve memory usage

Use the memory allocated inside `query` as they will no longer be needed, and use that memory in the secondary index iterator
Cesar Rodas 3 hónapja
szülő
commit
a57ba35f5a

+ 10 - 13
crates/storage/memory/src/lib.rs

@@ -132,7 +132,7 @@ impl Storage for Memory {
         return Ok(None);
     }
 
-    async fn get_by_filter(&self, query: Filter) -> Result<Self::Cursor<'_>, Error> {
+    async fn get_by_filter(&self, mut query: Filter) -> Result<Self::Cursor<'_>, Error> {
         let limit = if query.limit == 0 {
             None
         } else {
@@ -142,28 +142,25 @@ impl Storage for Memory {
         let (index, index_prefixes) = if !query.references_to_event.is_empty() {
             (
                 self.indexes.ref_event.read().await,
-                query
-                    .references_to_event
-                    .iter()
-                    .map(|c| c.as_ref().to_vec())
+                std::mem::take(&mut query.references_to_event)
+                    .into_iter()
+                    .map(|c| c.take())
                     .collect::<VecDeque<_>>(),
             )
         } else if !query.references_to_public_key.is_empty() {
             (
                 self.indexes.ref_pub_key.read().await,
-                query
-                    .references_to_public_key
-                    .iter()
-                    .map(|c| c.as_ref().to_vec())
+                std::mem::take(&mut query.references_to_public_key)
+                    .into_iter()
+                    .map(|c| c.take())
                     .collect::<VecDeque<_>>(),
             )
         } else if !query.ids.is_empty() {
             (
                 self.indexes.ids_by_time.read().await,
-                query
-                    .ids
-                    .iter()
-                    .map(|c| c.as_ref().to_vec())
+                std::mem::take(&mut query.ids)
+                    .into_iter()
+                    .map(|c| c.take())
                     .collect::<VecDeque<_>>(),
             )
         } else {

+ 17 - 19
crates/storage/rocksdb/src/lib.rs

@@ -226,42 +226,40 @@ impl Storage for RocksDb {
             let ns: Arc<BoundColumnFamily<'_>> =
                 self.reference_to_cf_handle(ReferenceType::RefEvent)?;
 
-            let keys = query
-                .references_to_event
-                .iter()
-                .map(|c| c.as_ref().to_vec())
+            let keys = std::mem::take(&mut query.references_to_event)
+                .into_iter()
+                .map(|c| c.take())
                 .collect();
-            query.references_to_event.clear();
             (Some(ns), None, keys)
         } else if !query.references_to_public_key.is_empty() {
             let ns = self.reference_to_cf_handle(ReferenceType::RefEvent)?;
-            let keys = query
-                .references_to_public_key
-                .iter()
-                .map(|c| c.as_ref().to_vec())
+            let keys = std::mem::take(&mut query.references_to_public_key)
+                .into_iter()
+                .map(|c| c.take())
                 .collect();
-            query.references_to_public_key.clear();
             (Some(ns), None, keys)
         } else if !query.ids.is_empty() {
-            let keys = query.ids.iter().map(|c| c.as_ref().to_vec()).collect();
-            query.ids.clear();
+            let keys = std::mem::take(&mut query.ids)
+                .into_iter()
+                .map(|c| c.take())
+                .collect();
             (None, None, keys)
         } else if !query.authors.is_empty() {
             let ns = self.reference_to_cf_handle(ReferenceType::Author)?;
-            let keys = query.authors.iter().map(|c| c.as_ref().to_vec()).collect();
-            query.authors.clear();
+            let keys = std::mem::take(&mut query.authors)
+                .into_iter()
+                .map(|c| c.take())
+                .collect();
             (Some(ns), None, keys)
         } else if !query.kinds.is_empty() {
             let ns = self.reference_to_cf_handle(ReferenceType::Kind)?;
-            let keys = query
-                .kinds
-                .iter()
+            let keys = std::mem::take(&mut query.kinds)
+                .into_iter()
                 .map(|kind| {
-                    let kind: u32 = (*kind).into();
+                    let kind: u32 = kind.into();
                     kind.to_be_bytes().to_vec()
                 })
                 .collect();
-            query.kinds.clear();
             (Some(ns), None, keys)
         } else {
             let cf_handle = self.reference_to_cf_handle(ReferenceType::Stream)?;

+ 5 - 0
crates/types/src/types/addr.rs

@@ -110,6 +110,11 @@ impl Addr {
         Addr::try_from_bytes(hex::decode(pk)?, hrp)
     }
 
+    /// Converts the Addr into a vector of bytes
+    pub fn take(self) -> Vec<u8> {
+        self.bytes
+    }
+
     /// Instantiates a new Addr from a vector of bytes with an optional human
     /// readable part
     pub fn try_from_bytes(bytes: Vec<u8>, hrp: Option<HumanReadablePart>) -> Result<Self, Error> {