From 3ed59813e8e4c80fb70209052f1955bdd88fab67 Mon Sep 17 00:00:00 2001 From: Andrew Lamb Date: Wed, 4 Sep 2024 09:26:23 -0400 Subject: [PATCH] reduce allocations --- datafusion/common/src/scalar/mod.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/datafusion/common/src/scalar/mod.rs b/datafusion/common/src/scalar/mod.rs index b48247d57ec2..3e5e55bfdb60 100644 --- a/datafusion/common/src/scalar/mod.rs +++ b/datafusion/common/src/scalar/mod.rs @@ -3646,18 +3646,22 @@ fn fmt_list(arr: ArrayRef, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{value_formatter}") } +/// writes a byte array to formatter. `[1, 2, 3]` ==> `"1,2,3"` +fn fmt_binary(data: &[u8], f: &mut fmt::Formatter) -> fmt::Result { + let mut first = true; + for b in data { + if !first { + write!(f, ",{b}")?; + } else { + write!(f, "{}", b)?; + } + first = false; + } + Ok(()) +} + impl fmt::Debug for ScalarValue { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fn fmt_binary(data: &[u8], f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "{}", - data.iter() - .map(|v| format!("{v}")) - .collect::>() - .join(",") - ) - } match self { ScalarValue::Decimal128(_, _, _) => write!(f, "Decimal128({self})"), ScalarValue::Decimal256(_, _, _) => write!(f, "Decimal256({self})"),