From 2987cbdd9104c7350f2c26b10673b9af180264f8 Mon Sep 17 00:00:00 2001 From: Yotam Ofek Date: Tue, 20 Jun 2023 12:10:57 +0300 Subject: [PATCH] Optimize `AggregatedBytes::to_vec` (#2786) ## Motivation and Context Avoid intermediate vec allocations in `AggregatedBytes::to_vec`. ## Description Calling `slice::to_vec` on every segment is wasteful, the segments can be just flattened into an iterator over `u8`s and collected directly. Also makes the code a bit simpler. ## Testing ## Checklist - [x] I have updated `CHANGELOG.next.toml` if I made changes to the smithy-rs codegen or runtime crates - [ ] I have updated `CHANGELOG.next.toml` if I made changes to the AWS SDK, generated SDK code, or SDK runtime crates ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._ --------- Co-authored-by: 82marbag <69267416+82marbag@users.noreply.github.com> --- CHANGELOG.next.toml | 6 ++++++ rust-runtime/aws-smithy-http/src/byte_stream.rs | 6 +----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.next.toml b/CHANGELOG.next.toml index e7157b365..aec1605b3 100644 --- a/CHANGELOG.next.toml +++ b/CHANGELOG.next.toml @@ -11,6 +11,12 @@ # meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "client | server | all"} # author = "rcoh" +[[smithy-rs]] +message = "Avoid intermediate vec allocations in AggregatedBytes::to_vec." +author = "yotamofek" +references = ["smithy-rs#2786"] +meta = { "breaking" = false, "tada" = false, "bug" = false } + [[smithy-rs]] message = "Fix bug in AWS JSON 1.x routers where, if a service had more than 14 operations, the router was created without the route for the 15th operation." author = "thor-bjorgvinsson" diff --git a/rust-runtime/aws-smithy-http/src/byte_stream.rs b/rust-runtime/aws-smithy-http/src/byte_stream.rs index fcd697c7b..e067018a9 100644 --- a/rust-runtime/aws-smithy-http/src/byte_stream.rs +++ b/rust-runtime/aws-smithy-http/src/byte_stream.rs @@ -484,11 +484,7 @@ impl AggregatedBytes { /// Convert this buffer into a `Vec` pub fn to_vec(self) -> Vec { - self.0 - .into_inner() - .into_iter() - .flat_map(|b| b.to_vec()) - .collect() + self.0.into_inner().into_iter().flatten().collect() } } -- GitLab