From d9afa20befb568ecc9a656d9649ec67199578ebe Mon Sep 17 00:00:00 2001 From: Alexander Beedie Date: Fri, 8 Nov 2024 19:42:37 +0400 Subject: [PATCH] return to the faster-performing version --- crates/polars-core/src/fmt.rs | 48 ++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/crates/polars-core/src/fmt.rs b/crates/polars-core/src/fmt.rs index fc5f6a9e84d1c..cd85b81453ea5 100644 --- a/crates/polars-core/src/fmt.rs +++ b/crates/polars-core/src/fmt.rs @@ -1019,13 +1019,12 @@ pub fn fmt_duration_string(mut v: i64, unit: TimeUnit, iso: bool) -> String { if v < 0 { // negative sign before "P" indicates that the entire ISO duration is negative. // the Polars version applies a negative sign to each *individual* part. - write!(s, "-P").unwrap(); + s.push_str("-P"); v = v.abs() } else { - write!(s, "P").unwrap(); + s.push('P'); } }; - // iterate over dtype-specific sizes to appropriately scale // and extract 'days', 'hours', 'minutes', and 'seconds' parts. for (i, &size) in sizes.iter().enumerate() { @@ -1035,63 +1034,70 @@ pub fn fmt_duration_string(mut v: i64, unit: TimeUnit, iso: bool) -> String { (v % sizes[i - 1]) / size }; if whole_num != 0 || (iso && i == 3) { - write!(s, "{}", buffer.format(whole_num)).unwrap(); + s.push_str(buffer.format(whole_num)); if iso { // (index 3 => 'seconds' part): the ISO version writes - // fractional seconds, not nano/micro/milliseconds + // fractional seconds, not integer nano/micro/milliseconds. if i == 3 { let secs = match unit { TimeUnit::Nanoseconds => format!(".{:09}", v % size), TimeUnit::Microseconds => format!(".{:06}", v % size), TimeUnit::Milliseconds => format!(".{:03}", v % size), }; - write!(s, "{}", secs.trim_end_matches('0')).unwrap(); + s.push_str(secs.trim_end_matches('0')); } - write!(s, "{}", ISO_DURATION_PARTS[i]).unwrap(); + s.push_str(ISO_DURATION_PARTS[i]); // (index 0 => 'days' part): after writing days above (if non-zero) - // the ISO duration string requires a `T` before the time part + // the ISO duration string requires a `T` before the time part. if i == 0 { - write!(s, "T").unwrap(); + s.push('T'); } } else { - write!(s, "{}", DURATION_PARTS[i]).unwrap(); + s.push_str(DURATION_PARTS[i]); if v % size != 0 { - write!(s, " ").unwrap(); + s.push(' '); } } } else if iso && i == 0 { - write!(s, "T").unwrap(); + // always need to write the `T` separator for ISO + // durations, even if there is no 'days' part. + s.push('T'); } } if iso { - // ISO version has already written fractional seconds (if non-zero) + // ISO version has already written out the fractional seconds (if non-zero). if s.ends_with('T') { s.pop(); } } else { - // Polars version writes out fractional seconds separately as - // integer nano/micro/millseconds + // Polars version writes out fractional seconds as integer nano/micro/milliseconds. match unit { TimeUnit::Nanoseconds => { if v % 1000 != 0 { - write!(s, "{}ns", buffer.format(v % 1_000_000_000)).unwrap(); + s.push_str(buffer.format(v % 1_000_000_000)); + s.push_str("ns"); } else if v % 1_000_000 != 0 { - write!(s, "{}µs", buffer.format((v % 1_000_000_000) / 1000)).unwrap(); + s.push_str(buffer.format((v % 1_000_000_000) / 1000)); + s.push_str("µs"); } else if v % 1_000_000_000 != 0 { - write!(s, "{}ms", buffer.format((v % 1_000_000_000) / 1_000_000)).unwrap(); + s.push_str(buffer.format((v % 1_000_000_000) / 1_000_000)); + s.push_str("ms"); } }, TimeUnit::Microseconds => { if v % 1000 != 0 { - write!(s, "{}µs", buffer.format(v % 1_000_000)).unwrap(); + s.push_str(buffer.format(v % 1_000_000)); + s.push_str("µs"); } else if v % 1_000_000 != 0 { - write!(s, "{}ms", buffer.format((v % 1_000_000) / 1_000)).unwrap(); + s.push_str(buffer.format((v % 1_000_000) / 1_000)); + s.push_str("ms"); } }, TimeUnit::Milliseconds => { if v % 1000 != 0 { - write!(s, "{}ms", buffer.format(v % 1_000)).unwrap(); + s.push_str(buffer.format(v % 1_000)); + s.push_str("ms"); } }, }