GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

@j-baker j-baker commented Aug 5, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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.

All reactions
@j-baker
Fixes mockito#2720 Use StackWalker on Java 9+ to create Locations
0391e26
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 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
@j-baker
Class loadability tests no longer fail on Java 8
a5c573f
@j-baker
j-baker force-pushed the jbaker/j9stackwalker branch from 9c7d62c to a5c573f Compare August 8, 2022 09:46
TimvdLippe
TimvdLippe suggested changes Aug 12, 2022
View reviewed changes

@TimvdLippe TimvdLippe left a comment

Copy link
Copy Markdown
Contributor

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.

All reactions
Comment thread src/main/java/org/mockito/internal/debugging/Java9PlusLocationImpl.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/main/java/org/mockito/internal/debugging/Java9PlusLocationImpl.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/main/java/org/mockito/internal/debugging/LocationFactory.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/test/java/org/mockitousage/internal/debugging/LocationFactoryTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/main/java/org/mockito/internal/debugging/Java9PlusLocationImpl.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/main/java/org/mockito/internal/debugging/Java9PlusLocationImpl.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/main/java/org/mockito/internal/debugging/Java9PlusLocationImpl.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/main/java/org/mockito/internal/debugging/Java9PlusLocationImpl.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/main/java/org/mockito/internal/debugging/LocationFactory.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@j-baker
PR comments
46b4033
j-baker added a commit to j-baker/mockito that referenced this pull request Aug 13, 2022
@j-baker
TypeSafeMatching no longer iterates over class methods inefficiently
b543f46
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 j-baker mentioned this pull request Aug 13, 2022
TypeSafeMatching no longer iterates over class methods inefficiently #2729
Merged
7 of 8 tasks
j-baker added a commit to j-baker/mockito that referenced this pull request Aug 13, 2022
@j-baker
TypeSafeMatching no longer iterates over class methods inefficiently
c7312d3
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 2 commits August 13, 2022 15:39
@j-baker
Refactor test organization
b403d8c
@j-baker
./gradlew check works now
8dc8978
@j-baker
j-baker requested a review from TimvdLippe August 13, 2022 14:43
@TimvdLippe TimvdLippe closed this in #2729 Aug 13, 2022
TimvdLippe pushed a commit that referenced this pull request Aug 13, 2022
@j-baker
Optimize TypeSafeMatching iteration over class methods (#2729)
89698ba
`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
@TimvdLippe TimvdLippe reopened this Aug 13, 2022
@j-baker

j-baker commented Aug 13, 2022

Copy link
Copy Markdown
Contributor Author

Latest commit is green but the status checks are red. Not sure why, but I assume a click of retry will do the trick.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

TimvdLippe
TimvdLippe reviewed Aug 13, 2022
View reviewed changes
Comment thread src/test/java/org/mockitointegration/ClassLoadabilityChecker.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@TimvdLippe

TimvdLippe commented Aug 13, 2022

Copy link
Copy Markdown
Contributor

@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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@j-baker
Linkage Error
60fd2cb
@codecov-commenter

codecov-commenter commented Aug 13, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Codecov Report

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     
Impacted Files Coverage Δ
.../mockito/internal/debugging/Java8LocationImpl.java 80.00% <ø> (ø)
...kito/internal/debugging/Java9PlusLocationImpl.java 68.04% <68.04%> (ø)
...ckito/exceptions/stacktrace/StackTraceCleaner.java 100.00% <100.00%> (ø)
...a/org/mockito/internal/MockedConstructionImpl.java 72.22% <100.00%> (ø)
...in/java/org/mockito/internal/MockedStaticImpl.java 80.51% <100.00%> (ø)
.../internal/creation/bytebuddy/MockMethodAdvice.java 80.51% <100.00%> (+0.11%) ⬆️
...rnal/creation/bytebuddy/MockMethodInterceptor.java 85.18% <100.00%> (ø)
...ockito/internal/creation/proxy/ProxyMockMaker.java 78.57% <100.00%> (+0.31%) ⬆️
...java/org/mockito/internal/debugging/Localized.java 100.00% <100.00%> (ø)
...rg/mockito/internal/debugging/LocationFactory.java 100.00% <100.00%> (ø)
... and 15 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@j-baker

j-baker commented Aug 13, 2022

Copy link
Copy Markdown
Contributor Author

Sigh, you were right. My bad. Anyway, it looks like it'll go green now.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

TimvdLippe
TimvdLippe suggested changes Aug 13, 2022
View reviewed changes

@TimvdLippe TimvdLippe left a comment

Copy link
Copy Markdown
Contributor

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.

All reactions
Comment thread src/main/java/org/mockito/internal/debugging/LocationFactory.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@j-baker
Remove unused constructors
af8454e
TimvdLippe
TimvdLippe approved these changes Aug 13, 2022
View reviewed changes

@TimvdLippe TimvdLippe left a comment

Copy link
Copy Markdown
Contributor

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.

All reactions
@TimvdLippe
TimvdLippe merged commit 73a861f into mockito:main Aug 13, 2022
@GThinesh

GThinesh commented Oct 11, 2022

Copy link
Copy Markdown

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 :) )

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@TimvdLippe

TimvdLippe commented Oct 11, 2022

Copy link
Copy Markdown
Contributor

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
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@j-baker

j-baker commented Oct 11, 2022 via email

Copy link
Copy Markdown
Contributor Author
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
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@GThinesh

GThinesh commented Oct 16, 2022

Copy link
Copy Markdown

@TimvdLippe That is true. Unfortunately we are still stuck with Java 8.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@GThinesh

GThinesh commented Oct 16, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

@j-baker The solution you refer the flag a counter that stops recording after certain number of invocations?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@j-baker

j-baker commented Oct 16, 2022 via email

Copy link
Copy Markdown
Contributor Author
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.
…
On Sun, 16 Oct 2022 at 07:03, Thinesh Ganeshalingam < ***@***.***> wrote: @j-baker <https://github.com/j-baker> The solution you refer the flag a counter that stops recording after certain invocations? — Reply to this email directly, view it on GitHub <#2723 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AAAKMJP4WVCZRPOMCOVB6FTWDOLBNANCNFSM55V5N2YQ> . You are receiving this because you were mentioned.Message ID: ***@***.***>
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@TimvdLippe TimvdLippe mentioned this pull request Oct 19, 2022
CI is crashing with memory issues #2478
Closed
@bursauxa bursauxa mentioned this pull request Dec 14, 2022
mockito-scala does not work with mockito 4.8.0 and above mockito/mockito-scala#480
Closed
@xuwei-k xuwei-k mentioned this pull request Feb 3, 2023
use java.lang.StackWalker if possible scalikejdbc/scalikejdbc#1877
Closed
@DerFrZocker DerFrZocker mentioned this pull request Jul 15, 2023
Move LocationFactory to plugins to allow overriding it #3063
Open
6 of 8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer
@TimvdLippe TimvdLippe TimvdLippe approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

LocationImpl adds performance overheads due to instantiating a stack trace

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@j-baker @TimvdLippe @codecov-commenter @GThinesh
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.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.