Skip to content

Commit 98dd977

Browse files
authored
feat(buffer): add allocator-backed storage (#9668)
## Summary Add allocator-backed storage to `vortex-buffer`. Design: vortex-data/rfcs#65 ## Changes - Use `allocator_api2` for owned buffers. - Keep external buffers zero-copy. - Preserve runtime alignment and `bytes`/Arrow interop. - Add allocator-aware constructors and tests. ## API Changes Owned buffers no longer use `bytes::Bytes` as storage. The `bytes` crate remains for interop. `Buffer::into_inner` is now `Buffer::into_bytes`. ## Allocation benchmarks Local medians on an Apple M5 Max with Rust 1.98.0: ```console cargo bench -p vortex-buffer --bench allocation -- --max-time 2 --sample-count 500 ``` | Operation | Size | Vortex | Vortex custom allocator | Bytes | Arrow | | --- | ---: | ---: | ---: | ---: | ---: | | allocate + drop | 1 KiB | 14.7 ns | 17.0 ns | 19.1 ns | 21.7 ns | | allocate + drop | 16 KiB | 18.5 ns | 20.6 ns | 17.8 ns | 20.6 ns | | allocate + drop | 64 KiB | 77.9 ns | 81.8 ns | 76.6 ns | 83.8 ns | | allocate + freeze + drop | 64 B | 33.6 ns | 35.6 ns | 36.2 ns | 37.2 ns | | allocate + freeze + drop | 256 B | 34.3 ns | 36.6 ns | 33.3 ns | 35.6 ns | | take ownership of `Vec` + drop | 64 B | 22.9 ns | N/A | 7.7 ns | 20.6 ns | | take ownership of `Vec` + drop | 1 KiB | 29.1 ns | N/A | 17.0 ns | 29.4 ns | | take ownership of `Vec` + drop | 64 KiB | 56.1 ns | N/A | 38.8 ns | 46.0 ns | The custom allocator's `Arc` adds about 2–4 ns per allocation lifecycle. Normal mutable allocation is comparable across the three implementations. Vortex and Arrow eagerly allocate shared backing metadata when taking ownership of a `Vec`; Bytes keeps a promotable Vec representation and is about 12–15 ns faster for small buffers. The custom allocator column does not apply to `Vec` ownership. Rust's `Vec` uses the global allocator, so zero-copy adoption must retain that allocator for deallocation. Moving its contents to a custom allocator would require a copy and would measure a different operation. Vortex normally prefers 256-byte alignment. The checked-in suite also measures Vortex with only the requested alignment to separate alignment padding from ownership overhead. --------- Signed-off-by: Nicholas Gates <[email protected]>
1 parent 6db45e9 commit 98dd977

21 files changed

Lines changed: 1481 additions & 488 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ rust-version = "1.95"
9797
version = "0.1.0"
9898

9999
[workspace.dependencies]
100+
allocator-api2 = "0.2.21"
100101
alp = "0.0.2"
101102
anyhow = "1.0.100"
102103
arbitrary = "1.3.2"

‎encodings/pco/src/array.rs‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ use vortex_array::vtable::child_to_validity;
4747
use vortex_array::vtable::validity_to_child;
4848
use vortex_buffer::BufferMut;
4949
use vortex_buffer::ByteBuffer;
50-
use vortex_buffer::ByteBufferMut;
5150
use vortex_error::VortexError;
5251
use vortex_error::VortexResult;
5352
use vortex_error::vortex_bail;
@@ -568,17 +567,17 @@ impl PcoData {
568567
}
569568
);
570569

571-
let mut chunk_meta_buffer = ByteBufferMut::with_capacity(cc.meta_size_hint());
570+
let mut chunk_meta_buffer = Vec::with_capacity(cc.meta_size_hint());
572571
cc.write_meta(&mut chunk_meta_buffer)
573572
.map_err(vortex_err_from_pco)?;
574-
chunk_meta_buffers.push(chunk_meta_buffer.freeze());
573+
chunk_meta_buffers.push(ByteBuffer::from(chunk_meta_buffer));
575574

576575
let mut page_infos = vec![];
577576
for (page_idx, page_n_values) in cc.n_per_page().into_iter().enumerate() {
578-
let mut page = ByteBufferMut::with_capacity(cc.page_size_hint(page_idx));
577+
let mut page = Vec::with_capacity(cc.page_size_hint(page_idx));
579578
cc.write_page(page_idx, &mut page)
580579
.map_err(vortex_err_from_pco)?;
581-
page_buffers.push(page.freeze());
580+
page_buffers.push(ByteBuffer::from(page));
582581
page_infos.push(PcoPageInfo {
583582
n_values: u32::try_from(page_n_values)?,
584583
});

‎encodings/sparse/src/lib.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ use vortex_array::validity::Validity;
4848
use vortex_array::vtable::VTable;
4949
use vortex_array::vtable::ValidityVTable;
5050
use vortex_buffer::Buffer;
51-
use vortex_buffer::ByteBufferMut;
5251
use vortex_error::VortexExpect as _;
5352
use vortex_error::VortexResult;
5453
use vortex_error::vortex_bail;
@@ -217,7 +216,7 @@ impl VTable for Sparse {
217216
match idx {
218217
0 => {
219218
let fill_value_buffer =
220-
ScalarValue::to_proto_bytes::<ByteBufferMut>(array.fill_value.value()).freeze();
219+
ScalarValue::to_proto_bytes::<Vec<u8>>(array.fill_value.value()).into();
221220
BufferHandle::new_host(fill_value_buffer)
222221
}
223222
_ => vortex_panic!("SparseArray buffer index {idx} out of bounds"),

‎fuzz/fuzz_targets/file_io.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use vortex_array::expr::lit;
1919
use vortex_array::expr::root;
2020
use vortex_array::scalar_fn::fns::operators::Operator;
2121
use vortex_btrblocks::BtrBlocksCompressorBuilder;
22-
use vortex_buffer::ByteBufferMut;
2322
use vortex_error::VortexExpect;
2423
use vortex_error::vortex_panic;
2524
use vortex_file::OpenOptionsSessionExt;
@@ -72,7 +71,7 @@ fuzz_target!(|fuzz: FuzzFileAction| -> Corpus {
7271
),
7372
};
7473

75-
let mut full_buff = ByteBufferMut::empty();
74+
let mut full_buff = Vec::new();
7675
let _footer = write_options
7776
.blocking(&*RUNTIME)
7877
.write(&mut full_buff, array_data.to_array_iterator())

‎vortex-array/src/arrays/constant/vtable/mod.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::hash::Hash;
66
use std::hash::Hasher;
77

88
use itertools::Itertools;
9-
use vortex_buffer::ByteBufferMut;
109
use vortex_error::VortexExpect;
1110
use vortex_error::VortexResult;
1211
use vortex_error::vortex_ensure;
@@ -107,7 +106,7 @@ impl VTable for Constant {
107106
fn buffer(array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
108107
match idx {
109108
0 => BufferHandle::new_host(
110-
ScalarValue::to_proto_bytes::<ByteBufferMut>(array.scalar.value()).freeze(),
109+
ScalarValue::to_proto_bytes::<Vec<u8>>(array.scalar.value()).into(),
111110
),
112111
_ => vortex_panic!("ConstantArray buffer index {idx} out of bounds"),
113112
}

‎vortex-array/src/arrays/struct_/vtable/operations.rs‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,25 @@ use crate::array::OperationsVTable;
99
use crate::arrays::Struct;
1010
use crate::arrays::struct_::StructArrayExt;
1111
use crate::scalar::Scalar;
12+
use crate::scalar::ScalarValue;
1213

1314
impl OperationsVTable<Struct> for Struct {
1415
fn scalar_at(
1516
array: ArrayView<'_, Struct>,
1617
index: usize,
1718
ctx: &mut ExecutionCtx,
1819
) -> VortexResult<Scalar> {
19-
let field_scalars: VortexResult<Vec<Scalar>> = array
20+
let field_values = array
2021
.iter_unmasked_fields()
21-
.map(|field| field.execute_scalar(index, ctx))
22-
.collect();
22+
.map(|field| field.execute_scalar(index, ctx).map(Scalar::into_value))
23+
.collect::<VortexResult<Vec<_>>>()?;
2324
// SAFETY: The vtable guarantees index is in-bounds and non-null before this is called.
24-
// Each field's scalar_at returns a scalar with the field's own dtype.
25-
Ok(unsafe { Scalar::struct_unchecked(array.dtype().clone(), field_scalars?) })
25+
// Each field's scalar_at returns a value with the field's own dtype.
26+
Ok(unsafe {
27+
Scalar::new_unchecked(
28+
array.dtype().clone(),
29+
Some(ScalarValue::Tuple(field_values)),
30+
)
31+
})
2632
}
2733
}

‎vortex-array/src/builders/varbinview.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,8 @@ impl ArrayBuilder for VarBinViewBuilder {
800800
}
801801

802802
impl VarBinViewBuilder {
803-
#[inline]
803+
#[allow(clippy::inline_always)]
804+
#[inline(always)]
804805
fn push_view(
805806
&mut self,
806807
view: BinaryView,

‎vortex-array/src/memory.rs‎

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::fmt::Debug;
88
use std::mem::size_of;
99
use std::sync::Arc;
1010

11-
use bytes::Bytes;
1211
use vortex_buffer::Alignment;
1312
use vortex_buffer::Buffer;
1413
use vortex_buffer::ByteBuffer;
@@ -244,26 +243,14 @@ impl HostAllocator for DefaultHostAllocator {
244243
// SAFETY: We fully initialize this slice before freezing it.
245244
unsafe { buffer.set_len(len) };
246245
Ok(WritableHostBuffer::new(Box::new(
247-
DefaultWritableHostBuffer { buffer, alignment },
246+
DefaultWritableHostBuffer { buffer },
248247
)))
249248
}
250249
}
251250

252251
#[derive(Debug)]
253252
struct DefaultWritableHostBuffer {
254253
buffer: ByteBufferMut,
255-
alignment: Alignment,
256-
}
257-
258-
#[derive(Debug)]
259-
struct HostBufferOwner {
260-
buffer: ByteBufferMut,
261-
}
262-
263-
impl AsRef<[u8]> for HostBufferOwner {
264-
fn as_ref(&self) -> &[u8] {
265-
self.buffer.as_slice()
266-
}
267254
}
268255

269256
impl HostBufferMut for DefaultWritableHostBuffer {
@@ -272,17 +259,15 @@ impl HostBufferMut for DefaultWritableHostBuffer {
272259
}
273260

274261
fn alignment(&self) -> Alignment {
275-
self.alignment
262+
self.buffer.alignment()
276263
}
277264

278265
fn as_mut_slice(&mut self) -> &mut [u8] {
279266
self.buffer.as_mut_slice()
280267
}
281268

282269
fn freeze(self: Box<Self>) -> ByteBuffer {
283-
let Self { buffer, alignment } = *self;
284-
let bytes = Bytes::from_owner(HostBufferOwner { buffer });
285-
ByteBuffer::from_bytes_aligned(bytes, alignment)
270+
self.buffer.freeze()
286271
}
287272
}
288273

‎vortex-arrow/src/executor/byte_view.rs‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use std::sync::Arc;
66
use arrow_array::ArrayRef as ArrowArrayRef;
77
use arrow_array::GenericByteViewArray;
88
use arrow_array::types::ByteViewType;
9-
use arrow_buffer::ScalarBuffer;
109
use vortex_array::ArrayRef;
1110
use vortex_array::ExecutionCtx;
1211
use vortex_array::arrays::VarBinViewArray;
1312
use vortex_array::builtins::ArrayBuiltins;
1413
use vortex_array::dtype::Nullability;
14+
use vortex_buffer::Buffer;
1515
use vortex_error::VortexResult;
1616

1717
use crate::dtype::from_arrow_data_type;
@@ -22,8 +22,8 @@ pub fn canonical_varbinview_to_arrow<T: ByteViewType>(
2222
array: &VarBinViewArray,
2323
ctx: &mut ExecutionCtx,
2424
) -> VortexResult<ArrowArrayRef> {
25-
let views =
26-
ScalarBuffer::<u128>::from(array.views_handle().as_host().clone().into_arrow_buffer());
25+
let views = Buffer::<u128>::from_byte_buffer(array.views_handle().as_host().clone())
26+
.into_arrow_scalar_buffer();
2727
let buffers: Vec<_> = array
2828
.data_buffers()
2929
.iter()
@@ -64,3 +64,23 @@ pub(super) fn to_arrow_byte_view<T: ByteViewType>(
6464
let varbinview = array.execute::<VarBinViewArray>(ctx)?;
6565
execute_varbinview_to_arrow::<T>(&varbinview, ctx)
6666
}
67+
68+
#[cfg(test)]
69+
mod tests {
70+
use arrow_array::types::StringViewType;
71+
use vortex_array::VortexSessionExecute;
72+
use vortex_array::array_session;
73+
74+
use super::*;
75+
76+
#[test]
77+
fn empty_views_are_aligned() -> VortexResult<()> {
78+
let array = VarBinViewArray::from_iter_str(std::iter::empty::<&str>());
79+
let mut ctx = array_session().create_execution_ctx();
80+
81+
let arrow = canonical_varbinview_to_arrow::<StringViewType>(&array, &mut ctx)?;
82+
83+
assert!(arrow.is_empty());
84+
Ok(())
85+
}
86+
}

0 commit comments

Comments
 (0)