From 6480bc4aedfd0b03dcfa16fb41ce76d46882eda0 Mon Sep 17 00:00:00 2001 From: joshgord Date: Thu, 4 Jul 2019 12:31:09 -0700 Subject: [PATCH] add a checkArgument that avoids varargs (#465) 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. [ 3] com.netflix.servo.tag.Tags.newTag [ 4] com.netflix.servo.monitor.MonitorConfig$Builder.withTag ``` --- .../com/netflix/servo/util/Preconditions.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/servo-core/src/main/java/com/netflix/servo/util/Preconditions.java b/servo-core/src/main/java/com/netflix/servo/util/Preconditions.java index 63d914e5..622cd002 100644 --- a/servo-core/src/main/java/com/netflix/servo/util/Preconditions.java +++ b/servo-core/src/main/java/com/netflix/servo/util/Preconditions.java @@ -43,7 +43,28 @@ public static 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); + } + } } /**