Skip to content

Commit

Permalink
fix(rust): use iter to avoid reallocate (#1821)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
jiacai2050 authored Aug 29, 2024
1 parent 19d62b3 commit 0af2084
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 26 deletions.
2 changes: 1 addition & 1 deletion rust/fury-core/src/resolver/meta_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
11 changes: 4 additions & 7 deletions rust/fury-core/src/serializer/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 7 additions & 10 deletions rust/fury-core/src/serializer/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions rust/fury-core/src/serializer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 3 additions & 6 deletions rust/fury-core/src/serializer/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0af2084

Please sign in to comment.