Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Starlark: optimize StarlarkInt.Big comparison to StarlarkInt.Int{32,64} #12638

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/main/java/net/starlark/java/eval/StarlarkInt.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,17 +436,30 @@ public int compareTo(StarlarkInt x) {

/** Returns a value whose signum is equal to x - y. */
public static int compare(StarlarkInt x, StarlarkInt y) {
if (x instanceof Int32 && y instanceof Int32) {
return Integer.compare(((Int32) x).v, ((Int32) y).v);
}

try {
return Long.compare(x.toLongFast(), y.toLongFast());
long xl = x.toLongFast();
try {
long yl = y.toLongFast();
// both x and y are within long range
return Long.compare(xl, yl);
} catch (Overflow unused) {
// minlong <= x <= maxlong
// y is Big: y < minlong || y > maxlong
// so compare(x, y) == -y.signum()
return -((Big) y).v.signum();
}
} catch (Overflow unused) {
/* fall through */
try {
y.toLongFast();
// x is Big: x < minlong || x > maxlong
// minlong <= y <= maxlong
// so compare(x, y) == x.signum()
return ((Big) x).v.signum();
} catch (Overflow unused2) {
// both x and y are Big
return ((Big) x).v.compareTo(((Big) y).v);
}
}

return x.toBigInteger().compareTo(y.toBigInteger());
}

/** Returns x + y. */
Expand Down