Skip to content

Commit

Permalink
bugfix: reduce enum array allocation in QueryLogger (#12478)
Browse files Browse the repository at this point in the history
Motivation:

reduce enum array allocation in QueryLogger

Modifications:

cache QueryLogEntry.values() in a static variable

Result:

Java enum .values() method is invoked exactly once

Additional context:

https://www.gamlor.info/wordpress/2017/08/javas-enum-values-hidden-allocations/
  • Loading branch information
sullis authored Feb 23, 2024
1 parent d7feaaf commit 1004d3b
Showing 1 changed file with 2 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
public class QueryLogger {

private static final Logger LOGGER = LoggerFactory.getLogger(QueryLogger.class);
private static final QueryLogEntry[] QUERY_LOG_ENTRY_VALUES = QueryLogEntry.values();

private final int _maxQueryLengthToLog;
private final RateLimiter _logRateLimiter;
Expand Down Expand Up @@ -82,7 +83,7 @@ public void log(QueryLogParams params) {
}

final StringBuilder queryLogBuilder = new StringBuilder();
for (QueryLogEntry value : QueryLogEntry.values()) {
for (QueryLogEntry value : QUERY_LOG_ENTRY_VALUES) {
value.format(queryLogBuilder, this, params);
queryLogBuilder.append(',');
}
Expand Down

0 comments on commit 1004d3b

Please sign in to comment.