Skip to content

Use '0'+n for converting single digit to char#521

Merged
tompng merged 1 commit intoruby:masterfrom
tompng:single_digit_to_char
Apr 7, 2026
Merged

Use '0'+n for converting single digit to char#521
tompng merged 1 commit intoruby:masterfrom
tompng:single_digit_to_char

Conversation

@tompng
Copy link
Copy Markdown
Member

@tompng tompng commented Apr 6, 2026

In both case (snprintf, Vp_ulltoa), nn is always a single digit number in range 0..9.

@tompng
Copy link
Copy Markdown
Member Author

tompng commented Apr 6, 2026

Benchmark

require "bundler/inline"

gemfile do
  source 'https://rubygems.org'
  gem "benchmark-ips"
  gem "bigdecimal" # Use `-Ilib` to load the local version of bigdecimal
end

small = BigDecimal("29.99")
large = BigDecimal("32423094234234.23423432")
huge = BigDecimal('9' * 1000 + '.' + '9' * 1000)
Benchmark.ips do |x|
  x.report("22.99") { small.to_s }
  x.report("large") { large.to_s }
  x.report("huge") { huge.to_s }
end

Before

ruby 4.0.0 (2025-12-25 revision 553f1675f3) +YJIT +MN +PRISM [arm64-darwin24]
Warming up --------------------------------------
               22.99   809.036k i/100ms
               large   670.072k i/100ms
                huge    19.506k i/100ms
Calculating -------------------------------------
               22.99      9.116M (± 1.5%) i/s  (109.70 ns/i) -     46.115M in   5.059908s
               large      7.640M (± 1.3%) i/s  (130.88 ns/i) -     38.864M in   5.087452s
                huge    199.845k (± 1.8%) i/s    (5.00 μs/i) -      1.014M in   5.077167s

After

ruby 4.0.0 (2025-12-25 revision 553f1675f3) +YJIT +MN +PRISM [arm64-darwin24]
Warming up --------------------------------------
               22.99   995.850k i/100ms
               large   980.671k i/100ms
                huge    70.413k i/100ms
Calculating -------------------------------------
               22.99     10.766M (± 2.4%) i/s   (92.88 ns/i) -     54.772M in   5.090304s
               large     11.430M (± 1.7%) i/s   (87.49 ns/i) -     57.860M in   5.063603s
                huge    700.051k (± 3.1%) i/s    (1.43 μs/i) -      3.521M in   5.033985s

@tompng tompng merged commit 72937b7 into ruby:master Apr 7, 2026
84 checks passed
@tompng tompng deleted the single_digit_to_char branch April 7, 2026 12:10
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