Skip to content

Commit

Permalink
Optimize Vec allocations in map deserialization.
Browse files Browse the repository at this point in the history
Before:

utils::bench_deserialize_map       ... bench:   171.39 ns/iter (+/- 3.53)
utils::bench_deserialize_map_bytes ... bench:   148.27 ns/iter (+/- 3.19)

After:

utils::bench_deserialize_map       ... bench:   168.34 ns/iter (+/- 3.19)
utils::bench_deserialize_map_bytes ... bench:   145.40 ns/iter (+/- 1.92)

Signed-off-by: Piotr Sikora <[email protected]>
  • Loading branch information
PiotrSikora committed Oct 28, 2024
1 parent 6d88ed5 commit 7455773
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/hostcalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ pub fn get_map(map_type: MapType) -> Result<Vec<(String, String)>, Status> {
match proxy_get_header_map_pairs(map_type, &mut return_data, &mut return_size) {
Status::Ok => {
if !return_data.is_null() {
let serialized_map = Vec::from_raw_parts(return_data, return_size, return_size);
Ok(utils::deserialize_map(&serialized_map))
let serialized_map = std::slice::from_raw_parts(return_data, return_size);
Ok(utils::deserialize_map(serialized_map))
} else {
Ok(Vec::new())
}
Expand All @@ -170,8 +170,8 @@ pub fn get_map_bytes(map_type: MapType) -> Result<Vec<(String, Bytes)>, Status>
match proxy_get_header_map_pairs(map_type, &mut return_data, &mut return_size) {
Status::Ok => {
if !return_data.is_null() {
let serialized_map = Vec::from_raw_parts(return_data, return_size, return_size);
Ok(utils::deserialize_map_bytes(&serialized_map))
let serialized_map = std::slice::from_raw_parts(return_data, return_size);
Ok(utils::deserialize_map_bytes(serialized_map))
} else {
Ok(Vec::new())
}
Expand Down Expand Up @@ -1200,11 +1200,11 @@ mod utils {
}

pub(super) fn deserialize_map(bytes: &[u8]) -> Vec<(String, String)> {
let mut map = Vec::new();
if bytes.is_empty() {
return map;
return Vec::new();
}
let size = u32::from_le_bytes(<[u8; 4]>::try_from(&bytes[0..4]).unwrap()) as usize;
let mut map = Vec::with_capacity(size);
let mut p = 4 + size * 8;
for n in 0..size {
let s = 4 + n * 8;
Expand All @@ -1224,11 +1224,11 @@ mod utils {
}

pub(super) fn deserialize_map_bytes(bytes: &[u8]) -> Vec<(String, Bytes)> {
let mut map = Vec::new();
if bytes.is_empty() {
return map;
return Vec::new();
}
let size = u32::from_le_bytes(<[u8; 4]>::try_from(&bytes[0..4]).unwrap()) as usize;
let mut map = Vec::with_capacity(size);
let mut p = 4 + size * 8;
for n in 0..size {
let s = 4 + n * 8;
Expand Down

0 comments on commit 7455773

Please sign in to comment.