Project

General

Profile

Actions

Feature #21650

closed

Performance regression: Rational#floor(ndigits) extremely slow for huge ndigits in Ruby 3.4 (ok in 3.2)

Feature #21650: Performance regression: Rational#floor(ndigits) extremely slow for huge ndigits in Ruby 3.4 (ok in 3.2)

Added by koilanetroc (Oleg Tolmashov) 1 day ago. Updated about 22 hours ago.

Status:
Feedback
Assignee:
-
Target version:
-
[ruby-core:123558]

Description

Summary

Rational#floor(ndigits) with a very large positive ndigits takes tens of seconds in Ruby 3.4, while it returns essentially instantly in Ruby 3.2. Reproducible on macOS and Linux. Looks like a missing fast‑path for rationals whose decimal expansion terminates.

Steps to reproduce

require "benchmark"

puts RUBY_DESCRIPTION
t = Benchmark.realtime { (2 ** -3).floor(2 ** 31) }
puts "elapsed: #{t.round(3)}s"

Also reproduces with the explicit rational form:

Benchmark.realtime { Rational(1, 8).floor(2 ** 31) }

Results on my machine

Ruby 3.2.8:

ruby 3.2.8 (2025-03-26 revision 13f495dc2c) [arm64-darwin24]
slow_math.rb:4: warning: in a**b, b may be too big
elapsed: 0.0s

Ruby 3.4.7:

ruby 3.4.7 (2025-10-08 revision 7a5688e2a2) +PRISM [arm64-darwin24]
elapsed: 39.214s

Actual behavior

On Ruby 3.4.x this call takes ~tens of seconds (e.g., ~40s on my machine), consuming CPU. Same on macOS and Linux.

Expected behavior

The method should return quickly ышьшдфкдн фы


Related issues 1 (0 open1 closed)

Related to Ruby - Feature #20811: `warning: in a**b, b may be too big` is really helpful?ClosedActions
Actions

Also available in: PDF Atom