RFR: 8268829: Provide an optimized way to walk the stack with Class object only [v2]
Mandy Chung
mchung at openjdk.org
Mon Aug 21 21:10:36 UTC 2023
> 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 `Option.NO_METHOD_INFO` new stack walking option.
> If no method information is needed, this option can be used such that the
> 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
>
> Adding `NO_METHOD_TIME` option 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 implementation in traversing
> `StackFrame`s.
>
> For example: to find the first caller filtering a known list of implementation class,
> existing code can just add `NO_METHOD_INFO` in the call to `StackWalker::getInstance`
> to create a stack walker instance:
>
>
> StackWalker walker = StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE, NO_METHOD_INFO);
> 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.
>
> The alternative considered 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.
>
> ### Implementation Details
>
> If `NO_METHOD_NAME` is set, the implementation creates `ClassFrameInfo[]`
> buffer that is filled by the VM during stack walking. `ClassFrameInfo` holds the
> Class instance plus `flags` which indicate if it's caller sensitive or hidden.
> With this change, `StackWalker::getCa...
Mandy Chung has updated the pull request incrementally with one additional commit since the last revision:
clean up
-------------
Changes:
- all: https://git.openjdk.org/jdk/pull/15370/files
- new: https://git.openjdk.org/jdk/pull/15370/files/e9e2b390..87775552
Webrevs:
- full: https://webrevs.openjdk.org/?repo=jdk&pr=15370&range=01
- incr: https://webrevs.openjdk.org/?repo=jdk&pr=15370&range=00-01
Stats: 6 lines in 4 files changed: 1 ins; 2 del; 3 mod
Patch: https://git.openjdk.org/jdk/pull/15370.diff
Fetch: git fetch https://git.openjdk.org/jdk.git pull/15370/head:pull/15370
PR: https://git.openjdk.org/jdk/pull/15370
More information about the core-libs-dev
mailing list