Skip to content

Commit d6d4709

Browse files
authored
Merge pull request jruby#9292 from headius/time_rational_subseconds_tweaks
Reduce BigInteger construction
2 parents b837d29 + fde333e commit d6d4709

1 file changed

Lines changed: 22 additions & 13 deletions

File tree

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,20 +128,29 @@ 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+
RubyInteger num = subSecond.getNumerator();
132+
RubyInteger den = subSecond.getDenominator();
133+
if (num.equals(den)) {
134+
secondsInRational = 1;
135+
} else {
136+
var numerator = num.asBigInteger(context);
137+
var denominator = den.asBigInteger(context);
138+
long subSeconds;
139+
int cmp = numerator.compareTo(denominator);
140+
141+
if (cmp > 0) {
142+
secondsInRational = numerator.divide(denominator).intValue();
143+
numerator = numerator.mod(denominator);
144+
}
145+
146+
subSeconds = numerator
147+
.multiply(RubyTime.TIME_SCALE_BI)
148+
.divide(denominator)
149+
.longValue();
150+
151+
millis = subSeconds / 1_000_000;
152+
nanos = subSeconds % 1_000_000;
136153
}
137-
138-
long subSeconds = BigInteger.valueOf(numerator)
139-
.multiply(RubyTime.TIME_SCALE_BI)
140-
.divide(BigInteger.valueOf(denominator))
141-
.longValue();
142-
143-
millis = subSeconds / 1_000_000;
144-
nanos = subSeconds % 1_000_000;
145154
} else {
146155
double secs = toDouble(context, secondObj);
147156

0 commit comments

Comments
 (0)