Skip to content

Commit

Permalink
Reduce garbage from allocations in deprecation logger (elastic#38780)
Browse files Browse the repository at this point in the history
1. Setting length for formatWarning String to avoid AbstractStringBuilder.ensureCapacityInternal calls
2. Adding extra check for parameter array length == 0 to avoid unnecessarily creating StringBuilder in LoggerMessageFormat.format

Helps to narrow the performance gap in throughout for geonames benchmark (elastic#37411) by 3%. For more details: elastic#37530 (comment) 

Relates to elastic#37530
Relates to elastic#37411
Relates to elastic#35754
  • Loading branch information
ebadyano committed Feb 25, 2019
1 parent 3d49523 commit 297155b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,11 @@ public Void run() {
* @return a warning value formatted according to RFC 7234
*/
public static String formatWarning(final String s) {
return WARNING_PREFIX + " " + "\"" + escapeAndEncode(s) + "\"";
// Assume that the common scenario won't have a string to escape and encode.
int length = WARNING_PREFIX.length() + s.length() + 3;
final StringBuilder sb = new StringBuilder(length);
sb.append(WARNING_PREFIX).append(" \"").append(escapeAndEncode(s)).append("\"");
return sb.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static String format(final String prefix, final String messagePattern, fi
if (messagePattern == null) {
return null;
}
if (argArray == null) {
if (argArray == null || argArray.length == 0) {
if (prefix == null) {
return messagePattern;
} else {
Expand Down

0 comments on commit 297155b

Please sign in to comment.