Skip to content

Performance improvement in applicationContext.getBean() & AutoWired with complex beans [SPR-7988] #12643

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

Closed
spring-projects-issues opened this issue Feb 22, 2011 · 5 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: bulk-closed An outdated, unresolved issue that's closed in bulk as part of a cleaning process

Comments

@spring-projects-issues
Copy link
Collaborator

Jose Luis Freire opened SPR-7988 and commented

In an effort to improve the response time of our application we did some profiling and found out that too much time was being consumed by the Spring Framework itself.

The time was being consumed with applicationContext.getBean(), because in our application we almost don't have session scoped beans and in one request multiple beans can be instantiated.

Our beans are all annotated, with multiple Autowired properties, with several advices to process, and the timing of one bean instantiation was about from 50ms (the most simpler ones) to 150ms (the more complex ones) each.

In many of our pages, the bean instantiation was responsible for more than half of the total processing time.

In our profiling we found that the hotspot was in AopUtils.canApply() method.

What the Spring Framework is doing is evaluating the bean by reflection every time the bean is created, and with complex objects that operation can be really expensive.

The answer is simple, we should cache those results, but due to the complexity of this method we didn't felt comfortable in evaluating what was cacheable, so we choose other targets:

  • AopUtils.getMostSpecificMethod
  • AnnotationClassFilter.matches

We have very good results and we believe that in complex web applications where most of your beans aren't session scoped, the overall performance and responsiveness of the application can be greatly improved (in our tests, 10x).

What we're doing is trading memory for CPU.

To give credit where credit is due, we inspired our solution with Jonathan work (http://jawspeak.com/2010/11/28/spring-slow-autowiring-by-type-getbeannamesfortype-fix-10x-speed-boost-3600ms-to/)

Our test scenario is a simple page that instantiates the same bean 100 times. The total time dropped from more than 5s to about 500ms.

Attached is two screenshots of the profiler:

  1. Profiling without cache
  2. Profiling with cache

I'm also attaching the diffs for review.

By caching these two methods (AopUtils.getMostSpecificMethod(..), and AnnotationClassFilter.matches(..) and not AopUtils.canApply(..) we're probably giving a speed boost to all the framework, but I still believe that we must do some work with AopUtils, but I'll leave that to someone that understands more of the probable consequences.


Affects: 3.0.5

Attachments:

9 votes, 14 watchers

@spring-projects-issues
Copy link
Collaborator Author

Jose Luis Freire commented

Profiler without caching

@spring-projects-issues
Copy link
Collaborator Author

Jose Luis Freire commented

Profiler with caching

@spring-projects-issues
Copy link
Collaborator Author

Jose Luis Freire commented

Attached Diffs

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Mar 21, 2011

Lari Hotari commented

This issue might be related to #11536 .

@spring-projects-issues spring-projects-issues added status: waiting-for-triage An issue we've not yet triaged or decided on type: enhancement A general enhancement in: core Issues in core modules (aop, beans, core, context, expression) and removed type: enhancement A general enhancement labels Jan 11, 2019
@rstoyanchev rstoyanchev added status: bulk-closed An outdated, unresolved issue that's closed in bulk as part of a cleaning process and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 11, 2019
@spring-projects-issues
Copy link
Collaborator Author

Bulk closing outdated, unresolved issues. Please, reopen if still relevant.

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: bulk-closed An outdated, unresolved issue that's closed in bulk as part of a cleaning process
Projects
None yet
Development

No branches or pull requests

2 participants