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

refactor(rust): Simplify decimal formatting and remove itoap dep #20880

Merged
merged 2 commits into from
Jan 24, 2025
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
8 changes: 0 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ hashbrown_old_nightly_hack = { package = "hashbrown", version = "0.14.5", featur
hex = "0.4.3"
indexmap = { version = "2", features = ["std", "serde"] }
itoa = "1.0.6"
itoap = { version = "1", features = ["simd"] }
libc = "0.2"
memchr = "2.6"
memmap = { package = "memmap2", version = "0.9" }
Expand Down
3 changes: 1 addition & 2 deletions crates/polars-arrow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ ethnum = { workspace = true }
atoi_simd = { workspace = true, optional = true }
fast-float2 = { workspace = true, optional = true }
itoa = { workspace = true, optional = true }
itoap = { workspace = true, optional = true }
ryu = { workspace = true, optional = true }

regex = { workspace = true, optional = true }
Expand Down Expand Up @@ -144,7 +143,7 @@ timezones = [
"chrono-tz",
]
dtype-array = []
dtype-decimal = ["atoi", "itoap"]
dtype-decimal = ["atoi", "itoa"]
bigidx = ["polars-utils/bigidx"]
nightly = []
performant = []
Expand Down
104 changes: 42 additions & 62 deletions crates/polars-arrow/src/compute/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,36 +112,69 @@ pub fn deserialize_decimal(mut bytes: &[u8], precision: Option<u8>, scale: u8) -
}
}

const BUF_LEN: usize = 48;
const MAX_DECIMAL_LEN: usize = 48;

#[derive(Clone, Copy)]
pub struct FormatBuffer {
data: [u8; BUF_LEN],
pub struct DecimalFmtBuffer {
data: [u8; MAX_DECIMAL_LEN],
len: usize,
}

impl Default for FormatBuffer {
impl Default for DecimalFmtBuffer {
fn default() -> Self {
Self::new()
}
}

impl FormatBuffer {
impl DecimalFmtBuffer {
#[inline]
pub const fn new() -> Self {
Self {
data: [0; BUF_LEN],
data: [0; MAX_DECIMAL_LEN],
len: 0,
}
}

#[inline]
pub fn as_str(&self) -> &str {
pub fn format(&mut self, x: i128, scale: usize, trim_zeros: bool) -> &str {
let factor = POW10[scale];
let mut itoa_buf = itoa::Buffer::new();

self.len = 0;
let (div, rem) = x.unsigned_abs().div_rem_euclid(&factor);
if x < 0 {
self.data[0] = b'-';
self.len += 1;
}

let div_fmt = itoa_buf.format(div);
self.data[self.len..self.len + div_fmt.len()].copy_from_slice(div_fmt.as_bytes());
self.len += div_fmt.len();

if scale == 0 {
return unsafe { std::str::from_utf8_unchecked(&self.data[..self.len]) };
}

self.data[self.len] = b'.';
self.len += 1;

let rem_fmt = itoa_buf.format(rem + factor); // + factor adds leading 1 where period would be.
self.data[self.len..self.len + rem_fmt.len() - 1].copy_from_slice(rem_fmt[1..].as_bytes());
self.len += rem_fmt.len() - 1;

if trim_zeros {
while self.data.get(self.len - 1) == Some(&b'0') {
self.len -= 1;
}
if self.data.get(self.len - 1) == Some(&b'.') {
self.len -= 1;
}
}

unsafe { std::str::from_utf8_unchecked(&self.data[..self.len]) }
}
}

const POW10: [i128; 39] = [
const POW10: [u128; 39] = [
1,
10,
100,
Expand Down Expand Up @@ -183,59 +216,6 @@ const POW10: [i128; 39] = [
100000000000000000000000000000000000000,
];

pub fn format_decimal(v: i128, scale: usize, trim_zeros: bool) -> FormatBuffer {
const ZEROS: [u8; BUF_LEN] = [b'0'; BUF_LEN];

let mut buf = FormatBuffer::new();
let factor = POW10[scale];
let (div, rem) = v.abs().div_rem_euclid(&factor);

unsafe {
let mut ptr = buf.data.as_mut_ptr();
if v < 0 {
*ptr = b'-';
buf.len = 1;
ptr = ptr.add(1);
}
let n_whole = itoap::write_to_ptr(ptr, div);
buf.len += n_whole;
ptr = ptr.add(n_whole);

if scale == 0 {
return buf;
}

*ptr = b'.';
ptr = ptr.add(1);

if rem != 0 {
let mut frac_buf = [0_u8; BUF_LEN];
let n_frac = itoap::write_to_ptr(frac_buf.as_mut_ptr(), rem);
std::ptr::copy_nonoverlapping(ZEROS.as_ptr(), ptr, scale - n_frac);
ptr = ptr.add(scale - n_frac);
std::ptr::copy_nonoverlapping(frac_buf.as_mut_ptr(), ptr, n_frac);
ptr = ptr.add(n_frac);
} else {
std::ptr::copy_nonoverlapping(ZEROS.as_ptr(), ptr, scale);
ptr = ptr.add(scale);
}
buf.len += 1 + scale;

if trim_zeros {
ptr = ptr.sub(1);
while *ptr == b'0' {
ptr = ptr.sub(1);
buf.len -= 1;
}
if *ptr == b'.' {
buf.len -= 1;
}
}
}

buf
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
2 changes: 0 additions & 2 deletions crates/polars-compute/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ chrono = { workspace = true, optional = true }
either = { workspace = true }
fast-float2 = { workspace = true, optional = true }
itoa = { workspace = true, optional = true }
itoap = { workspace = true, optional = true }
num-traits = { workspace = true }
polars-error = { workspace = true }
polars-utils = { workspace = true }
Expand All @@ -37,7 +36,6 @@ cast = [
"dep:chrono",
"dep:fast-float2",
"dep:itoa",
"dep:itoap",
"dep:ryu",
]
gather = []
Expand Down
7 changes: 4 additions & 3 deletions crates/polars-compute/src/cast/decimal_to.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,18 @@ where
/// Returns a [`Utf8Array`] where every element is the utf8 representation of the decimal.
#[cfg(feature = "dtype-decimal")]
pub(super) fn decimal_to_utf8view(from: &PrimitiveArray<i128>) -> Utf8ViewArray {
use arrow::compute::decimal::DecimalFmtBuffer;

let (_, from_scale) = if let ArrowDataType::Decimal(p, s) = from.dtype().to_logical_type() {
(*p, *s)
} else {
panic!("internal error: i128 is always a decimal")
};

let mut mutable = MutableBinaryViewArray::with_capacity(from.len());

let mut fmt_buf = DecimalFmtBuffer::new();
for &x in from.values().iter() {
let buf = arrow::compute::decimal::format_decimal(x, from_scale, false);
mutable.push_value_ignore_validity(buf.as_str())
mutable.push_value_ignore_validity(fmt_buf.format(x, from_scale, false))
}

mutable.freeze().with_validity(from.validity().cloned())
Expand Down
6 changes: 2 additions & 4 deletions crates/polars-core/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1293,11 +1293,9 @@ impl Series {
#[inline]
#[cfg(feature = "dtype-decimal")]
fn fmt_decimal(f: &mut Formatter<'_>, v: i128, scale: usize) -> fmt::Result {
use arrow::compute::decimal::format_decimal;

let mut fmt_buf = arrow::compute::decimal::DecimalFmtBuffer::new();
let trim_zeros = get_trim_decimal_zeros();
let repr = format_decimal(v, scale, trim_zeros);
f.write_str(fmt_float_string(repr.as_str()).as_str())
f.write_str(fmt_float_string(fmt_buf.format(v, scale, trim_zeros)).as_str())
}

#[cfg(all(
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-io/src/csv/write/write_impl/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ fn bool_serializer<const QUOTE_NON_NULL: bool>(array: &BooleanArray) -> impl Ser
fn decimal_serializer(array: &PrimitiveArray<i128>, scale: usize) -> impl Serializer {
let trim_zeros = arrow::compute::decimal::get_trim_decimal_zeros();

let mut fmt_buf = arrow::compute::decimal::DecimalFmtBuffer::new();
let f = move |&item, buf: &mut Vec<u8>, _options: &SerializeOptions| {
let value = arrow::compute::decimal::format_decimal(item, scale, trim_zeros);
buf.extend_from_slice(value.as_str().as_bytes());
buf.extend_from_slice(fmt_buf.format(item, scale, trim_zeros).as_bytes());
};

make_serializer::<_, _, false>(f, array.iter(), |array| {
Expand Down
5 changes: 3 additions & 2 deletions crates/polars-json/src/json/write/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::Write;
use arrow::array::*;
use arrow::bitmap::utils::ZipValidity;
#[cfg(feature = "dtype-decimal")]
use arrow::compute::decimal::{format_decimal, get_trim_decimal_zeros};
use arrow::compute::decimal::get_trim_decimal_zeros;
use arrow::datatypes::{ArrowDataType, IntegerType, TimeUnit};
use arrow::io::iterator::BufStreamingIterator;
use arrow::offset::Offset;
Expand Down Expand Up @@ -122,9 +122,10 @@ fn decimal_serializer<'a>(
take: usize,
) -> Box<dyn StreamingIterator<Item = [u8]> + 'a + Send + Sync> {
let trim_zeros = get_trim_decimal_zeros();
let mut fmt_buf = arrow::compute::decimal::DecimalFmtBuffer::new();
let f = move |x: Option<&i128>, buf: &mut Vec<u8>| {
if let Some(x) = x {
utf8::write_str(buf, format_decimal(*x, scale, trim_zeros).as_str()).unwrap()
utf8::write_str(buf, fmt_buf.format(*x, scale, trim_zeros)).unwrap()
} else {
buf.extend(b"null")
}
Expand Down
Loading