Reduce garbage from allocations in deprecation logger (#38780) (#39370)

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: https://github.com/elastic/elasticsearch/issues/37530#issuecomment-462758384 

Relates to #37530
Relates to #37411
Relates to #35754
This commit is contained in:
Evgenia Badyanova 2019-02-25 16:23:22 -05:00 committed by GitHub
parent 5c7dd6f0ee
commit 1ed3407930
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 2 deletions

View File

@ -259,7 +259,11 @@ public class DeprecationLogger {
* @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();
}
/**

View File

@ -40,7 +40,7 @@ public class LoggerMessageFormat {
if (messagePattern == null) {
return null;
}
if (argArray == null) {
if (argArray == null || argArray.length == 0) {
if (prefix == null) {
return messagePattern;
} else {