Merged
Conversation
Most of the time was spent in two calls to `snprintf`, by a
simpler integer to ASCII function, it can be made several time faster.
The code is largely adapted from an earlier version of ruby/json.
ruby/json now use a much more optimized algorithm, but there are
licensing consideration so not sure it's worth optimizing that much.
Before:
```
ruby 4.0.2 (2026-03-17 revision d3da9fec82) +YJIT +PRISM [arm64-darwin25]
Warming up --------------------------------------
22.99 408.215k i/100ms
large 230.802k i/100ms
Calculating -------------------------------------
22.99 4.214M (± 2.2%) i/s (237.32 ns/i) - 21.227M in 5.040152s
large 2.384M (± 2.6%) i/s (419.45 ns/i) - 12.002M in 5.037698s
```
After:
```
ruby 4.0.2 (2026-03-17 revision d3da9fec82) +YJIT +PRISM [arm64-darwin25]
Warming up --------------------------------------
22.99 1.026M i/100ms
large 846.057k i/100ms
Calculating -------------------------------------
22.99 10.882M (± 0.8%) i/s (91.89 ns/i) - 55.426M in 5.093603s
large 9.094M (± 1.0%) i/s (109.97 ns/i) - 45.687M in 5.024549s
```
```ruby
require "bundler/inline"
gemfile do
source 'https://rubygems.org'
gem "benchmark-ips"
gem "bigdecimal", path: "/Users/byroot/src/github.com/byroot/bigdecimal"
end
small = BigDecimal("29.99")
large = BigDecimal("32423094234234.23423432")
Benchmark.ips do |x|
x.report("22.99") { small.to_s }
x.report("large") { large.to_s }
end
```
Member
|
@byroot Thanks a lot. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Most of the time was spent in two calls to
snprintf, by a simpler integer to ASCII function, it can be made several time faster.The code is largely adapted from an earlier version of ruby/json. ruby/json now use a much more optimized algorithm, but there are licensing consideration so not sure it's worth optimizing that much.
Before:
After: