Skip to content

Commit

Permalink
perf(serde_v8): preallocate vector when serializing arrays (denoland#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronO committed Apr 4, 2021
1 parent e33e46e commit 5f2a83f
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions serde_v8/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,12 @@ pub struct ArraySerializer<'a, 'b, 'c> {
}

impl<'a, 'b, 'c> ArraySerializer<'a, 'b, 'c> {
pub fn new(scope: ScopePtr<'a, 'b, 'c>) -> Self {
// let serializer = Serializer::new(scope);
Self {
scope,
// serializer,
pending: vec![],
}
pub fn new(scope: ScopePtr<'a, 'b, 'c>, len: Option<usize>) -> Self {
let pending = match len {
Some(len) => Vec::with_capacity(len),
None => vec![],
};
Self { scope, pending }
}
}

Expand Down Expand Up @@ -421,8 +420,8 @@ impl<'a, 'b, 'c> ser::Serializer for Serializer<'a, 'b, 'c> {
}

/// Serialises any Rust iterable into a JS Array
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
Ok(ArraySerializer::new(self.scope))
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
Ok(ArraySerializer::new(self.scope, len))
}

fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
Expand Down

0 comments on commit 5f2a83f

Please sign in to comment.