Skip to content
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

Optimize ClassUtils#getMostSpecificMethod #30272

Closed
dreis2211 opened this issue Apr 3, 2023 · 2 comments
Closed

Optimize ClassUtils#getMostSpecificMethod #30272

dreis2211 opened this issue Apr 3, 2023 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Milestone

Comments

@dreis2211
Copy link
Contributor

dreis2211 commented Apr 3, 2023

Hi,

I've been profiling some (test-)application context starts lately on Spring Framework 5.x and noticed that with one of our medium-larger applications - which I'm unfortunately not able to share - that during startup almost 100K exceptions are being thrown (caused by some Aspects not in our control). The large majority of this is ending up in ClassUtils.getMostSpecificMethod. The majority of cases seems to be caused by checking for the static factory methods on Map.of, Map.ofEntriesetc. with the target class Properties where we already now that they won't exist. (Properties is checked because Spring Data repositories have an inner bean for the named queries, I believe)

image

I can't imagine this to be super fast or efficient. Could we do anything about that? I'm wondering if it would make sense to exclude static methods here in the first place. Or have some sort of pre-defined list of methods or classes that we know won't find certain things. Or caching the lookups etc.

We haven't been able to upgrade these apps so far to 6.x because the Spring-Security changes are blocking us from a smoother upgrade, but a quick check also revealed that nothing really drastically changed in that regards in the codebase and the risk of checking for methods that are known to not exist still applies. Having that said, there might be a chance that this particular issue for us is reduced with spring-projects/spring-data-commons@712477b but I haven't been able to confirm this just yet.

Anyhow, I'd appreciate if you could take a look if this can be somehow improved.

Cheers,
Christoph

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 3, 2023
@sdeleuze sdeleuze self-assigned this Apr 4, 2023
@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Apr 17, 2023
@sdeleuze sdeleuze added this to the 6.1.0-RC1 milestone Aug 23, 2023
@sdeleuze sdeleuze added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 23, 2023
@sdeleuze sdeleuze modified the milestones: 6.1.0-RC1, 6.0.12 Aug 23, 2023
@sdeleuze sdeleuze added the for: backport-to-5.3.x Marks an issue as a candidate for backport to 5.3.x label Aug 23, 2023
@github-actions github-actions bot added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.3.x Marks an issue as a candidate for backport to 5.3.x labels Aug 23, 2023
@sdeleuze sdeleuze changed the title Reduce NoSuchMethodExceptions being thrown by ClassUtils.getMostSpecificMethod Optimize ClassUtils#getMostSpecificMethod Aug 23, 2023
@sdeleuze
Copy link
Contributor

sdeleuze commented Aug 23, 2023

@dreis2211 After discussing with @jhoeller, I implemented filtering out both static and final methods, and that indeed reduces the number of invocations of the rest of the logic. Since ClassUtils#getMostSpecificMethod and ClassUtils#isOverridable are frequently invoked, I optimised the implementation as well. If you can, please share the impact on your application.

If you want to do more test about further optimizations, https://github.com/spring-projects/spring-petclinic is using a lot those methods as well, so feel free to use it to share more specific feedback.

The fix will be available in Spring Framework 5.3, 6.0 and 6.1.

@dreis2211
Copy link
Contributor Author

@sdeleuze Thanks. I won't be able to share anything in the foreseeable future, at least no in-depth analysis. But I'll see if it has any impact once it's live in one of our applications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants