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

Optimize Vec allocations in map deserialization. #285

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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
Loading