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

feat: display type #76

Merged
merged 3 commits into from
Feb 19, 2025
Merged

feat: display type #76

merged 3 commits into from
Feb 19, 2025

Conversation

robjtede
Copy link
Member

@robjtede robjtede commented Feb 12, 2025

waiting for #71

similar to #32

@robjtede robjtede linked an issue Feb 12, 2025 that may be closed by this pull request
src/lib.rs Outdated
Comment on lines 253 to 235
f.pad(&to_string_format(self.0, Format::IEC))
f.pad(&self.display().to_string())
Copy link
Member Author

Choose a reason for hiding this comment

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

be nice to get rid of this allocation

src/display.rs Outdated
Comment on lines 51 to 54
let unit_prefixes = match self.format {
Format::Iec | Format::Short => crate::UNITS_IEC.as_bytes(),
Format::Si => crate::UNITS_SI.as_bytes(),
};
Copy link
Member Author

Choose a reason for hiding this comment

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

Short is currently assumed to use IEC (binary) units due to the motivating use case of sort -h. WDYT about that?

Copy link

@wookietreiber wookietreiber Feb 12, 2025

Choose a reason for hiding this comment

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

How about combination:

enum ByteUnit {
    Iec,
    Si,
}

enum ByteFormat {
    /// Human readable output.
    Human(ByteUnit),

    /// For `sort -h` and friends.
    Short(ByteUnit),
}

Copy link
Member Author

@robjtede robjtede Feb 13, 2025

Choose a reason for hiding this comment

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

how we repr this internal isn't very important since we're exposing methods on Display

i've added a note on the iec_short method about compat with sort -h

@robjtede robjtede force-pushed the display-type branch 2 times, most recently from d1877f8 to 0484052 Compare February 13, 2025 20:34
@robjtede robjtede marked this pull request as ready for review February 13, 2025 20:35
@robjtede robjtede requested review from hyunsik, wookietreiber and MrCroxx and removed request for wookietreiber February 13, 2025 20:36
@robjtede
Copy link
Member Author

robjtede commented Feb 13, 2025

The added display benchmark shows that the additional fast path branch is worth it:

Timer precision: 41 ns
display                         fastest       │ slowest       │ median        │ mean          │ samples │ iters
╰─ display_inner_display        160.4 ns      │ 738.6 ns      │ 165.7 ns      │ 169.5 ns      │ 1000    │ 32000
├─ display_bytesize_standard    160.4 ns      │ 613.6 ns      │ 169.6 ns      │ 171.3 ns      │ 1000    │ 32000
├─ display_always_pad_standard  187.8 ns      │ 402.6 ns      │ 194.3 ns      │ 196.2 ns      │ 1000    │ 32000
├─ display_bytesize_custom      251.6 ns      │ 381.8 ns      │ 262 ns        │ 262.7 ns      │ 1000    │ 16000
├─ display_always_pad_custom    249 ns        │ 10.66 µs      │ 290 ns        │ 290.4 ns      │ 1000    │ 1000

Table sorted by median ascending (lower is beter)

Comment on lines +237 to +242
if f.width().is_none() && f.precision().is_none() {
// allocation-free fast path for when no formatting options are specified
write!(f, "{display}")
} else {
f.pad(&display.to_string())
}
Copy link
Member Author

Choose a reason for hiding this comment

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

this fast path trick was taken from std::fmt::Formatter::pad

https://doc.rust-lang.org/1.84.0/src/core/fmt/mod.rs.html#1438-1442

@robjtede
Copy link
Member Author

bump @MrCroxx for review

i'm keen to get v2 prepped after this

Copy link
Collaborator

@MrCroxx MrCroxx left a comment

Choose a reason for hiding this comment

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

LGTM

Thank you for your contribution. The code is awesome. 🤩

I have no comments on the code. But may I ask where does the "short" format come from, which standard or similar library?

@robjtede
Copy link
Member Author

robjtede commented Feb 18, 2025

where does the "short" format come from

we discussed a bit here #14 but i'm open to changing the name "short" to something else

@robjtede robjtede merged commit ce103e6 into master Feb 19, 2025
11 checks passed
@robjtede robjtede deleted the display-type branch February 19, 2025 21:42
@robjtede robjtede added this to the 2.0 milestone Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sort -h friendly output mode
3 participants