Skip to content

Commit 19fef9b

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Avoid varargs allocations in min/max calls.
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
1 parent 9938818 commit 19fef9b

File tree

2 files changed

+12
-8
lines changed
  • android/guava/src/com/google/common/primitives
  • guava/src/com/google/common/primitives

2 files changed

+12
-8
lines changed

android/guava/src/com/google/common/primitives/Ints.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import static com.google.common.base.Preconditions.checkElementIndex;
1919
import static com.google.common.base.Preconditions.checkNotNull;
2020
import static com.google.common.base.Preconditions.checkPositionIndexes;
21-
import static java.lang.Math.max;
22-
import static java.lang.Math.min;
2321

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

281281
/**
@@ -453,8 +453,10 @@ private enum LexicographicalComparator implements Comparator<int[]> {
453453
INSTANCE;
454454

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

guava/src/com/google/common/primitives/Ints.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import static com.google.common.base.Preconditions.checkElementIndex;
1919
import static com.google.common.base.Preconditions.checkNotNull;
2020
import static com.google.common.base.Preconditions.checkPositionIndexes;
21-
import static java.lang.Math.max;
22-
import static java.lang.Math.min;
2321

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

283283
/**
@@ -455,8 +455,10 @@ private enum LexicographicalComparator implements Comparator<int[]> {
455455
INSTANCE;
456456

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

0 commit comments

Comments
 (0)