Skip to content

Commit da01bb2

Browse files
author
Javier Atadia
committed
Revert "Optimize Vec allocations in map deserialization. (proxy-wasm#285)"
This reverts commit 764fcea.
1 parent 58c84eb commit da01bb2

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/hostcalls.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ pub fn get_map(map_type: MapType) -> Result<Vec<(String, String)>, Status> {
152152
match proxy_get_header_map_pairs(map_type, &mut return_data, &mut return_size) {
153153
Status::Ok => {
154154
if !return_data.is_null() {
155-
let serialized_map = std::slice::from_raw_parts(return_data, return_size);
156-
Ok(utils::deserialize_map(serialized_map))
155+
let serialized_map = Vec::from_raw_parts(return_data, return_size, return_size);
156+
Ok(utils::deserialize_map(&serialized_map))
157157
} else {
158158
Ok(Vec::new())
159159
}
@@ -170,8 +170,8 @@ pub fn get_map_bytes(map_type: MapType) -> Result<Vec<(String, Bytes)>, Status>
170170
match proxy_get_header_map_pairs(map_type, &mut return_data, &mut return_size) {
171171
Status::Ok => {
172172
if !return_data.is_null() {
173-
let serialized_map = std::slice::from_raw_parts(return_data, return_size);
174-
Ok(utils::deserialize_map_bytes(serialized_map))
173+
let serialized_map = Vec::from_raw_parts(return_data, return_size, return_size);
174+
Ok(utils::deserialize_map_bytes(&serialized_map))
175175
} else {
176176
Ok(Vec::new())
177177
}
@@ -1209,11 +1209,11 @@ mod utils {
12091209
}
12101210

12111211
pub(super) fn deserialize_map(bytes: &[u8]) -> Vec<(String, String)> {
1212+
let mut map = Vec::new();
12121213
if bytes.is_empty() {
1213-
return Vec::new();
1214+
return map;
12141215
}
12151216
let size = u32::from_le_bytes(<[u8; 4]>::try_from(&bytes[0..4]).unwrap()) as usize;
1216-
let mut map = Vec::with_capacity(size);
12171217
let mut p = 4 + size * 8;
12181218
for n in 0..size {
12191219
let s = 4 + n * 8;
@@ -1233,11 +1233,11 @@ mod utils {
12331233
}
12341234

12351235
pub(super) fn deserialize_map_bytes(bytes: &[u8]) -> Vec<(String, Bytes)> {
1236+
let mut map = Vec::new();
12361237
if bytes.is_empty() {
1237-
return Vec::new();
1238+
return map;
12381239
}
12391240
let size = u32::from_le_bytes(<[u8; 4]>::try_from(&bytes[0..4]).unwrap()) as usize;
1240-
let mut map = Vec::with_capacity(size);
12411241
let mut p = 4 + size * 8;
12421242
for n in 0..size {
12431243
let s = 4 + n * 8;

0 commit comments

Comments
 (0)