fast way to infer caller
Remi Forax
forax at univ-mlv.fr
Wed Apr 6 15:52:01 UTC 2022
----- Original Message -----
> From: "Ceki Gülcü" <ceki at qos.ch>
> To: "core-libs-dev" <core-libs-dev at 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
More information about the core-libs-dev
mailing list