RFR: 8268829: Provide an optimized way to walk the stack with Class object only [v5]
Brent Christian
bchristi at openjdk.org
Thu Aug 24 22:24:13 UTC 2023
On Thu, 24 Aug 2023 18:44:14 GMT, Mandy Chung <mchung at openjdk.org> wrote:
>> 8268829: Provide an optimized way to walk the stack with Class object only
>>
>> `StackWalker::walk` creates one `StackFrame` per frame and the current implementation
>> allocates one `StackFrameInfo` and one `MemberName` objects per frame. Some frameworks
>> like logging may only interest in the Class object but not the method name nor the BCI,
>> for example, filters out its implementation classes to find the caller class. It's
>> similar to `StackWalker::getCallerClass` but allows a predicate to filter out the element.
>>
>> This PR proposes to add `StackWalker.Kind` enum to specify the information that a stack walker
>> collects. If no method information is needed, a `StackWalker` of `CLASS_INFO` can be used
>> instead and such stack walker will save the overhead (1) to extract the method information
>> and (2) the memory used for the stack walking. In addition, this can also fix
>>
>> - [8311500](https://bugs.openjdk.org/browse/JDK-8311500): StackWalker.getCallerClass() throws UOE if invoked reflectively
>>
>> New factory methods to take a parameter to specify the kind of stack walker to be created are defined.
>> This provides a simple way for existing code, for example logging frameworks, to take advantage of
>> this enhancement with the least change as it can keep the existing function for traversing
>> `StackFrame`s.
>>
>> For example: to find the first caller filtering a known list of implementation class,
>> existing code can call `StackWalker::getInstance(CLASS_INFO, ...)` to create a stack walker instance:
>>
>>
>> StackWalker walker = StackWalker.getInstance(Kind.CLASS_INFO, Option.RETAIN_CLASS_REFERENCE);
>> Optional<Class<?>> callerClass = walker.walk(s ->
>> s.map(StackFrame::getDeclaringClass)
>> .filter(interestingClasses::contains)
>> .findFirst());
>>
>>
>> If method information is accessed on the `StackFrame`s produced by this stack walker such as
>> `StackFrame::getMethodName`, then `UnsupportedOperationException` will be thrown.
>>
>> #### Alternatives Considered
>> One alternative is to provide a new API:
>> `<T> T walkClass(Function<? super Stream<Class<?>, ? extends T> function)`
>>
>> In this case, the caller would need to pass a function that takes a stream
>> of `Class` object instead of `StackFrame`. Existing code would have to
>> modify calls to the `walk` method to `walkClass` and the function body.
>>
>> Another alternative is to add a new `NO_METHOD_INFO` option. Similar to the proposed API,
>>...
>
> Mandy Chung has updated the pull request incrementally with one additional commit since the last revision:
>
> review feedback and javadoc clean up
src/java.base/share/classes/java/lang/StackWalker.java line 47:
> 45: * from the stack frames by default. If the method information is not needed,
> 46: * a {@code StackWalker} collecting {@linkplain Kind#CLASS_INFO class only information}
> 47: * can be used instead via the {@link #getInstance(Kind, Option...)} method.
The description of `Kind` should come after the basics of what StackWalker does:
* <p> The {@link StackWalker#walk walk} method opens a sequential stream
* of {@link StackFrame StackFrame}s for the current thread and then applies
* the given function to walk the {@code StackFrame} stream.
It should be clarified that `METHOD_INFO` returns method information _as well as_ class information.
Overall I would still prefer adding an `Option.OMIT_METHOD_INFO` over a new `Kind` enum.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/15370#discussion_r1304929416
More information about the core-libs-dev
mailing list