Skip to content

Commit

Permalink
Improve float to string performance using ryu
Browse files Browse the repository at this point in the history
  • Loading branch information
psvri committed Feb 15, 2024
1 parent 0bcb19b commit 428a7b4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
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

0 comments on commit 428a7b4

Please sign in to comment.