GeezerVison is showing github.com — Fixes #2720: Use StackWalker on Java 9+ to create Locations by j-baker · Pull Request #2723 · mockito/mockito
Conversation
j-baker
commented
Aug 5, 2022
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Fixes #2720.
In terms of memory allocations, this reduces the overall memory
allocations of creating a Location by an order of magnitude in
Java 9, and as compared to Java 8.
The implementation is somewhat involved due to these desires:
- Minimize the amount of work done if the Location is never used. This
is done by not converting the StackFrame into a StackTraceElement,
instead wrapping in an intermediate type. The StackTraceElement conversion
will only occur (internally) if the .getFileName() method is called, which it usually isn't.
- Ensure the implementation is still Serializable. This is ensured with
a .writeReplace method.
- Minimize the number of allocations, which is basically an exercise in
lambda caching.
- Ensuring the old mechanism still works on Java 8.
- MethodHandles are even lower overhead than the reflection, but AnimalSniffer does
not like them since they don't exist in Android.
Presently on Java 9+, on a stack depth of 1000 the old mechanism will allocate
40kB of RAM per call. The new one will allocate 1.5kB of RAM per call,
which is a huge improvement. Even in a very basic JUnit test with no stack depth,
the difference is about 1.5kB of allocation vs 8kB.
This is still sadly not the 'close-to-no-overhead' solution I was
looking for. I therefore also added a system property that can be used
to fully disable Location creation. I'm aware that this is likely not
the right approach given Mockito has plugins and settings - mostly
looking for guidance here given I'm not sure what would be idiomatic
here.
Checklist
- Read the contributing guide
- PR should be motivated, i.e. what does it fix, why, and if relevant how
- If possible / relevant include an example in the description, that could help all readers
including project members to get a better picture of the change
- Avoid other runtime dependencies
- Meaningful commit history ; intention is important please rebase your commit history so that each
commit is meaningful and help the people that will explore a change in 2 years
- The pull request follows coding style
- Mention
Fixes #<issue number> in the description if relevant
- At least one commit should mention
Fixes #<issue number> if relevant
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
In terms of memory allocations, this reduces the overall memory
allocations of creating a location by an order of magnitude in
Java 9, and as compared to Java 8.
The implementation is somewhat involved due to these desires:
- Minimize the amount of work done if the Location is never used. This
is done by not converting the StackFrame into a StackTraceElement,
instead wrapping in an intermediate state. The StackTraceElement conversion
will only occur (internally) if the .getFileName() method is called.
- Ensure the implementation is still Serializable. This is ensured with
a .writeReplace method.
- Minimize the number of allocations, which is basically an exercise in
lambda caching.
- Ensuring the old mechanism still works on Java 8.
Presently on Java 9+, on a stack depth of 1000 the old mechanism will allocate
40kB of RAM per call. The new one will allocate 1.5kB of RAM per call,
which is a huge improvement.
This is still sadly not the 'close-to-no-overhead' solution I was
looking for. I therefore also added a system property that can be used
to fully disable Location creation. I'm aware that this is likely not
the right approach given Mockito has plugins and settings - mostly
looking for guidance here given I'm not sure what would be idiomatic
here.
j-baker
changed the title
Use StackWalker on Java 9+ to create Locations
Fixes #2720: Use StackWalker on Java 9+ to create Locations
Aug 5, 2022
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.
The CI build is failing because of auto-formatting issues. You can run ./gradlew spotlessApply to fix these automatically.
Overall a nice improvement! Mostly some minor comments, but overall looking solid already. Good work 😄
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
j-baker
added a commit
to j-baker/mockito
that referenced
this pull request
Aug 13, 2022
Fixes mockito#2723.
Class.getMethods is an inefficient method call which is being called on
each mock invocation. It ends up constructing new Method objects for
each method on the class, and this can dominate the overall performance
of Mockito mocks. This commit caches the result of the computation.
Once concern is that this change uses some static state. I considered:
- Instance state - based on where this type is constructed it seemed
like it'd be a big imposition on code readability elsewhere.
- Weakly referenced map. Mockito has a type for this, but the
constructor of that type produces a Thread with which to clean up.
Both of these seemed like overkill compared to the overhead expected
in the real world (which should be on the order of a few kilobytes of
RAM at most).
j-baker
added a commit
to j-baker/mockito
that referenced
this pull request
Aug 13, 2022
Fixes mockito#2723.
Class.getMethods is an inefficient method call which is being called on
each mock invocation. It ends up constructing new Method objects for
each method on the class, and this can dominate the overall performance
of Mockito mocks. This commit caches the result of the computation.
Once concern is that this change uses some static state. I considered:
- Instance state - based on where this type is constructed it seemed
like it'd be a big imposition on code readability elsewhere.
- Weakly referenced map. Mockito has a type for this, but the
constructor of that type produces a Thread with which to clean up.
Both of these seemed like overkill compared to the overhead expected
in the real world (which should be on the order of a few kilobytes of
RAM at most).
TimvdLippe
pushed a commit
that referenced
this pull request
Aug 13, 2022
`Class.getMethods` is an inefficient method call which is being called on
each mock invocation. It ends up constructing new `Method` objects for
each method on the class, and this can dominate the overall performance
of Mockito mocks. This commit caches the result of the computation.
Once concern is that this change uses some static state. I considered:
- Instance state - based on where this type is constructed it seemed
like it'd be a big imposition on code readability elsewhere.
- Weakly referenced map. Mockito has a type for this, but the
constructor of that type produces a Thread with which to clean up.
Both of these seemed like overkill compared to the overhead expected
in the real world (which should be on the order of a few kilobytes of
RAM at most).
Fixes #2723
|
Latest commit is green but the status checks are red. Not sure why, but I assume a click of retry will do the trick.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
|
@j-baker The latest commit referenced in this PR is the one from #2729. The JUnit test uses Class.forName, which breaks on Java 8. See my comment for a suggestion on how to fix it.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Merging #2723 (af8454e) into main (95b43e5) will increase coverage by 0.01%.
The diff coverage is 77.69%.
@@ Coverage Diff @@
## main #2723 +/- ##
============================================
+ Coverage 86.05% 86.06% +0.01%
- Complexity 2790 2815 +25
============================================
Files 317 320 +3
Lines 8395 8527 +132
Branches 1048 1052 +4
============================================
+ Hits 7224 7339 +115
- Misses 896 912 +16
- Partials 275 276 +1
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Sigh, you were right. My bad. Anyway, it looks like it'll go green now.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Last nits and then this is ready-to-go 🎉
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks for the hard work and investigation into performance! Much appreciated 😄
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Is there any specific reason to remove the system variable to disable location tracking altogether? My teams is still using Java 8 and this is a major problem for us as well. Locally testing without location reduces the test time from 10+ min to 5 min. (Yes we have a huge code base with large entity graphs :) )
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
I prefer to not ship a flag just for Java 8, which is EOL. Instead, upgrading to Java 9 and above should yield similar benefits. Shipping flags is expensive in the longterm and in this case, I don't think it is worth carrying that cost.
|
GThinesh reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
It’s probably worth saying:
Before my changes, a simple mock call takes 3us.
After, it takes 2us.
If you remove all call site recording, it takes 200ns.
In my case I was considering making it so that it stopped recording call
sites on hotter paths (E.g. after the thousandth call to a mock or
whatever) but it wasn’t a big enough blocker for me.
…
On Tue, 11 Oct 2022 at 08:53, Tim van der Lippe ***@***.***> wrote:
I prefer to not ship a flag just for Java 8, which is EOL. Instead,
upgrading to Java 9 and above should yield similar benefits. Shipping flags
is expensive in the longterm and in this case, I don't think it is worth
carrying that cost.
—
Reply to this email directly, view it on GitHub
< #2723 (comment)>,
or unsubscribe
< https://github.com/notifications/unsubscribe-auth/AAAKMJMN3HJWG3NJPATVORTWCUMIBANCNFSM55V5N2YQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
GThinesh reacted with heart emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@TimvdLippe That is true. Unfortunately we are still stuck with Java 8.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
GThinesh
commented
Oct 16, 2022
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
@j-baker The solution you refer the flag a counter that stops recording after certain number of invocations?
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
I was considering proposing making the location tracking code always stop
recording after enough invocations, the logic being that if you’ve done
thousands of them, knowing the precise location where a single call happens
from is probably less interesting.
There’s possibly also something where if mock invocation recording is
disabled, it might be reasonable to not record stub invocation locations.
…
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
1 more reviewer
Reviewers whose approvals may not affect merge requirements
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|