Skip to content

numfmt: reuse formatting buffers across lines and fields - #16531

Open
vpashaiev wants to merge 1 commit into
uutils:mainfrom
vpashaiev:numfmt-reuse-format-buffer
Open

vpashaiev wants to merge 1 commit into
uutils:mainfrom
vpashaiev:numfmt-reuse-format-buffer

Conversation

@vpashaiev

@vpashaiev vpashaiev commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Formatting each number allocates several temporary String buffers across transform_to, grouping, padding, and format_string.

Reuse a single String buffer across lines and arguments and write formatted output into it in place.

Benchmarks (cargo bench --package uu_numfmt --bench numfmt_bench):

  • numfmt_stream_to_si: 41.97 ms -> 26.26 ms (+68% in CodSpeed)
  • numfmt_large_numbers_si: 12.01 ms -> 7.85 ms (+29% in CodSpeed)
  • numfmt_stream_to_si_precision: 53.96 ms -> 38.93 ms
  • numfmt_to_si: 10.34 ms -> 7.69 ms
  • numfmt_to_iec: 10.40 ms -> 7.76 ms
  • numfmt_padding: 11.43 ms -> 8.75 ms
  • valgrind (100k lines, --to=si): 380,763 allocs (6.56 MB) -> 562 allocs (235 KB)

@codspeed

codspeed Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 20.62%

1 warning

⚠️ 11 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 11 improved benchmarks
✅ 11 untouched benchmarks
⏩ 454 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ CPU Simulation numfmt_stream_to_si 260.8 ms 157.6 ms +65.5%
⚡ CPU Simulation numfmt_stream_to_si_precision 304.2 ms 204.1 ms +49.06%
⚡ CPU Simulation numfmt_to_si_precision[10000] 78.7 ms 67.8 ms +16.02%
⚡ CPU Simulation numfmt_to_si[10000] 75.5 ms 65.9 ms +14.56%
⚡ CPU Simulation numfmt_to_iec[10000] 75.9 ms 66.3 ms +14.51%
⚡ CPU Simulation numfmt_round_modes[("up", 10000)] 76.2 ms 66.6 ms +14.41%
⚡ CPU Simulation numfmt_round_modes[("down", 10000)] 76.4 ms 66.8 ms +14.36%
⚡ CPU Simulation numfmt_round_modes[("towards-zero", 10000)] 76.4 ms 66.8 ms +14.36%
⚡ CPU Simulation numfmt_padding[(10000, 50)] 80.6 ms 70.6 ms +14.11%
⚡ CPU Simulation numfmt_large_numbers_si[10000] 81.8 ms 72 ms +13.7%
⚡ CPU Simulation numfmt_from_si[10000] 64.6 ms 59.9 ms +7.88%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing vpashaiev:numfmt-reuse-format-buffer (ce4fefc) with main (fca3de1)2

Open in CodSpeed

Footnotes

  1. 454 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (36079aa) during the generation of this report, so fca3de1 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/id/smack is no longer failing!
Congrats! The gnu test tests/mkdir/smack-no-root is no longer failing!
Congrats! The gnu test tests/mkdir/smack-root is no longer failing!
Congrats! The gnu test tests/df/skip-rootfs is now passing!

@vpashaiev vpashaiev changed the title numfmt: reuse formatting buffers across lines and fields numfmt: reuse formatting buffers across lines and fields (perf +29%) Nov 16, 2026
Comment thread src/uu/numfmt/src/format.rs Resolved
Comment thread src/uu/numfmt/src/format.rs Resolved
Comment thread src/uu/numfmt/src/format.rs Resolved
Comment thread src/uu/numfmt/src/format.rs Resolved
@sylvestre

Copy link
Copy Markdown
Contributor

thanks for working on this!
The speedup is good, but the change brings in more code than it needs: a padding block repeated 4 times, hand-built digits and two wrappers nothing calls (see inline).
Please also check the red Tests job.

@vpashaiev
vpashaiev marked this pull request as draft November 16, 2026 09:05
@vpashaiev
vpashaiev force-pushed the numfmt-reuse-format-buffer branch from 5b07dee to 9aca9ee Compare November 16, 2026 10:01
@vpashaiev

vpashaiev commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Updated all four points
and
simplified transform_to / apply_grouping_in_place so the diff is now net-negative:

  • added dec_sep note to write_float_localized doc comment
  • replaced the manual < 10.0 digit formatting with write_float_localized and deduplicated the transform_to match arms
  • extracted pad_in_place helper for padding
  • dropped the two unused wrapper functions

@vpashaiev
vpashaiev force-pushed the numfmt-reuse-format-buffer branch from 9aca9ee to d19d754 Compare November 16, 2026 11:14
@vpashaiev vpashaiev changed the title numfmt: reuse formatting buffers across lines and fields (perf +29%) numfmt: reuse formatting buffers across lines and fields (perf +20%) Nov 16, 2026
@vpashaiev
vpashaiev marked this pull request as ready for review November 17, 2026 06:18
@sylvestre
sylvestre force-pushed the numfmt-reuse-format-buffer branch from d19d754 to b2b0a2f Compare November 17, 2026 06:38
Comment thread src/uu/numfmt/src/format.rs Resolved
Comment thread src/uu/numfmt/src/format.rs Resolved
@vpashaiev
vpashaiev force-pushed the numfmt-reuse-format-buffer branch from b2b0a2f to ce4fefc Compare November 17, 2026 16:04
@github-actions

Copy link
Copy Markdown

Binary size comparison:

Individual binary size comparison VS main (threshold: >=5% AND >=4 KB).

Total size of compared binaries: 155.49 MB (+104 KB, +0.07%)

Significant per-binary changes:
  env     1.20 MB ->    1.28 MB  (+80 KB, +6.51%)

@vpashaiev
vpashaiev force-pushed the numfmt-reuse-format-buffer branch from ce4fefc to 48b2ca5 Compare November 18, 2026 14:49
@vpashaiev vpashaiev changed the title numfmt: reuse formatting buffers across lines and fields (perf +20%) numfmt: reuse formatting buffers across lines and fields Nov 18, 2026

This branch has not been deployed

No deployments
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.

2 participants