Skip to content

Commit

Permalink
perf(encoding): use estimate size to reserve memory in row encoding (r…
Browse files Browse the repository at this point in the history
…isingwavelabs#8909)

Co-authored-by: stonepage <40830455+st1page@users.noreply.github.com>
  • Loading branch information
Honeta and st1page committed Mar 31, 2023
1 parent 1b05700 commit 010bec6
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/common/src/row/mod.rs
Expand Up @@ -78,15 +78,23 @@ pub trait Row: Sized + std::fmt::Debug + PartialEq + Eq {
/// Serializes the row with value encoding and returns the bytes.
#[inline]
fn value_serialize(&self) -> Vec<u8> {
let mut buf = Vec::with_capacity(self.len()); // each datum is at least 1 byte
let estimate_size = self
.iter()
.map(value_encoding::estimate_serialize_datum_size)
.sum();
let mut buf = Vec::with_capacity(estimate_size);
self.value_serialize_into(&mut buf);
buf
}

/// Serializes the row with value encoding and returns the bytes.
#[inline]
fn value_serialize_bytes(&self) -> Bytes {
let mut buf = BytesMut::with_capacity(self.len()); // each datum is at least 1 byte
let estimate_size = self
.iter()
.map(value_encoding::estimate_serialize_datum_size)
.sum();
let mut buf = BytesMut::with_capacity(estimate_size);
self.value_serialize_into(&mut buf);
buf.freeze()
}
Expand Down

0 comments on commit 010bec6

Please sign in to comment.