Skip to content

Commit

Permalink
Avoid varargs allocations in min/max calls.
Browse files Browse the repository at this point in the history
I unwittingly introduced these in cl/687381111. I'm not sure why we didn't end up with similar changes in other `primitives` files, but it seems we did not.

I happened to notice while working on cl/689484111.

RELNOTES=n/a
PiperOrigin-RevId: 689775552
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Oct 25, 2024
1 parent 9938818 commit 19fef9b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 6 additions & 4 deletions android/guava/src/com/google/common/primitives/Ints.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import static com.google.common.base.Preconditions.checkElementIndex;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkPositionIndexes;
import static java.lang.Math.max;
import static java.lang.Math.min;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
Expand Down Expand Up @@ -273,9 +271,11 @@ public static int max(int... array) {
* @throws IllegalArgumentException if {@code min > max}
* @since 21.0
*/
// A call to bare "min" or "max" would resolve to our varargs method, not to any static import.
@SuppressWarnings("StaticImportPreferred")
public static int constrainToRange(int value, int min, int max) {
checkArgument(min <= max, "min (%s) must be less than or equal to max (%s)", min, max);
return min(max(value, min), max);
return Math.min(Math.max(value, min), max);
}

/**
Expand Down Expand Up @@ -453,8 +453,10 @@ private enum LexicographicalComparator implements Comparator<int[]> {
INSTANCE;

@Override
// A call to bare "min" or "max" would resolve to our varargs method, not to any static import.
@SuppressWarnings("StaticImportPreferred")
public int compare(int[] left, int[] right) {
int minLength = min(left.length, right.length);
int minLength = Math.min(left.length, right.length);
for (int i = 0; i < minLength; i++) {
int result = Integer.compare(left[i], right[i]);
if (result != 0) {
Expand Down
10 changes: 6 additions & 4 deletions guava/src/com/google/common/primitives/Ints.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import static com.google.common.base.Preconditions.checkElementIndex;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkPositionIndexes;
import static java.lang.Math.max;
import static java.lang.Math.min;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
Expand Down Expand Up @@ -275,9 +273,11 @@ public static int max(int... array) {
* @throws IllegalArgumentException if {@code min > max}
* @since 21.0
*/
// A call to bare "min" or "max" would resolve to our varargs method, not to any static import.
@SuppressWarnings("StaticImportPreferred")
public static int constrainToRange(int value, int min, int max) {
checkArgument(min <= max, "min (%s) must be less than or equal to max (%s)", min, max);
return min(max(value, min), max);
return Math.min(Math.max(value, min), max);
}

/**
Expand Down Expand Up @@ -455,8 +455,10 @@ private enum LexicographicalComparator implements Comparator<int[]> {
INSTANCE;

@Override
// A call to bare "min" or "max" would resolve to our varargs method, not to any static import.
@SuppressWarnings("StaticImportPreferred")
public int compare(int[] left, int[] right) {
int minLength = min(left.length, right.length);
int minLength = Math.min(left.length, right.length);
for (int i = 0; i < minLength; i++) {
int result = Integer.compare(left[i], right[i]);
if (result != 0) {
Expand Down

0 comments on commit 19fef9b

Please sign in to comment.