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

Minor: improve performance of ScalarValue::Binary* debug #12323

Merged
merged 6 commits into from
Sep 6, 2024

Conversation

alamb
Copy link
Contributor

@alamb alamb commented Sep 4, 2024

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?

  1. Add tests for binary debug
  2. Fix a bug that the tests found
  3. improve performance with fewer allocations

Are these changes tested?

Yes

Are there any user-facing changes?

@github-actions github-actions bot added the common Related to common crate label Sep 4, 2024
@@ -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}, \"")?;
Copy link
Contributor Author

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

@@ -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;
Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and format! as well :)

Copy link
Member

@lewiszlw lewiszlw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

/// 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 {
Copy link
Contributor

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(())
}

Copy link
Contributor

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(())
}

Copy link
Contributor Author

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(())
}

@Dandandan
Copy link
Contributor

Added one small suggestion

@alamb alamb merged commit 923f098 into apache:main Sep 6, 2024
24 checks passed
@alamb alamb deleted the alamb/less_debug_copy branch September 6, 2024 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
common Related to common crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants