Skip to content

Commit f6fdf61

Browse files
committed
uucore: format: Small optimizations in num_format for seq
In most common use cases: - We can bypass a lot of `write_output` when width == 0. - Simplify format_float_decimal when the input is an integer. Also document another interesting case in src/uu/seq/BENCHMARKING.md.
1 parent f4ecafa commit f6fdf61

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/uu/seq/BENCHMARKING.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ hyperfine -L seq seq,./target/release/seq "{seq} 0 0.000001 1"
4343
hyperfine -L seq seq,./target/release/seq "{seq} -100 1 1000000"
4444
```
4545

46+
It is also interesting to compare performance with large precision
47+
format. But in this case, the output itself should also be compared,
48+
as GNU `seq` may not provide the same precision (`uutils` version of
49+
`seq` provides arbitrary precision, while GNU `seq` appears to be
50+
limited to `long double` on the given platform, i.e. 64/80/128-bit
51+
float):
52+
```shell
53+
hyperfine -L seq seq,target/release/seq "{seq} -f%.30f 0 0.000001 1"
54+
```
55+
4656
## Optimizations
4757

4858
### Buffering stdout

src/uucore/src/lib/features/format/num_format.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,16 @@ fn format_float_non_finite(e: &ExtendedBigDecimal, case: Case) -> String {
354354

355355
fn format_float_decimal(bd: &BigDecimal, precision: usize, force_decimal: ForceDecimal) -> String {
356356
debug_assert!(!bd.is_negative());
357-
if precision == 0 && force_decimal == ForceDecimal::Yes {
358-
format!("{bd:.0}.")
359-
} else {
360-
format!("{bd:.precision$}")
357+
if precision == 0 {
358+
let (bi, scale) = bd.as_bigint_and_scale();
359+
if scale == 0 && force_decimal != ForceDecimal::Yes {
360+
// Optimization when printing integers.
361+
return bi.to_str_radix(10);
362+
} else if force_decimal == ForceDecimal::Yes {
363+
return format!("{bd:.0}.");
364+
}
361365
}
366+
format!("{bd:.precision$}")
362367
}
363368

364369
fn format_float_scientific(
@@ -614,6 +619,11 @@ fn write_output(
614619
width: usize,
615620
alignment: NumberAlignment,
616621
) -> std::io::Result<()> {
622+
if width == 0 {
623+
writer.write_all(sign_indicator.as_bytes())?;
624+
writer.write_all(s.as_bytes())?;
625+
return Ok(());
626+
}
617627
// Take length of `sign_indicator`, which could be 0 or 1, into consideration when padding
618628
// by storing remaining_width indicating the actual width needed.
619629
// Using min() because self.width could be 0, 0usize - 1usize should be avoided

0 commit comments

Comments
 (0)