Skip to content

ApplicationListener is kind of inefficient, and maybe its usage could be optimized #13566

Closed

Description

@dsyer

Spring is pretty slow at calculating the generic signature of ApplicationListeners. See https://jira.spring.io/browse/SPR-16970 for more background.

Spring Boot exacerbates these issues by having 2
ApplicationEventMulticasters (one in EventPublishingRunListener
and one in the actual ApplicationContext).

Some of the Boot listeners could be implemented as
SmartApplicationListener to avoid the cost of looking up the generic
type information.

It might also help to use a different callback
(e.g. SpringApplicationRunListener or
ApplicationContextInitializer) instead of
ApplicationListener. E.g. LiquibaseServiceLocatorApplicationListener
which is queried multiple times in a vanilla Boot app, only to do nothing
because Liquibase is not present. It only needs to be called once, so ApplicationContextInitializer
would probably suffice.

Activity

  1. added
    theme: performance
    on Jun 24, 2018
  2. added
    status: waiting-for-triage
    on Jun 25, 2018
  3. added
    type: task
    and removed
    status: waiting-for-triage
    on Jun 26, 2018
  4. added this to the Backlog milestone on Jun 26, 2018
  5. philwebb commented on Sep 24, 2018

    @philwebb
    Member

    The cache in GenericApplicationListenerAdapter is static so two ApplicationEventMulticasters shouldn't make a big difference.

  6. dsyer commented on Sep 25, 2018

    @dsyer
    MemberAuthor

    The cache in AbstractApplicationEventMulticaster is not static though. So there is a cost in having multiple instances. (The other points are more important anyway.)

  7. modified the milestones: 2.1.x, 2.x on Oct 4, 2018
  8. modified the milestones: 2.x, on Jan 14, 2019
  9. wilkinsona commented on Jan 18, 2019

    @wilkinsona
    Member

    During start up of SampleWebFluxApplication, a total of 4-5ms is spent getting the application listeners for the 8 events that are multicast. Switching LiquibaseServiceLocatorApplicationListener to be a SpringApplicationRunListener makes no discernible difference to the time spent retrieving the application listeners.

    I'm going to close this one for now as there doesn't appear to be much, if anything, to be gained.

  10. removed this from the milestone on Jan 18, 2019
  11. added
    status: declined
    on Jan 18, 2019
  12. dsyer commented on Jan 18, 2019

    @dsyer
    MemberAuthor

    Oh well. So be it. I'm pretty disappointed in this though, I have to say. It's a small cost for a small reward. What's so wrong about that? For the record, I think that a more direct approach is probably intrinsically better.

  13. wilkinsona commented on Jan 18, 2019

    @wilkinsona
    Member

    It's a small cost for a small reward

    I haven't been able to identify any reward. Upon converting the Liquibase listener to be a run listener, I couldn't see any difference in the time taken getting and calling the application listeners (it was comfortably within the error bars), and that was without considering the possible increase in time taken to call the run listeners.

    The cost isn't that small either. In addition to the time taken to make the changes, there's also the fact that some of the conversions would be breaking API changes. LiquibaseServiceLocatorApplicationListener, for example, is public. If someone can present evidence that there's a gain to be had here that's reproducible and more precise than "kind of inefficient" we can reconsider, but right now I don't think any change can be justified as, from what I've seen, it won't make any discernible difference to performance.

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

    status: declinedtheme: performancetype: task

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions