Skip to content

Commit 9d54e74

Browse files
authored
Reduce garbage from allocations in deprecation logger (#38780)
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 (#37411) by 3%. For more details: #37530 (comment) Relates to #37530 Relates to #37411 Relates to #35754
1 parent 256b1cb commit 9d54e74

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

server/src/main/java/org/elasticsearch/common/logging/DeprecationLogger.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,11 @@ public Void run() {
259259
* @return a warning value formatted according to RFC 7234
260260
*/
261261
public static String formatWarning(final String s) {
262-
return WARNING_PREFIX + " " + "\"" + escapeAndEncode(s) + "\"";
262+
// Assume that the common scenario won't have a string to escape and encode.
263+
int length = WARNING_PREFIX.length() + s.length() + 3;
264+
final StringBuilder sb = new StringBuilder(length);
265+
sb.append(WARNING_PREFIX).append(" \"").append(escapeAndEncode(s)).append("\"");
266+
return sb.toString();
263267
}
264268

265269
/**

server/src/main/java/org/elasticsearch/common/logging/LoggerMessageFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static String format(final String prefix, final String messagePattern, fi
4040
if (messagePattern == null) {
4141
return null;
4242
}
43-
if (argArray == null) {
43+
if (argArray == null || argArray.length == 0) {
4444
if (prefix == null) {
4545
return messagePattern;
4646
} else {

0 commit comments

Comments
 (0)