mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-13 00:00:32 +02:00
Tweak growth algorithm in BytesMut::reserve
This commit is contained in:
+2
-2
@@ -1695,12 +1695,12 @@ impl Inner {
|
|||||||
// asking for more than the initial buffer capacity. Allocate more
|
// asking for more than the initial buffer capacity. Allocate more
|
||||||
// than requested if `new_cap` is not much bigger than the current
|
// than requested if `new_cap` is not much bigger than the current
|
||||||
// capacity.
|
// capacity.
|
||||||
new_cap = cmp::max(len << 1, new_cap);
|
new_cap = cmp::max(v.capacity() << 1, new_cap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new vector to store the data
|
// Create a new vector to store the data
|
||||||
let mut v = Vec::with_capacity(new_cap);
|
let mut v = Vec::with_capacity(new_cap.next_power_of_two());
|
||||||
|
|
||||||
// Copy the bytes
|
// Copy the bytes
|
||||||
v.extend_from_slice(self.as_ref());
|
v.extend_from_slice(self.as_ref());
|
||||||
|
|||||||
+12
-2
@@ -207,7 +207,7 @@ fn fns_defined_for_bytes_mut() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn reserve() {
|
fn reserve_convert() {
|
||||||
// Inline -> Vec
|
// Inline -> Vec
|
||||||
let mut bytes = BytesMut::with_capacity(8);
|
let mut bytes = BytesMut::with_capacity(8);
|
||||||
bytes.put("hello");
|
bytes.put("hello");
|
||||||
@@ -236,11 +236,21 @@ fn reserve() {
|
|||||||
let a = bytes.drain_to(30);
|
let a = bytes.drain_to(30);
|
||||||
|
|
||||||
bytes.reserve(128);
|
bytes.reserve(128);
|
||||||
assert_eq!(bytes.capacity(), bytes.len() + 128);
|
assert_eq!(bytes.capacity(), (bytes.len() + 128).next_power_of_two());
|
||||||
|
|
||||||
drop(a);
|
drop(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn reserve_growth() {
|
||||||
|
let mut bytes = BytesMut::with_capacity(64);
|
||||||
|
bytes.put("hello world");
|
||||||
|
let _ = bytes.drain();
|
||||||
|
|
||||||
|
bytes.reserve(65);
|
||||||
|
assert_eq!(bytes.capacity(), 128);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn inline_storage() {
|
fn inline_storage() {
|
||||||
let mut bytes = BytesMut::with_capacity(inline_cap());
|
let mut bytes = BytesMut::with_capacity(inline_cap());
|
||||||
|
|||||||
Reference in New Issue
Block a user