Skip to content

Commit fde333e

Browse files
committed
Special case when num == den
If numerator == denominator, we don't need to do any of this math and can avoid constructing any objects.
1 parent 898b010 commit fde333e

1 file changed

Lines changed: 22 additions & 14 deletions

File tree

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +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().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);
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;
137153
}
138-
139-
subSeconds = numerator
140-
.multiply(RubyTime.TIME_SCALE_BI)
141-
.divide(denominator)
142-
.longValue();
143-
144-
millis = subSeconds / 1_000_000;
145-
nanos = subSeconds % 1_000_000;
146154
} else {
147155
double secs = toDouble(context, secondObj);
148156

0 commit comments

Comments
 (0)