----- Original Message ----- > From: "Ceki Gülcü" <c...@qos.ch> > To: "core-libs-dev" <core-libs-dev@openjdk.java.net> > Sent: Wednesday, April 6, 2022 5:30:51 PM > Subject: fast way to infer caller
> Hello, Hello, > > As you are probably aware, one of the important primitives used in > logging libraries is inferring the caller of a given logging statement. > The current common practice is to create a throwable and process its > stack trace. This is rather wasteful and rather slow. As an alternative, > I have tried using the StackWalker API to infer the caller but was > unsatisfied with the performance. > > MethodHandles.lookup().lookupClass() looks very promising except that > there is no way to specify the depth. > > I am looking for a method to obtain the Nth caller at a cost of around > 100 to 200 nanoseconds of CPU time. Do you think the JDK could cater > for this use case? We have designed the StackWalker with that in mind https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/StackWalker.html see the discussion on StackWalker.getCallerClass() https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/StackWalker.html#getCallerClass() > > -- > Ceki Gülcü > > Sponsoring SLF4J/logback/reload4j at https://github.com/sponsors/qos-ch regards, Rémi