Skip to content

Commit

Permalink
add a checkArgument that avoids varargs (#465)
Browse files Browse the repository at this point in the history
Avoids varargs and the allocation overhead of creating a string array.

During application profiling found that some allocation can be avoided
if we explicitly invoke the checkArgument method without varargs

Allocation profiling via async-profiler:

```
--- 1451114625215846968 bytes (21.40%), 10371 samples
  [ 0] java.lang.String[]
  [ 1] com.netflix.servo.tag.BasicTag.checkNotEmpty
  [ 2] com.netflix.servo.tag.BasicTag.<init>
  [ 3] com.netflix.servo.tag.Tags.newTag
  [ 4] com.netflix.servo.monitor.MonitorConfig$Builder.withTag
```
  • Loading branch information
joshgord authored and brharrington committed Jul 4, 2019
1 parent 3252cd4 commit 6480bc4
Showing 1 changed file with 22 additions and 1 deletion.
Expand Up @@ -43,7 +43,28 @@ public static <T> T checkNotNull(T obj, String name) {
* @throws IllegalArgumentException if {@code expression} is false
*/
public static void checkArgument(boolean expression, String errorMessage) {
checkArgument(expression, errorMessage, null);
checkArgument(expression, errorMessage, (String) null);
}

/**
* Ensures the truth of an expression involving one or more parameters to the
* calling method.
*
*
* @param expression a boolean expression
* @param errorMessage the error message that can be a formattable string
* @param arg argument if using a formatted string
* @throws IllegalArgumentException if {@code expression} is false
*/
public static void checkArgument(boolean expression, String errorMessage, String arg) {
if (!expression) {
if (arg != null) {
String message = String.format(errorMessage, arg);
throw new IllegalArgumentException(message);
} else {
throw new IllegalArgumentException(errorMessage);
}
}
}

/**
Expand Down

0 comments on commit 6480bc4

Please sign in to comment.