-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Minor: improve performance of ScalarValue::Binary*
debug
#12323
Conversation
@@ -3707,7 +3711,7 @@ impl fmt::Debug for ScalarValue { | |||
write!(f, "FixedSizeBinary({size}, {self})") | |||
} | |||
ScalarValue::FixedSizeBinary(size, Some(b)) => { | |||
write!(f, "Binary({size}, \"")?; | |||
write!(f, "FixedSizeBinary({size}, \"")?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a bug (likely copy/paste error) found with the tests
datafusion/common/src/scalar/mod.rs
Outdated
@@ -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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the difference here is that join
allocates a new String
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... and format!
as well :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
datafusion/common/src/scalar/mod.rs
Outdated
/// 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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is slightly more idiomatic written as follows:
fn fmt_binary(data: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
let mut data_iter = data.iter();
if let Some(b) = data_iter.next() {
write!(f, "{}", b)?;
}
for b in data_iter {
write!(f, ",{b}")?;
}
Ok(())
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or even better
fn fmt_binary(data: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
if let Some(b) = data.first() {
write!(f, "{}", b)?;
}
for b in data.iter().skip(1) {
write!(f, ",{b}")?;
}
Ok(())
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I came up with something slightly different that I still think is more idiomatic in 99a57e7.
/// 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 iter = data.iter();
if let Some(b) = iter.next() {
write!(f, "{b}")?;
}
for b in iter {
write!(f, ",{b}")?;
}
Ok(())
}
Added one small suggestion |
Which issue does this PR close?
Rationale for this change
This is similar to #12322
(Another) Follow on to #12192 from @lewiszlw
What changes are included in this PR?
Are these changes tested?
Yes
Are there any user-facing changes?