Skip to content

perf: add a stack-allocated to_base_be#490

Merged
prestwich merged 1 commit intorecmo:mainfrom
DaniPopes:no-alloc-fmt
Jul 21, 2025
Merged

perf: add a stack-allocated to_base_be#490
prestwich merged 1 commit intorecmo:mainfrom
DaniPopes:no-alloc-fmt

Conversation

@DaniPopes
Copy link
Contributor

@DaniPopes DaniPopes commented Jul 6, 2025

Adds to_base_be_2, which allocates the result of to_base_le on the stack instead of the heap, and use it in fmt.

This is faster than to_base_be (#488), which does 2 divisions per iteration instead of 1. However it only works for the biggest power of the base, so it can't replace to_base_be entirely as it would be a breaking change.

Based on #488.

@codspeed-hq
Copy link

codspeed-hq bot commented Jul 6, 2025

CodSpeed Performance Report

Merging #490 will improve performances by 68.04%

Comparing DaniPopes:no-alloc-fmt (5de0b88) with main (7bf292e)

Summary

⚡ 24 improvements
✅ 271 untouched benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
fmt/binary/128 8.7 ms 7.6 ms +14.18%
fmt/binary/192 12.5 ms 10.9 ms +14.94%
fmt/binary/256 17.6 ms 14 ms +26.15%
fmt/binary/384 25.8 ms 20.3 ms +26.98%
fmt/binary/512 37.2 ms 26.6 ms +39.84%
fmt/binary/64 5.1 ms 4.2 ms +21.16%
fmt/decimal/128 5.7 ms 4.8 ms +18.85%
fmt/decimal/192 7.9 ms 6.8 ms +16.36%
fmt/decimal/256 11.5 ms 8.9 ms +29.38%
fmt/decimal/384 16.5 ms 13.4 ms +22.94%
fmt/decimal/512 24.8 ms 18.6 ms +33.31%
fmt/decimal/64 3.6 ms 2.8 ms +31.69%
fmt/hex/128 6.4 ms 5.3 ms +20.84%
fmt/hex/192 8.7 ms 7.1 ms +23.34%
fmt/hex/256 12.5 ms 8.8 ms +42.93%
fmt/hex/384 17.8 ms 12.4 ms +44.37%
fmt/hex/512 26.6 ms 16 ms +66.31%
fmt/hex/64 4.3 ms 3.5 ms +23.12%
fmt/octal/128 6 ms 4.9 ms +21.93%
fmt/octal/192 8.4 ms 6.7 ms +24.08%
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

@DaniPopes DaniPopes force-pushed the no-alloc-fmt branch 4 times, most recently from 4dadf5a to 25c818f Compare July 7, 2025 17:26
@DaniPopes DaniPopes marked this pull request as ready for review July 7, 2025 18:03
@DaniPopes DaniPopes requested a review from prestwich as a code owner July 7, 2025 18:03
@DaniPopes DaniPopes marked this pull request as draft July 17, 2025 10:26
@DaniPopes DaniPopes force-pushed the no-alloc-fmt branch 3 times, most recently from f301658 to 6d90132 Compare July 18, 2025 07:06
@DaniPopes DaniPopes marked this pull request as ready for review July 18, 2025 07:06
/// Panics if the base is less than 2.
#[inline]
#[track_caller]
pub fn to_base_be_2(&self, base: u64) -> impl Iterator<Item = u64> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

should this be public?

Copy link
Collaborator

@prestwich prestwich Jul 20, 2025

Choose a reason for hiding this comment

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

I'm mostly fine with it being public, but can we add some more docs?

  • what exactly would be breaking (an example would be great
  • when it is recommended to use _2 vs to_base_be
  • a reference to _2 in the docs for to_base_be describing when it should be used

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

@prestwich prestwich merged commit 2973b98 into recmo:main Jul 21, 2025
19 checks passed
@DaniPopes DaniPopes deleted the no-alloc-fmt branch July 22, 2025 07:00
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