carterkozak commented on a change in pull request #289: LOG4J2-2644 - Improve
performance of getting location info
URL: https://github.com/apache/logging-log4j2/pull/289#discussion_r298841455
##########
File path:
log4j-api-java9/src/main/java/org/apache/logging/log4j/util/StackLocator.java
##########
@@ -72,15 +78,40 @@ private StackLocator() {
}
public StackTraceElement calcLocation(final String fqcnOfLogger) {
- return stackWalker.walk(
- s -> s.dropWhile(f -> !f.getClassName().equals(fqcnOfLogger))
// drop the top frames until we reach the logger
- .dropWhile(f -> f.getClassName().equals(fqcnOfLogger))
// drop the logger frames
- .findFirst())
- .get()
- .toStackTraceElement();
+ return walker.walk(s ->
s.filter(LOCATOR.get().setFqcn(fqcnOfLogger)).findFirst()).get().toStackTraceElement();
Review comment:
We may want `CallerLocator` to implement
`Function<Stream<StackWalker.StackFrame>, Optional<StackWalker.StackFrame>>` to
avoid the lambda allocation for `s ->
s.filter(LOCATOR.get().setFqcn(fqcnOfLogger)).findFirst()`
```suggestion
return
walker.walk(LOCATOR.get().setFqcn(fqcnOfLogger)).get().toStackTraceElement();
```
using something like this:
```java
static final class CallerLocator implements
Predicate<StackWalker.StackFrame>,
Function<Stream<StackWalker.StackFrame>,
Optional<StackWalker.StackFrame>> {
private String fqcn;
private boolean foundFqcn = false;
private boolean foundLogger = false;
CallerLocator setFqcn(String fqcn) {
this.fqcn = fqcn;
this.foundFqcn = false;
this.foundLogger = false;
return this;
}
@Override
public boolean test(StackWalker.StackFrame t) {
final String className = t.getClassName();
if (!foundLogger) {
if (!foundFqcn) {
foundFqcn = className.equals(fqcn);
return false;
} else if (!className.equals(fqcn)) {
foundLogger = true;
return true;
}
}
return false;
}
@Override
public Optional<StackWalker.StackFrame>
apply(Stream<StackWalker.StackFrame> stackFrameStream) {
return stackFrameStream.filter(this).findFirst();
}
}
```
I'm not sure how much that helps if at all, I haven't looked into how much
allocation StackWalker does internally.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services