Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve float to string cast by ~20%-40% #5401

Merged
merged 2 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions arrow-cast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ lexical-core = { version = "^0.8", default-features = false, features = ["write-
atoi = "2.0.0"
comfy-table = { version = "7.0", optional = true, default-features = false }
base64 = "0.21"
ryu = "1.0.16"

[dev-dependencies]
criterion = { version = "0.5", default-features = false }
Expand Down
16 changes: 15 additions & 1 deletion arrow-cast/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,23 @@ macro_rules! primitive_display {
};
}

macro_rules! primitive_display_float {
($($t:ty),+) => {
$(impl<'a> DisplayIndex for &'a PrimitiveArray<$t>
{
fn write(&self, idx: usize, f: &mut dyn Write) -> FormatResult {
let value = self.value(idx);
let mut buffer = ryu::Buffer::new();
f.write_str(buffer.format(value))?;
Ok(())
}
})+
};
}

primitive_display!(Int8Type, Int16Type, Int32Type, Int64Type);
primitive_display!(UInt8Type, UInt16Type, UInt32Type, UInt64Type);
primitive_display!(Float32Type, Float64Type);
primitive_display_float!(Float32Type, Float64Type);

impl<'a> DisplayIndex for &'a PrimitiveArray<Float16Type> {
fn write(&self, idx: usize, f: &mut dyn Write) -> FormatResult {
Expand Down
4 changes: 3 additions & 1 deletion arrow/benches/cast_kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ fn add_benchmark(c: &mut Criterion) {
c.bench_function("cast f32 to string 512", |b| {
b.iter(|| cast_array(&f32_array, DataType::Utf8))
});

c.bench_function("cast f64 to string 512", |b| {
b.iter(|| cast_array(&f64_array, DataType::Utf8))
});
c.bench_function("cast timestamp_ms to i64 512", |b| {
b.iter(|| cast_array(&time_ms_array, DataType::Int64))
});
Expand Down
Loading