Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Improved performance in cast Primitive to Binary/String (2x) (#646)
Browse files Browse the repository at this point in the history
  • Loading branch information
sundy-li authored Nov 30, 2021
1 parent a2d84d9 commit 23c7d8a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
4 changes: 4 additions & 0 deletions benches/cast_kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ fn add_benchmark(c: &mut Criterion) {
c.bench_function("cast utf8 to date64 512", |b| {
b.iter(|| cast_array(&utf8_date_time_array, DataType::Date64))
});

c.bench_function("cast int32 to binary 512", |b| {
b.iter(|| cast_array(&i32_array, DataType::Binary))
});
}

criterion_group!(benches, add_benchmark);
Expand Down
44 changes: 34 additions & 10 deletions src/compute/cast/primitive_to.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use std::hash::Hash;

use crate::error::Result;
use crate::{
array::*,
bitmap::Bitmap,
compute::arity::unary,
datatypes::{DataType, TimeUnit},
temporal_conversions::*,
types::NativeType,
};
use crate::{
error::Result,
util::{lexical_to_bytes, lexical_to_string},
util::lexical_to_bytes_mut,
};

use super::CastOptions;
Expand All @@ -19,9 +17,21 @@ use super::CastOptions;
pub fn primitive_to_binary<T: NativeType + lexical_core::ToLexical, O: Offset>(
from: &PrimitiveArray<T>,
) -> BinaryArray<O> {
let iter = from.iter().map(|x| x.map(|x| lexical_to_bytes(*x)));

BinaryArray::from_trusted_len_iter(iter)
let mut buffer = vec![];
let builder = from.iter().fold(
MutableBinaryArray::<O>::with_capacity(from.len()),
|mut builder, x| {
match x {
Some(x) => {
lexical_to_bytes_mut(*x, &mut buffer);
builder.push(Some(buffer.as_slice()));
}
None => builder.push_null(),
}
builder
},
);
builder.into()
}

pub(super) fn primitive_to_binary_dyn<T, O>(from: &dyn Array) -> Result<Box<dyn Array>>
Expand Down Expand Up @@ -60,9 +70,23 @@ where
pub fn primitive_to_utf8<T: NativeType + lexical_core::ToLexical, O: Offset>(
from: &PrimitiveArray<T>,
) -> Utf8Array<O> {
let iter = from.iter().map(|x| x.map(|x| lexical_to_string(*x)));

Utf8Array::from_trusted_len_iter(iter)
let mut buffer = vec![];
let builder = from.iter().fold(
MutableUtf8Array::<O>::with_capacity(from.len()),
|mut builder, x| {
match x {
Some(x) => {
lexical_to_bytes_mut(*x, &mut buffer);
builder.push(Some(unsafe {
std::str::from_utf8_unchecked(buffer.as_slice())
}));
}
None => builder.push_null(),
}
builder
},
);
builder.into()
}

pub(super) fn primitive_to_utf8_dyn<T, O>(from: &dyn Array) -> Result<Box<dyn Array>>
Expand Down

0 comments on commit 23c7d8a

Please sign in to comment.