Skip to content

perf: eliminate heap allocations in format_decimal_bytes - #5486

Merged
oferchen merged 2 commits into
masterfrom
perf/rss-format-decimal-stack-buf
Jun 5, 2026
Merged

oferchen merged 2 commits into
masterfrom
perf/rss-format-decimal-stack-buf

Conversation

@oferchen

@oferchen oferchen commented Jun 5, 2026

Copy link
Copy Markdown
Owner

Summary

  • Replace Vec<String> intermediate allocation in format_decimal_bytes with a fixed [u8; 26] stack buffer, eliminating 3+ heap allocations per call
  • RSS-2 profiling identified this as allocation site Add byte-based legacy greeting parser #7 at 100K files (4.9% / 9.6 MB of total allocations)
  • The new implementation writes digits and commas right-to-left into a stack-allocated array, producing only the final return String

Test plan

  • Existing tests (format_decimal_bytes_small, _thousands, _millions) verify output parity
  • New format_decimal_bytes_edge_cases test covers: 1, 10, 100, 1000, 10000, 100000, 1B, and u64::MAX
  • Upstream test suite (progress_format_upstream.rs) validates 0, 999, 1000, 12345, 1.2M, 1.2B, u64::MAX
  • CI nextest validates all platforms (Linux, macOS, Windows)

Replace Vec<String> intermediate allocation with a fixed [u8; 26]
stack buffer. The previous implementation created a String from
to_string(), split it into a Vec<String> of 3-digit groups, then
joined with commas - allocating 3+ heap objects per call. The new
implementation writes digits and commas right-to-left into a stack
buffer, producing a single String allocation.

RSS-2 profiling identified this function as allocation site #7,
responsible for 4.9% (9.6 MB) of total allocations at 100K files.
@oferchen
oferchen merged commit 864365e into master Jun 5, 2026
67 checks passed
@oferchen
oferchen deleted the perf/rss-format-decimal-stack-buf branch June 5, 2026 19:28
oferchen added a commit that referenced this pull request Aug 24, 2026
* perf: eliminate heap allocations in format_decimal_bytes

Replace Vec<String> intermediate allocation with a fixed [u8; 26]
stack buffer. The previous implementation created a String from
to_string(), split it into a Vec<String> of 3-digit groups, then
joined with commas - allocating 3+ heap objects per call. The new
implementation writes digits and commas right-to-left into a stack
buffer, producing a single String allocation.

RSS-2 profiling identified this function as allocation site #7,
responsible for 4.9% (9.6 MB) of total allocations at 100K files.

* fix: resolve formatting issues
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant