Skip to content

Commit 7ed68a3

Browse files
committed
perf(buffer): double logical growth capacity
Signed-off-by: Nicholas Gates <nick@nickgates.com>
1 parent 7fed7eb commit 7ed68a3

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

vortex-buffer/src/buffer_mut.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,13 @@ impl<T> BufferMut<T> {
461461
.checked_mul(size_of::<T>())
462462
.vortex_expect("buffer capacity overflow");
463463
let physical_alignment = max(self.alignment, self.physical_alignment);
464-
let required_size = required_size
464+
let current_size = self.allocation.size() - self.offset;
465+
let logical_size = required_size
466+
.max(current_size.saturating_mul(2))
467+
.max(physical_alignment.as_usize());
468+
let allocation_size = logical_size
465469
.checked_add(physical_alignment.as_usize())
466470
.vortex_expect("buffer capacity overflow");
467-
let current_size = self.allocation.size() - self.offset;
468-
let allocation_size = required_size.max(current_size.saturating_mul(2));
469471
let allocation_alignment = if self.allocation.size() == 0 {
470472
1
471473
} else {
@@ -1084,6 +1086,19 @@ mod test {
10841086
assert_eq!(buffer.as_slice(), (0..10_000).collect::<Vec<_>>());
10851087
}
10861088

1089+
#[test]
1090+
fn growth_seeds_and_doubles_logical_capacity() {
1091+
let alignment = Alignment::new(64);
1092+
let mut buffer = BufferMut::<u8>::empty_aligned(alignment);
1093+
1094+
buffer.push(0);
1095+
let capacity = buffer.capacity();
1096+
assert!(capacity >= alignment.as_usize());
1097+
1098+
buffer.reserve(capacity);
1099+
assert!(buffer.capacity() >= capacity * 2);
1100+
}
1101+
10871102
#[test]
10881103
fn from_iter() {
10891104
let buf = BufferMut::from_iter([0, 10, 20, 30]);

0 commit comments

Comments
 (0)