From 764fcead559a20a247787a1ac62bcc918478fbfe Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Sun, 1 Dec 2024 21:38:31 -0500 Subject: [PATCH 1/2] Optimize Vec allocations in map deserialization. (#285) 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 --- src/hostcalls.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/hostcalls.rs b/src/hostcalls.rs index 15687888..f8252c56 100644 --- a/src/hostcalls.rs +++ b/src/hostcalls.rs @@ -152,8 +152,8 @@ pub fn get_map(map_type: MapType) -> Result, 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()) } @@ -170,8 +170,8 @@ pub fn get_map_bytes(map_type: MapType) -> Result, 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()) } @@ -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; @@ -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; From 597634dbba0f3fa201e39abc025f2dcc31f97fae Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Sun, 1 Dec 2024 21:39:15 -0500 Subject: [PATCH 2/2] Update MSRV to Rust v1.65.0. (#291) Hashbrown updated its MSRV in a patch release (v0.15.1), so we have to follow. Signed-off-by: Piotr Sikora --- .github/workflows/rust.yml | 6 +++--- Cargo.toml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index da9683f9..76bd7980 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -125,14 +125,14 @@ jobs: run: | curl -OL https://static.rust-lang.org/rustup/rustup-init.sh chmod +x ./rustup-init.sh - ./rustup-init.sh -y --default-toolchain 1.64.0 + ./rustup-init.sh -y --default-toolchain 1.65.0 rm rustup-init.sh echo "$HOME/.cargo/bin" >> $GITHUB_PATH - name: Update Rust run: | - rustup toolchain install 1.64.0 --component clippy --component rustfmt - rustup default 1.64.0 + rustup toolchain install 1.65.0 --component clippy --component rustfmt + rustup default 1.65.0 rustup target add wasm32-unknown-unknown rustup target add wasm32-wasi diff --git a/Cargo.toml b/Cargo.toml index 24193eb8..7b0469bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "proxy-wasm" version = "0.2.3-dev" authors = ["Piotr Sikora "] -rust-version = "1.64" +rust-version = "1.65" description = "WebAssembly for Proxies" readme = "README.md" license = "Apache-2.0"