Repository navigation
Description
Is your feature request related to a problem or challenge? Please describe what you are trying to do.
While we work on micro micro optimizations, we have seen a common pattern where older parts of the arrow-rs codebase use ArrayData to create new arrays.
An ArrayData has at least one extra allocation (for the Vec that holds Buffers) as well as a bunch of dynamic function calls. While this overhead is small individually, it is paid for every array so in aggregate it can be substantial
It also typically requires an unsafe call which is unnecessary as the new APIs can be checked by the compiler.
Quoting @tustvold
My 2 cents is it would be better to move the codepaths relying on ArrayData over to using the typed arrays directly, this should not only cut down on allocations but unnecessary validation and dispatch overheads.
Describe the solution you'd like
Change relying on ArrayData over to creating the typed arrays directly, this should not only cut down on allocations but unnecessary validation and dispatch overheads.
Describe alternatives you've considered
Here are some example PRs
- [Parquet] perf: Create
PrimitiveArrays directly rather than viaArrayData#9122 - [Parquet] perf: Create StructArrays directly rather than via
ArrayData(1% improvement) #9120
the old, less efficient pattern looks like this (note the vec![buffer] to create a buffer).
let data = unsafe {
ArrayData::new_unchecked(T::DATA_TYPE, len, None, Some(null), 0, vec![buffer], vec![])
};
PrimitiveArray::from(data)or
let array_data = ArrayDataBuilder::new(arrow_data_type)
.len(self.record_reader.num_values())
.add_buffer(record_data)
.null_bit_buffer(self.record_reader.consume_bitmap_buffer());
let array_data = unsafe { array_data.build_unchecked() };The new pattern looks like this (note no unsafe or allocations)
// Create nulls directly (note the `filter` to avoid nulls)
let nulls =
Some(NullBuffer::new(BooleanBuffer::new(null, 0, len))).filter(|n| n.null_count() > 0);
// Create Primitive Array directly
PrimitiveArray::new(ScalarBuffer::from(buffer), nulls)** Note the only tricky thing I have seen is that ArrayDataBuilder automatically checks / drops NullBuffers that have no nulls. When updating the code we need to follow a similar pattern
Additional context
Activity
Remove some `unsafe` and allocations when creating PrimitiveArrays fr…
Replace `ArrayData` with direct Array construction (#9338)
[Json] Replace `ArrayData` with typed Array construction in json-read…
[Json] Remove arrow-data dependency from arrow-json (#9812)
[arrow-select] Replace `ArrayData` with direct `Array` construction i…
[Json] Replace `ArrayData` with typed Array construction in json-read…
[Json] Remove arrow-data dependency from arrow-json (apache#9812)
[arrow-select] Replace `ArrayData` with direct `Array` construction i…
[arrow-select] perf: Replace `ArrayData` with direct `Array` construc…
Replace `ArrayData` with direct `Array` construction in `arrow-row` (#…
Replace conversion of binary->string in arrow-row from arraydata to d…
minor: drive-by refactors for dicts in substring & filter (#10264)
perf(arrow-array): speed up `RunArray::try_new` by avoiding the `Arra…
perf(arrow-array): speed up `RunArray::try_new` by avoiding the `Arra…
perf(arrow-array): speed up `RunArray::try_new` by avoiding the `Arra…
[arrow-select] perf: Replace `ArrayData` with direct `Array` construc…
perf(arrow-array): build null, map, and dictionary arrays directly in…