Skip to content

Commit 898b010

Browse files
committed
Reduce BigInteger construction
The numerator and denominator need to be BigInteger, so request them as such and reuse those instances. This reduces the number of BigIntegers constructed to four in the case where they are both already RubyBignum instances and numerator is less than denominator, and eight in the worst case where neither of them are RubyBignum instances and numerator is greater than denominator. Previously, the best case was six BigInteger (extra from numerator and denominator being recreated).
1 parent 148f885 commit 898b010

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

core/src/main/java/org/jruby/util/time/TimeArgs.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,17 @@ public void initializeTime(ThreadContext context, RubyTime time, DateTimeZone dt
128128
if (secondObj instanceof RubyRational subSecond) {
129129
if (subSecond.isNegativeNumber(context)) throw argumentError(context, "argument out of range");
130130

131-
var numerator = subSecond.getNumerator().asLong(context);
132-
var denominator = subSecond.getDenominator().asLong(context);
133-
if (numerator >= denominator) {
134-
secondsInRational = (int) (numerator / denominator);
135-
numerator = numerator % denominator;
131+
var numerator = subSecond.getNumerator().asBigInteger(context);
132+
var denominator = subSecond.getDenominator().asBigInteger(context);
133+
long subSeconds;
134+
if (numerator.compareTo(denominator) >= 0) {
135+
secondsInRational = numerator.divide(denominator).intValue();
136+
numerator = numerator.mod(denominator);
136137
}
137138

138-
long subSeconds = BigInteger.valueOf(numerator)
139+
subSeconds = numerator
139140
.multiply(RubyTime.TIME_SCALE_BI)
140-
.divide(BigInteger.valueOf(denominator))
141+
.divide(denominator)
141142
.longValue();
142143

143144
millis = subSeconds / 1_000_000;

0 commit comments

Comments
 (0)