Skip to content

Commit 4964d9f

Browse files
Use ptr increment instead of add (#2337)
This seems to make a large different to the throughput of extend
1 parent a29972f commit 4964d9f

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ include = [
4343
"README.md",
4444
]
4545
edition = "2021"
46-
rust-version = "1.82"
46+
rust-version = "1.84"
4747
readme = "README.md"
4848
categories = ["database-implementations", "data-structures", "compression"]
4949

vortex-buffer/src/buffer_mut.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -423,19 +423,25 @@ impl<T> Extend<T> for BufferMut<T> {
423423

424424
let remaining = self.capacity() - self.len();
425425

426-
let dst: *mut T = self.bytes.spare_capacity_mut().as_mut_ptr().cast();
427-
let mut consumed = 0;
428-
while consumed < remaining {
426+
let begin: *const T = self.bytes.spare_capacity_mut().as_mut_ptr().cast();
427+
let mut dst: *mut T = begin.cast_mut();
428+
let buffer_end: *const T = unsafe { dst.add(remaining) };
429+
while dst.addr() < buffer_end.addr() {
429430
if let Some(item) = iterator.next() {
430-
// SAFETY: We know we have enough capacity to write the item.
431-
unsafe { dst.add(consumed).write(item) };
432-
consumed += 1;
431+
unsafe {
432+
// SAFETY: We know we have enough capacity to write the item.
433+
dst.write(item);
434+
// Note. we used to have dst.add(iteration).write(item), here.
435+
// however this was much slower than just incrementing dst.
436+
dst = dst.add(1);
437+
}
433438
} else {
434439
break;
435440
}
436441
}
437442

438-
self.length += consumed;
443+
// TODO(joe): replace with ptr_sub when stable
444+
self.length += unsafe { dst.byte_offset_from(begin) as usize / size_of::<T>() };
439445
unsafe { self.bytes.set_len(self.length * size_of::<T>()) };
440446

441447
iterator.for_each(|item| self.push(item));

0 commit comments

Comments
 (0)