Skip to content

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

Closed

Description

@spring-projects-issues

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

Activity

  1. spring-projects-issues commented on Feb 22, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Jose Luis Freire commented

    Profiler without caching

  2. spring-projects-issues commented on Feb 22, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Jose Luis Freire commented

    Profiler with caching

  3. spring-projects-issues commented on Feb 22, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Jose Luis Freire commented

    Attached Diffs

  4. spring-projects-issues commented on Mar 21, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Lari Hotari commented

    This issue might be related to #11536 .

  5. added
    status: waiting-for-triage
    type: enhancement
    in: core
    and removed
    type: enhancement
    on Jan 11, 2019
  6. added
    status: bulk-closed
    and removed
    status: waiting-for-triage
    on Jan 11, 2019
  7. spring-projects-issues commented on Jan 12, 2019

    @spring-projects-issues
    CollaboratorAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: corestatus: bulk-closed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions