-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unary and Binary functions trait (#726)
Introducing specialized traits for unary (like map) and binary (like &) operations on arrays. This will allow us to have specialized and ergonomic implementations for cases when we know the encoding of an array (like in `ArrayCompute` functions) ## Benchmarks As of 08bbd8d running on my M3 Max macbook ``` arrow_unary_add time: [228.22 µs 233.98 µs 239.93 µs] vortex_unary_add time: [1.5322 µs 1.5469 µs 1.5604 µs] arrow_binary_add time: [237.74 µs 240.93 µs 243.98 µs] vortex_binary_add time: [93.821 µs 94.796 µs 95.918 µs] ```
- Loading branch information
Showing
8 changed files
with
410 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,3 +89,7 @@ harness = false | |
[[bench]] | ||
name = "iter" | ||
harness = false | ||
|
||
[[bench]] | ||
name = "fn" | ||
harness = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use arrow_array::types::UInt32Type; | ||
use arrow_array::UInt32Array; | ||
use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; | ||
use vortex::array::PrimitiveArray; | ||
use vortex::elementwise::{BinaryFn, UnaryFn}; | ||
use vortex::validity::Validity; | ||
use vortex::IntoArray; | ||
|
||
fn vortex_unary_add(c: &mut Criterion) { | ||
let data = PrimitiveArray::from_vec((0_u32..1_000_000).collect::<Vec<_>>(), Validity::AllValid); | ||
c.bench_function("vortex_unary_add", |b| { | ||
b.iter_batched( | ||
|| (data.clone()), | ||
|data| data.unary(|v: u32| v + 1).unwrap(), | ||
BatchSize::SmallInput, | ||
) | ||
}); | ||
} | ||
|
||
fn arrow_unary_add(c: &mut Criterion) { | ||
let data = UInt32Array::from_iter_values(0_u32..1_000_000); | ||
c.bench_function("arrow_unary_add", |b| { | ||
b.iter_batched( | ||
|| data.clone(), | ||
|data: arrow_array::PrimitiveArray<UInt32Type>| data.unary::<_, UInt32Type>(|v| v + 1), | ||
BatchSize::SmallInput, | ||
) | ||
}); | ||
} | ||
|
||
fn vortex_binary_add(c: &mut Criterion) { | ||
let lhs = PrimitiveArray::from_vec((0_u32..1_000_000).collect::<Vec<_>>(), Validity::AllValid); | ||
let rhs = PrimitiveArray::from_vec((0_u32..1_000_000).collect::<Vec<_>>(), Validity::AllValid) | ||
.into_array(); | ||
c.bench_function("vortex_binary_add", |b| { | ||
b.iter_batched( | ||
|| (lhs.clone(), rhs.clone()), | ||
|(lhs, rhs)| lhs.binary(rhs, |l: u32, r: u32| l + r), | ||
BatchSize::SmallInput, | ||
) | ||
}); | ||
} | ||
|
||
fn arrow_binary_add(c: &mut Criterion) { | ||
let lhs = UInt32Array::from_iter_values(0_u32..1_000_000); | ||
let rhs = UInt32Array::from_iter_values(0_u32..1_000_000); | ||
c.bench_function("arrow_binary_add", |b| { | ||
b.iter_batched( | ||
|| (lhs.clone(), rhs.clone()), | ||
|(lhs, rhs)| { | ||
arrow_arith::arity::binary::<_, _, _, UInt32Type>(&lhs, &rhs, |a, b| a + b).unwrap() | ||
}, | ||
BatchSize::SmallInput, | ||
) | ||
}); | ||
} | ||
|
||
criterion_group!( | ||
name = benches; | ||
config = Criterion::default(); | ||
targets = | ||
arrow_unary_add, | ||
vortex_unary_add, | ||
arrow_binary_add, | ||
vortex_binary_add, | ||
); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use vortex_dtype::{NativePType, PType}; | ||
use vortex_error::VortexResult; | ||
|
||
use crate::iter::Batch; | ||
use crate::{Array, ArrayDType}; | ||
|
||
pub trait BinaryFn { | ||
fn binary<I: NativePType, U: NativePType, O: NativePType, F: Fn(I, U) -> O>( | ||
&self, | ||
rhs: Array, | ||
binary_fn: F, | ||
) -> VortexResult<Array>; | ||
} | ||
|
||
pub trait UnaryFn { | ||
fn unary<I: NativePType, O: NativePType, F: Fn(I) -> O>( | ||
&self, | ||
unary_fn: F, | ||
) -> VortexResult<Array>; | ||
} | ||
|
||
pub fn dyn_cast_array_iter<N: NativePType>(array: &Array) -> Box<dyn Iterator<Item = Batch<N>>> { | ||
match PType::try_from(array.dtype()).unwrap() { | ||
PType::U8 => Box::new( | ||
array | ||
.with_dyn(|a| a.as_primitive_array_unchecked().u8_iter()) | ||
.unwrap() | ||
.map(|b| b.as_::<N>()), | ||
), | ||
PType::U16 => Box::new( | ||
array | ||
.with_dyn(|a| a.as_primitive_array_unchecked().u16_iter()) | ||
.unwrap() | ||
.map(|b| b.as_::<N>()), | ||
), | ||
PType::U32 => Box::new( | ||
array | ||
.with_dyn(|a| a.as_primitive_array_unchecked().u32_iter()) | ||
.unwrap() | ||
.map(|b| b.as_::<N>()), | ||
), | ||
PType::U64 => Box::new( | ||
array | ||
.with_dyn(|a| a.as_primitive_array_unchecked().u64_iter()) | ||
.unwrap() | ||
.map(|b| b.as_::<N>()), | ||
), | ||
PType::I8 => Box::new( | ||
array | ||
.with_dyn(|a| a.as_primitive_array_unchecked().i8_iter()) | ||
.unwrap() | ||
.map(|b| b.as_::<N>()), | ||
), | ||
PType::I16 => Box::new( | ||
array | ||
.with_dyn(|a| a.as_primitive_array_unchecked().i16_iter()) | ||
.unwrap() | ||
.map(|b| b.as_::<N>()), | ||
), | ||
PType::I32 => Box::new( | ||
array | ||
.with_dyn(|a| a.as_primitive_array_unchecked().i32_iter()) | ||
.unwrap() | ||
.map(|b| b.as_::<N>()), | ||
), | ||
PType::I64 => Box::new( | ||
array | ||
.with_dyn(|a| a.as_primitive_array_unchecked().i64_iter()) | ||
.unwrap() | ||
.map(|b| b.as_::<N>()), | ||
), | ||
PType::F16 => Box::new( | ||
array | ||
.with_dyn(|a| a.as_primitive_array_unchecked().u64_iter()) | ||
.unwrap() | ||
.map(|b| b.as_::<N>()), | ||
), | ||
PType::F32 => Box::new( | ||
array | ||
.with_dyn(|a| a.as_primitive_array_unchecked().f32_iter()) | ||
.unwrap() | ||
.map(|b| b.as_::<N>()), | ||
), | ||
PType::F64 => Box::new( | ||
array | ||
.with_dyn(|a| a.as_primitive_array_unchecked().f64_iter()) | ||
.unwrap() | ||
.map(|b| b.as_::<N>()), | ||
), | ||
} | ||
} |
Oops, something went wrong.