Skip to content

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


/// 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
@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