Skip to content

Commit 1a880d6

Browse files
authored
Improve float to string cast by ~20%-40% (#5401)
* Add cast f64 to string to benchmark * Improve float to string performance using ryu
1 parent 2e6c7b9 commit 1a880d6

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

arrow-cast/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ lexical-core = { version = "^0.8", default-features = false, features = ["write-
5252
atoi = "2.0.0"
5353
comfy-table = { version = "7.0", optional = true, default-features = false }
5454
base64 = "0.21"
55+
ryu = "1.0.16"
5556

5657
[dev-dependencies]
5758
criterion = { version = "0.5", default-features = false }

arrow-cast/src/display.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,23 @@ macro_rules! primitive_display {
427427
};
428428
}
429429

430+
macro_rules! primitive_display_float {
431+
($($t:ty),+) => {
432+
$(impl<'a> DisplayIndex for &'a PrimitiveArray<$t>
433+
{
434+
fn write(&self, idx: usize, f: &mut dyn Write) -> FormatResult {
435+
let value = self.value(idx);
436+
let mut buffer = ryu::Buffer::new();
437+
f.write_str(buffer.format(value))?;
438+
Ok(())
439+
}
440+
})+
441+
};
442+
}
443+
430444
primitive_display!(Int8Type, Int16Type, Int32Type, Int64Type);
431445
primitive_display!(UInt8Type, UInt16Type, UInt32Type, UInt64Type);
432-
primitive_display!(Float32Type, Float64Type);
446+
primitive_display_float!(Float32Type, Float64Type);
433447

434448
impl<'a> DisplayIndex for &'a PrimitiveArray<Float16Type> {
435449
fn write(&self, idx: usize, f: &mut dyn Write) -> FormatResult {

arrow/benches/cast_kernels.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ fn add_benchmark(c: &mut Criterion) {
207207
c.bench_function("cast f32 to string 512", |b| {
208208
b.iter(|| cast_array(&f32_array, DataType::Utf8))
209209
});
210-
210+
c.bench_function("cast f64 to string 512", |b| {
211+
b.iter(|| cast_array(&f64_array, DataType::Utf8))
212+
});
211213
c.bench_function("cast timestamp_ms to i64 512", |b| {
212214
b.iter(|| cast_array(&time_ms_array, DataType::Int64))
213215
});

0 commit comments

Comments
 (0)