Repository navigation
Description
Phil Webb opened SPR-16933 and commented
I've been looking at the performance involved with using the SimpleMetadataReader and I suspect that it might be possible to improve AnnotationUtils.
I started with the following application that simulates the class parsing involved in a typical Spring Boot application
This app takes about 130ms to read the metadata from those 106 classes.
If I change the code to use an empty ASM visitor, parsing takes ~30ms
Reading just class bytes into an array takes ~22ms (so ASM parsing itself is quite fast)
If I remove the code that visits annotations so no AnnotationAttributes are read, it takes ~60ms.
Looking at the profiler a lot of time is spent in AnnotationUtils so I created a second sample:
The NAMES being used here are the same as were ultimately used with the first sample. This one is reading annotation data for 413 classes (starting from their String name) and it takes ~93ms.
Affects: 5.0.7
Attachments:
- Screen Shot 2018-06-11 at 5.13.56 PM.png (174.80 kB)
Issue Links:
- Annotations on generic interface methods not found by AnnotationUtils [SPR-16060] #20609 Annotations on generic interface methods not found by AnnotationUtils
- Reduce ClassUtils.forName overhead (in particular for annotation introspection purposes) [SPR-16667] #21208 Reduce ClassUtils.forName overhead (in particular for annotation introspection purposes)
- Comprehensively cache annotated methods for interfaces and superclasses [SPR-16675] #21216 Comprehensively cache annotated methods for interfaces and superclasses
- Avoid repeated superclass introspection in AnnotationUtils.findAnnotation [SPR-16730] #21271 Avoid repeated superclass introspection in AnnotationUtils.findAnnotation
- Spring internal configuration classes can no longer use @EventListener [SPR-17160] #19660 Spring internal configuration classes can no longer use
@EventListener - @Scheduled task runs twice on bean with target-class scoped proxy (when injected) [SPR-17166] #21702
@Scheduledtask runs twice on bean with target-class scoped proxy (when injected)
0 votes, 6 watchers
Activity
spring-projects-issues commentedon Jun 12, 2018
Phil Webb commented
Looking at the profiler,
AnnotationUtils.isSynthesizablemight be a hot spot. Also, the fact that a proxy can be created might also be problematic.spring-projects-issues commentedon Jun 12, 2018
Phil Webb commented
Updating the sample to just get immediate annotations and only once per class gives 32ms
spring-projects-issues commentedon Jul 31, 2018
Juergen Hoeller commented
Phil Webb, since there seem to be some immediate optimizations that we can apply to
AnnotationUtils.isSynthesizable, even backportable to 5.0.9, I'll narrow the scope of this ticket to that specific hotspot. Let's deal with a wider-ranging refactoring in a separate ticket.spring-projects-issues commentedon Jul 31, 2018
Juergen Hoeller commented
I've done a revision on master which considers any
java/javaxandorg.springframework.langannotations as not synthesizable upfront, checking not only inisSynthesizablebut also atsynthesizeAnnotation(Array)level and therefore bypassing such overhead (even the reflectiveAnnotation.annotationType()call) for these common meta-annotations. This shows for classpath scanning in particular since it optimizes the commonAnnotationUtilscode paths entered by ASM; additionally, we're also skipping meta-annotation introspection forjava.lang.annotationtypes inAnnotationAttributesReadingVisitorupfront.I'll backport a variant of this to 5.0.9, being less aggressive with exclusion and primarily focused on avoiding
java.lang.annotationintrospection overhead (which covers a lot of cases already).spring-projects-issues commentedon Aug 1, 2018
Juergen Hoeller commented
Slightly extended scope again: We're avoiding synthesizable annotation creation for
@Bean/@Scopeprocessing now, interacting withAnnotationAttributesinBeanAnnotationHelperand streamlining towards plain annotation introspection inAnnotationAttributesReadingVisitorsince we only need attributes there anyway.spring-projects-issues commentedon Aug 7, 2018
Sam Brannen commented
Thanks for tackling all of this, Juergen Hoeller!
spring-projects-issues commentedon Aug 15, 2018
Juergen Hoeller commented
For 5.1, we also cache declared annotation arrays now, avoiding GC pressure for the reflection-returned array clones.
Last but not least,
AnnotatedElementUtils.get/findAllMergedAnnotationshas variants with a set of annotation types to look for, so thatSpringCacheAnnotationParsercan perform combined searching for all of its cache operation annotations in one step. This is also 5.1 only.spring-projects-issues commentedon Jan 4, 2019
fbutter commented
Juergen Hoeller, in this commit a condition was added to exclude beans without a Component annotation. Consequently beans provided by bean methods are not considered as listener anymore. This is a major change and e.g. spring cloud is not compatible anymore (spring-cloud-consul-discovery is not compatible with Spring Boot 2.1.1.RELEASE #481|https://github.com/spring-cloud/spring-cloud-consul/issues/481]). I could not find this in the release notes, was this intended?
jhoeller commentedon Jan 31, 2019
For the record, the
@EventListenerrevision was an intentional optimization that only affectedorg.springframeworkclasses (i.e. not user-provided application components) and was communicated across the Spring portfolio projects accordingly. If any issues remain here, they should be fixed in the affected portfolio projected through annotating the components accordingly, or preferably through implementingApplicationListenerinstead (as the preferred option for infrastructure components).sbrannen commentedon Mar 3, 2019
I just got bitten by this change... in an event listener in our own test suite, where the package is
org.springframework.test.context.event. Thus, theisSpringContainerClass()obviously returnstruefor such types, and it took me a bit of debugging to figure it out. 😱