This is an automated email from the ASF dual-hosted git repository.

wangweipeng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fury.git


The following commit(s) were added to refs/heads/main by this push:
     new 0af20842 fix(rust): use iter to avoid reallocate (#1821)
0af20842 is described below

commit 0af2084262881c84a529e2120f5106cf86ca8ff1
Author: Jiacai Liu <[email protected]>
AuthorDate: Thu Aug 29 16:07:54 2024 +0800

    fix(rust): use iter to avoid reallocate (#1821)
    
    ## What does this PR do?
    
    Use iterator + collect to avoid re-allocate vec/set/map.
    
    ## Related issues
    
    ## Does this PR introduce any user-facing change?
    
    - [ ] Does this PR introduce any public API change?
    - [ ] Does this PR introduce any binary protocol compatibility change?
    
    ## Benchmark
---
 rust/fury-core/src/resolver/meta_resolver.rs |  2 +-
 rust/fury-core/src/serializer/list.rs        | 11 ++++-------
 rust/fury-core/src/serializer/map.rs         | 17 +++++++----------
 rust/fury-core/src/serializer/mod.rs         |  4 ++--
 rust/fury-core/src/serializer/set.rs         |  9 +++------
 5 files changed, 17 insertions(+), 26 deletions(-)

diff --git a/rust/fury-core/src/resolver/meta_resolver.rs 
b/rust/fury-core/src/resolver/meta_resolver.rs
index b9a442c5..1e177423 100644
--- a/rust/fury-core/src/resolver/meta_resolver.rs
+++ b/rust/fury-core/src/resolver/meta_resolver.rs
@@ -69,7 +69,7 @@ impl<'a> MetaWriterResolver<'a> {
 
     pub fn to_bytes(&self, writer: &mut Writer) -> Result<(), Error> {
         writer.var_int32(self.type_defs.len() as i32);
-        for item in self.type_defs.iter() {
+        for item in &self.type_defs {
             writer.bytes(item)
         }
         Ok(())
diff --git a/rust/fury-core/src/serializer/list.rs 
b/rust/fury-core/src/serializer/list.rs
index 0b147f43..46e76ff0 100644
--- a/rust/fury-core/src/serializer/list.rs
+++ b/rust/fury-core/src/serializer/list.rs
@@ -38,14 +38,11 @@ where
     }
 
     fn read(context: &mut ReadContext) -> Result<Self, Error> {
-        // length
+        // vec length
         let len = context.reader.var_int32();
-        // value
-        let mut result = Vec::new();
-        for _ in 0..len {
-            result.push(T::deserialize(context)?);
-        }
-        Ok(result)
+        (0..len)
+            .map(|_| T::deserialize(context))
+            .collect::<Result<Vec<_>, Error>>()
     }
 
     fn reserved_space() -> usize {
diff --git a/rust/fury-core/src/serializer/map.rs 
b/rust/fury-core/src/serializer/map.rs
index e79740f4..837e3ab6 100644
--- a/rust/fury-core/src/serializer/map.rs
+++ b/rust/fury-core/src/serializer/map.rs
@@ -42,17 +42,14 @@ impl<T1: Serializer + Eq + std::hash::Hash, T2: Serializer> 
Serializer for HashM
     }
 
     fn read(context: &mut ReadContext) -> Result<Self, Error> {
-        // length
+        // map length
         let len = context.reader.var_int32();
-        let mut result = HashMap::new();
-        // key-value
-        for _ in 0..len {
-            result.insert(
-                <T1 as Serializer>::deserialize(context)?,
-                <T2 as Serializer>::deserialize(context)?,
-            );
-        }
-        Ok(result)
+        (0..len)
+            .map(|_| {
+                <T1 as Serializer>::deserialize(context)
+                    .and_then(|k| <T2 as 
Serializer>::deserialize(context).map(|v| (k, v)))
+            })
+            .collect::<Result<HashMap<_, _>, Error>>()
     }
 
     fn reserved_space() -> usize {
diff --git a/rust/fury-core/src/serializer/mod.rs 
b/rust/fury-core/src/serializer/mod.rs
index 1803bdfb..20f02093 100644
--- a/rust/fury-core/src/serializer/mod.rs
+++ b/rust/fury-core/src/serializer/mod.rs
@@ -67,8 +67,8 @@ pub trait Serializer
 where
     Self: Sized,
 {
-    /// The fixed memory size of the Type.
-    /// Avoid the memory check, which would hurt performance.
+    /// The possible max memory size of the type.
+    /// Used to reserve the buffer space to avoid reallocation, which may hurt 
performance.
     fn reserved_space() -> usize;
 
     /// Write the data into the buffer.
diff --git a/rust/fury-core/src/serializer/set.rs 
b/rust/fury-core/src/serializer/set.rs
index 4b8b84db..fd4eb514 100644
--- a/rust/fury-core/src/serializer/set.rs
+++ b/rust/fury-core/src/serializer/set.rs
@@ -42,12 +42,9 @@ impl<T: Serializer + Eq + std::hash::Hash> Serializer for 
HashSet<T> {
     fn read(context: &mut ReadContext) -> Result<Self, Error> {
         // length
         let len = context.reader.var_int32();
-        let mut result = HashSet::new();
-        // key-value
-        for _ in 0..len {
-            result.insert(<T as Serializer>::deserialize(context)?);
-        }
-        Ok(result)
+        (0..len)
+            .map(|_| T::deserialize(context))
+            .collect::<Result<HashSet<_>, Error>>()
     }
 
     fn reserved_space() -> usize {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to