Commit b66add1
authored
Optimize BigDecimal#to_s (#519)
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
```1 parent 219cb2e commit b66add1
1 file changed
+31
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5449 | 5449 | | |
5450 | 5450 | | |
5451 | 5451 | | |
| 5452 | + | |
| 5453 | + | |
| 5454 | + | |
| 5455 | + | |
| 5456 | + | |
| 5457 | + | |
| 5458 | + | |
| 5459 | + | |
| 5460 | + | |
| 5461 | + | |
5452 | 5462 | | |
5453 | 5463 | | |
5454 | 5464 | | |
5455 | 5465 | | |
| 5466 | + | |
| 5467 | + | |
5456 | 5468 | | |
5457 | 5469 | | |
5458 | 5470 | | |
| |||
5492 | 5504 | | |
5493 | 5505 | | |
5494 | 5506 | | |
5495 | | - | |
5496 | | - | |
| 5507 | + | |
5497 | 5508 | | |
| 5509 | + | |
5498 | 5510 | | |
| 5511 | + | |
5499 | 5512 | | |
5500 | 5513 | | |
5501 | 5514 | | |
| |||
5514 | 5527 | | |
5515 | 5528 | | |
5516 | 5529 | | |
5517 | | - | |
| 5530 | + | |
| 5531 | + | |
| 5532 | + | |
| 5533 | + | |
| 5534 | + | |
| 5535 | + | |
| 5536 | + | |
| 5537 | + | |
| 5538 | + | |
| 5539 | + | |
| 5540 | + | |
| 5541 | + | |
| 5542 | + | |
| 5543 | + | |
| 5544 | + | |
| 5545 | + | |
5518 | 5546 | | |
5519 | 5547 | | |
5520 | 5548 | | |
| |||
0 commit comments