Skip to content

Commit

Permalink
Improve float to string cast by ~20%-40% (#5401)
Browse files Browse the repository at this point in the history
* Add cast f64 to string to benchmark

* Improve float to string performance using ryu
  • Loading branch information
psvri authored Feb 18, 2024
1 parent 2e6c7b9 commit 1a880d6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
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

0 comments on commit 1a880d6

Please sign in to comment.