Skip to content

Commit

Permalink
Avoid boxing ints in ConcurrentHashMultiset Preconditions checks
Browse files Browse the repository at this point in the history
The preconditions checks for occurrences are boxing int values for the format
string. This has shown up in profiling as we are using this collection quite
heavily. Switch from preconditions formatting to basic if statement to avoid boxing.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=101702798
  • Loading branch information
michajlo authored and cpovirk committed Aug 27, 2015
1 parent f952844 commit ee7ff4e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 6 additions & 0 deletions guava/src/com/google/common/collect/CollectPreconditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ static int checkNonnegative(int value, String name) {
return value;
}

static void checkPositive(int value, String name) {
if (value <= 0) {
throw new IllegalArgumentException(name + " must be positive but was: " + value);
}
}

/**
* Precondition tester for {@code Iterator.remove()} that throws an exception with a consistent
* error message.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public int add(E element, int occurrences) {
if (occurrences == 0) {
return count(element);
}
checkArgument(occurrences > 0, "Invalid occurrences: %s", occurrences);
CollectPreconditions.checkPositive(occurrences, "occurences");

while (true) {
AtomicInteger existingCounter = Maps.safeGet(countMap, element);
Expand Down Expand Up @@ -285,7 +285,7 @@ public int remove(@Nullable Object element, int occurrences) {
if (occurrences == 0) {
return count(element);
}
checkArgument(occurrences > 0, "Invalid occurrences: %s", occurrences);
CollectPreconditions.checkPositive(occurrences, "occurences");

AtomicInteger existingCounter = Maps.safeGet(countMap, element);
if (existingCounter == null) {
Expand Down Expand Up @@ -325,7 +325,7 @@ public boolean removeExactly(@Nullable Object element, int occurrences) {
if (occurrences == 0) {
return true;
}
checkArgument(occurrences > 0, "Invalid occurrences: %s", occurrences);
CollectPreconditions.checkPositive(occurrences, "occurences");

AtomicInteger existingCounter = Maps.safeGet(countMap, element);
if (existingCounter == null) {
Expand Down

0 comments on commit ee7ff4e

Please sign in to comment.