Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rust): use iter to avoid reallocate #1821

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading