diff --git a/src/bytes.rs b/src/bytes.rs index 297659d..31dec20 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -2019,7 +2019,7 @@ impl Inner { } // Create a new vector to store the data - let mut v = Vec::with_capacity(new_cap.next_power_of_two()); + let mut v = Vec::with_capacity(new_cap); // Copy the bytes v.extend_from_slice(self.as_ref()); diff --git a/tests/test_bytes.rs b/tests/test_bytes.rs index 5c30475..af481a2 100644 --- a/tests/test_bytes.rs +++ b/tests/test_bytes.rs @@ -302,7 +302,7 @@ fn reserve_convert() { let a = bytes.split_to(30); bytes.reserve(128); - assert_eq!(bytes.capacity(), (bytes.len() + 128).next_power_of_two()); + assert!(bytes.capacity() >= bytes.len() + 128); drop(a); } @@ -347,6 +347,42 @@ fn reserve_max_original_capacity_value() { assert_eq!(bytes.capacity(), 64 * 1024); } +#[test] +fn reserve_in_arc_unique_does_not_overallocate() { + let mut bytes = BytesMut::with_capacity(1000); + bytes.take(); + + // now bytes is Arc and refcount == 1 + + assert_eq!(1000, bytes.capacity()); + bytes.reserve(2001); + assert_eq!(2001, bytes.capacity()); +} + +#[test] +fn reserve_in_arc_unique_doubles() { + let mut bytes = BytesMut::with_capacity(1000); + bytes.take(); + + // now bytes is Arc and refcount == 1 + + assert_eq!(1000, bytes.capacity()); + bytes.reserve(1001); + assert_eq!(2000, bytes.capacity()); +} + +#[test] +fn reserve_in_arc_nonunique_does_not_overallocate() { + let mut bytes = BytesMut::with_capacity(1000); + let _copy = bytes.take(); + + // now bytes is Arc and refcount == 2 + + assert_eq!(1000, bytes.capacity()); + bytes.reserve(2001); + assert_eq!(2001, bytes.capacity()); +} + #[test] fn inline_storage() { let mut bytes = BytesMut::with_capacity(inline_cap());