Skip to content

Commit 6ccbfe1

Browse files
committed
Removed unnecessary intermediate Vec<Bytes> allocation in HTTP request body preparation
- Removed unnecessary intermediate `Vec<Bytes>` allocation in HTTP request body preparation - Directly consume `SegmentedBytes` iterator into stream using `Arc::unwrap_or_clone()` - Implemented zero-cost `BodyIterator` enum to avoid heap allocation and dynamic dispatch - Reduces memory overhead and eliminates Vec growth reallocations during request preparation
1 parent f4b4c90 commit 6ccbfe1

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/s3/client.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,24 @@ pub const MAX_PART_SIZE: u64 = 5_368_709_120; // 5 GiB
119119
/// ensuring the object does not exceed 5 TiB.
120120
pub const MAX_OBJECT_SIZE: u64 = 5_497_558_138_880; // 5 TiB
121121

122+
enum BodyIterator {
123+
Segmented(crate::s3::segmented_bytes::SegmentedBytesIntoIterator),
124+
FromVec(std::vec::IntoIter<Bytes>),
125+
Empty(std::iter::Empty<Bytes>),
126+
}
127+
128+
impl Iterator for BodyIterator {
129+
type Item = Bytes;
130+
131+
fn next(&mut self) -> Option<Self::Item> {
132+
match self {
133+
Self::Segmented(iter) => iter.next(),
134+
Self::FromVec(iter) => iter.next(),
135+
Self::Empty(iter) => iter.next(),
136+
}
137+
}
138+
}
139+
122140
/// Maximum number of parts allowed in a multipart upload.
123141
///
124142
/// Multipart uploads are limited to a total of 10,000 parts. If the object
@@ -539,14 +557,21 @@ impl MinioClient {
539557
}
540558

541559
if (*method == Method::PUT) || (*method == Method::POST) {
542-
//TODO: why-oh-why first collect into a vector and then iterate to a stream?
543-
let bytes_vec: Vec<Bytes> = match body.as_ref() {
544-
Some(v) => v.iter().collect(),
545-
None => Vec::new(),
560+
let iter = match body {
561+
Some(v) => {
562+
// Try to unwrap the Arc if we're the sole owner (zero-cost).
563+
// Otherwise, collect into a Vec to avoid cloning the SegmentedBytes structure.
564+
match Arc::try_unwrap(v) {
565+
Ok(segmented) => BodyIterator::Segmented(segmented.into_iter()),
566+
Err(arc) => {
567+
let vec: Vec<Bytes> = arc.iter().collect();
568+
BodyIterator::FromVec(vec.into_iter())
569+
}
570+
}
571+
}
572+
None => BodyIterator::Empty(std::iter::empty()),
546573
};
547-
let stream = futures_util::stream::iter(
548-
bytes_vec.into_iter().map(|b| -> Result<_, Error> { Ok(b) }),
549-
);
574+
let stream = futures_util::stream::iter(iter.map(|b| -> Result<_, Error> { Ok(b) }));
550575
req = req.body(Body::wrap_stream(stream));
551576
}
552577

0 commit comments

Comments
 (0)