-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8268829: Provide an optimized way to walk the stack with Class object only #15370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
👋 Welcome back mchung! A progress list of the required criteria for merging this PR into |
|
@mlchung The following labels will be automatically applied to this pull request:
When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command. |
|
Hi @mlchung, thanks for making a comment in an OpenJDK project! All comments and discussions in the OpenJDK Community must be made available under the OpenJDK Terms of Use. If you already are an OpenJDK Author, Committer or Reviewer, please click here to open a new issue so that we can record that fact. Please Use "Add GitHub user mlchung" for the summary. If you are not an OpenJDK Author, Committer or Reviewer, simply check the box below to accept the OpenJDK Terms of Use for your comments.
Your comment will be automatically restored once you have accepted the OpenJDK Terms of Use. |
|
Hi @mlchung, thanks for making a comment in an OpenJDK project! All comments and discussions in the OpenJDK Community must be made available under the OpenJDK Terms of Use. If you already are an OpenJDK Author, Committer or Reviewer, please click here to open a new issue so that we can record that fact. Please Use "Add GitHub user mlchung" for the summary. If you are not an OpenJDK Author, Committer or Reviewer, simply check the box below to accept the OpenJDK Terms of Use for your comments.
Your comment will be automatically restored once you have accepted the OpenJDK Terms of Use. |
|
@mlchung |
|
@mlchung |
|
Hi @mlchung, thanks for making a comment in an OpenJDK project! All comments and discussions in the OpenJDK Community must be made available under the OpenJDK Terms of Use. If you already are an OpenJDK Author, Committer or Reviewer, please click here to open a new issue so that we can record that fact. Please Use "Add GitHub user mlchung" for the summary. If you are not an OpenJDK Author, Committer or Reviewer, simply check the box below to accept the OpenJDK Terms of Use for your comments.
Your comment will be automatically restored once you have accepted the OpenJDK Terms of Use. |
|
Hi @mlchung, thanks for making a comment in an OpenJDK project! All comments and discussions in the OpenJDK Community must be made available under the OpenJDK Terms of Use. If you already are an OpenJDK Author, Committer or Reviewer, please click here to open a new issue so that we can record that fact. Please Use "Add GitHub user mlchung" for the summary. If you are not an OpenJDK Author, Committer or Reviewer, simply check the box below to accept the OpenJDK Terms of Use for your comments.
Your comment will be automatically restored once you have accepted the OpenJDK Terms of Use. |
It would be easy to add in the future if needed, say if more options were introduced and the common case requires 2 or more options. |
|
The API changes in the the current update (111661b) look good. |
bchristi-git
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. I like DROP_METHOD_INFO. I have just a few minor comments.
|
@mlchung This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be: You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 84 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. ➡️ To integrate this PR with the above commit message to the |
bchristi-git
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great - thanks!
| static StackWalker walker(String name) { | ||
| return switch (name) { | ||
| case "default" -> WALKER; | ||
| case "class+method" -> WALKER; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't you need to also change "default" into "class+method" in the @Param({"default", "class_only"}) annotations below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mlchung, thanks for making a comment in an OpenJDK project!
All comments and discussions in the OpenJDK Community must be made available under the OpenJDK Terms of Use. If you already are an OpenJDK Author, Committer or Reviewer, please click here to open a new issue so that we can record that fact. Please Use "Add GitHub user mlchung" for the summary.
If you are not an OpenJDK Author, Committer or Reviewer, simply check the box below to accept the OpenJDK Terms of Use for your comments.
- I agree to the OpenJDK Terms of Use for all comments I make in a project in the OpenJDK GitHub organization.
Your comment will be automatically restored once you have accepted the OpenJDK Terms of Use.
|
Hi @mlchung, thanks for making a comment in an OpenJDK project! All comments and discussions in the OpenJDK Community must be made available under the OpenJDK Terms of Use. If you already are an OpenJDK Author, Committer or Reviewer, please click here to open a new issue so that we can record that fact. Please Use "Add GitHub user mlchung" for the summary. If you are not an OpenJDK Author, Committer or Reviewer, simply check the box below to accept the OpenJDK Terms of Use for your comments.
Your comment will be automatically restored once you have accepted the OpenJDK Terms of Use. |
|
Going to push as commit 111ecdb.
Your commit was automatically rebased without conflicts. |
| // type is not a MethodType yet. Convert it thread-safely. | ||
| synchronized (this) { | ||
| if (type instanceof String sig) { | ||
| type = JLIA.getMethodType(sig, declaringClass().getClassLoader()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe there should be a return here:
| type = JLIA.getMethodType(sig, declaringClass().getClassLoader()); | |
| return type = JLIA.getMethodType(sig, declaringClass().getClassLoader()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type is of type Object, don't think this compiles as the result type of = is the type variable's type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mlchung, thanks for making a comment in an OpenJDK project!
All comments and discussions in the OpenJDK Community must be made available under the OpenJDK Terms of Use. If you already are an OpenJDK Author, Committer or Reviewer, please click here to open a new issue so that we can record that fact. Please Use "Add GitHub user mlchung" for the summary.
If you are not an OpenJDK Author, Committer or Reviewer, simply check the box below to accept the OpenJDK Terms of Use for your comments.
- I agree to the OpenJDK Terms of Use for all comments I make in a project in the OpenJDK GitHub organization.
Your comment will be automatically restored once you have accepted the OpenJDK Terms of Use.
| * | ||
| * @see StackWalker.Option#DROP_METHOD_INFO | ||
| */ | ||
| class ClassFrameInfo implements StackFrame { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class can be sealed:
| class ClassFrameInfo implements StackFrame { | |
| sealed class ClassFrameInfo implements StackFrame permits StackFrameInfo { |
|
The value of flags can only be 0 or 0x08000000, so flags & MEMBER_INFO_FLAGS in isHidden and isCallerSensitive methods are always false ClassFrameInfo(StackWalker walker) {
this.flags = walker.retainClassRef ? RETAIN_CLASS_REF_BIT : 0;
}
private static final int MEMBER_INFO_FLAGS = 0x00FFFFFF;
private static final int RETAIN_CLASS_REF_BIT = 0x08000000; // retainClassRefThe flags field is only assigned in the ClassFrameInfo constructor, so flags can be set to final, and the value here will only be 0 or RETAIN_CLASS_REF_BIT (0x08000000). So boolean isCallerSensitive() {
return JLIA.isCallerSensitive(flags & MEMBER_INFO_FLAGS);
}
boolean isHidden() {
return JLIA.isHiddenMember(flags & MEMBER_INFO_FLAGS);
} |
|
Sorry, I misread it. There is a comment in flags: |
8268829: Provide an optimized way to walk the stack with Class object only
StackWalker::walkcreates oneStackFrameper frame and the current implementationallocates one
StackFrameInfoand oneMemberNameobjects per frame. Some frameworkslike 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::getCallerClassbut allows a predicate to filter out the element.This PR proposes to add
Option::DROP_METHOD_INFOenum that requests to drop the method information. If no method information is needed, aStackWalkerwithDROP_METHOD_INFOcan be used instead and such stack walker will save the overhead of extracting the method information
and the memory used for the stack walking.
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
StackFrames.For example: to find the first caller filtering a known list of implementation class,
existing code can create a stack walker instance with
DROP_METHOD_INFOoption:If method information is accessed on the
StackFrames produced by this stack walker such asStackFrame::getMethodName, thenUnsupportedOperationExceptionwill be thrown.Javadoc & specdiff
https://cr.openjdk.org/~mchung/api/java.base/java/lang/StackWalker.html
https://cr.openjdk.org/~mchung/jdk22/specdiff/overview-summary.html
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
Classobject instead ofStackFrame. Existing code would have tomodify calls to the
walkmethod towalkClassand the function body.Implementation Details
A
StackWalkerconfigured withDROP_METHOD_INFOoption createsClassFrameInfo[]buffer that is filled by the VM during stack walking.
StackFrameInfois updated to bea subclass of
ClassFrameInfoand storesResolvedMethodNameand all other method information.ClassFrameInfoholds theClassinstance plusflagswhich indicate if it's hidden and caller-sensitive.Hence
StackWalker::getCallerClasscan take advantage of this change to fixJDK-8311500: StackWalker.getCallerClass() throws UOE if invoked reflectively. StackWalker::getCallerClass
will createClassFrameInfo[]` buffer instead and do the check in Java instead of in the VM.Performance
The microbenchmark shows that the runtime performance of stack walking with method information
is 15-31% faster than the old implementation. A
StackWalkerwithDROP_METHOD_INFOis about 21-43% faster compared to the
StackWalkercollecting the method information.The memory usage of the data structure (shown by -XX:PrintFieldLayout):
ClassFrameInfoStackFrameInfoResolvedMethodNameMemberNameStackFrameInfo(old)The existing implementation allocates a total of 104 bytes for each frame
(
StackFrameInfo+MemberName+ResolvedMethodName).MemberNameis designedto represent methods and fields and include additional fields that stack walker does not needed.
So
StackFrameInfois modified to storeResolvedMethodNamedirectly and include additionalfields to keep the name and type of the method that will be expanded when such info is requested.
The new implementation allocates a total of 72 bytes for each frame (
StackFrameInfo+ResolvedMethodName)which saves 30% of the buffer memory when collecting method information.
For a stack walker collecting just class information, 24 bytes is allocated for each frame
as only
ClassFrameInfois needed. In addition, it saves the overhead in creatingResolvedMethodNameobject in the VM.
StackWalker::getCallerClassThe new implementation of
StackWalker::getCallerClassis about ~3% degradation.The old implementation creates a
Classarray of size 8 whereas the new implementationwhich creates a
ClassFrameInfoarray of size 8 initialized with 6ClassFrameInfoelements.The new implementation of
getCallerClassadds 144 bytes more, which is insignificant.The benefit of this is to do the filtering in Java rather than doing in VM.
Progress
Issues
Reviewers
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/15370/head:pull/15370$ git checkout pull/15370Update a local copy of the PR:
$ git checkout pull/15370$ git pull https://git.openjdk.org/jdk.git pull/15370/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 15370View PR using the GUI difftool:
$ git pr show -t 15370Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/15370.diff
Webrev
Link to Webrev Comment