Skip to content

Commit

Permalink
Calculate original capacity only if necessary
Browse files Browse the repository at this point in the history
We don't need the original capacity if the shared data is unique, so
let's not calculate it until after that check.
  • Loading branch information
braddunbar committed Jan 28, 2024
1 parent e24587d commit 6f40d29
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/bytes_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6f40d29

Please sign in to comment.