diff options
author | 2024-02-06 13:46:49 -0500 | |
---|---|---|
committer | 2024-02-06 10:46:49 -0800 | |
commit | c6972d61328be113ec8e80c207815a4b84fe616c (patch) | |
tree | 0e79797333620ecf4cb85ab39f9d2823f0690d66 /src/bytes_mut.rs | |
parent | 47e83056f28e15e4ca68056a0136f3920b753783 (diff) | |
download | bytes-c6972d61328be113ec8e80c207815a4b84fe616c.tar.gz bytes-c6972d61328be113ec8e80c207815a4b84fe616c.tar.zst bytes-c6972d61328be113ec8e80c207815a4b84fe616c.zip |
Calculate original capacity only if necessary (#666)
We don't need the original capacity if the shared data is unique, so
let's not calculate it until after that check.
Diffstat (limited to '')
-rw-r--r-- | src/bytes_mut.rs | 9 |
1 files changed, 3 insertions, 6 deletions
diff --git a/src/bytes_mut.rs b/src/bytes_mut.rs index d1c1411..619defc 100644 --- a/src/bytes_mut.rs +++ b/src/bytes_mut.rs @@ -652,13 +652,7 @@ impl BytesMut { // Compute the new capacity let mut new_cap = len.checked_add(additional).expect("overflow"); - let original_capacity; - let original_capacity_repr; - unsafe { - original_capacity_repr = (*shared).original_capacity_repr; - original_capacity = original_capacity_from_repr(original_capacity_repr); - // First, try to reclaim the buffer. This is possible if the current // handle is the only outstanding handle pointing to the buffer. if (*shared).is_unique() { @@ -729,6 +723,9 @@ impl BytesMut { } } + let original_capacity_repr = unsafe { (*shared).original_capacity_repr }; + let original_capacity = original_capacity_from_repr(original_capacity_repr); + new_cap = cmp::max(new_cap, original_capacity); // Create a new vector to store the data |