spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

Avoid proxy hint generation for annotations that are never synthesized

Open sbrannen opened this issue 3 years ago • 2 comments

Overview

We can reduce the amount of generated proxy configuration and the native footprint by avoiding inclusion of unused annotation proxy classes in the native images.

Rationale

Annotations like @GetMapping in Spring MVC are composed annotations for @RequestMapping; however, there is nowhere in the core Spring Framework where a lookup is performed for a @GetMapping annotation. Rather, the corresponding lookups are only for merged @RequestMapping annotations.

Consequently, @RequestMapping needs to be synthesized, but the composed annotations @GetMapping, @PostMapping, etc. do not need to be synthesized.

In other words, WebAnnotationsRuntimeHintsRegistrar.registerHints() can be greatly simplified.

Brainstorming

We could analyze how we use RuntimeHintsUtils.registerAnnotation, simplify our usage by only registering annotations we know will need to be synthesized, and update the documentation to reflect this mindset/guidance.

We could introduce support (potentially in the agent) to detect the use of @AliasFor referencing a meta-annotation's attribute and automatically register the target meta-annotation for a synthesized annotation proxy.

Related Issues

  • #28767

sbrannen avatar Jul 06 '22 13:07 sbrannen

RuntimeHintsUtils has now a registerComposableAnnotation, so I think using it on RequestMapping should cover some ground. The brainstorming bit in this issue makes it harder for me to understand the scope it.

snicoll avatar Aug 01 '22 12:08 snicoll

@sbrannen can we please clarify the scope of this?

snicoll avatar Aug 10 '22 10:08 snicoll

RuntimeHintsUtils has now a registerComposableAnnotation, so I think using it on RequestMapping should cover some ground.

Yes, that should help.

My original concern was that we generate too many hints for SynthesizedAnnotation proxies for certain sets of annotations -- for example, @GetMapping, etc. in WebAnnotationsRuntimeHintsRegistrar. See also #28953.

We should now be using registerComposableAnnotation for @RequestMapping, and for @GetMapping, etc. we would need a new method that only registers the reflection hints for @GetMapping. In other words, if we supply GetMapping.class to RuntimeHintsUtils.registerAnnotation, it will register a SynthesizedAnnotation proxy hint for @GetMapping that will never be used within a native image (or at least not as a result of standard usage in the framework itself).

So, we could consider introducing a method such as RuntimeHintsUtils.registerAnnotationForReflectionOnly.

The brainstorming bit in this issue makes it harder for me to understand the scope it.

I've covered part of the brainstorming above regarding the introduction of another "for reflection only" registration method.

The second part is to document specific use cases in the Javadoc to make it clear to the user when to use which registerAnnotation* variant. @RequestMapping and @GetMapping serve as a prime example that we can discuss to clarify things for users.

The last part was about automating the process of invoking registerAnnotation* variants in an agent that performs static analysis to determine the appropriate action instead of leaving it up to the user. This part is of course totally optional and could be added later down the road if deemed desirable.

Does that make sense now?

sbrannen avatar Aug 11 '22 19:08 sbrannen

I am not 100% sure but it looks like this issue, as described, is supesrseded now

snicoll avatar Aug 16 '22 14:08 snicoll

Indeed it may be superseded now. I'll take another pass later in the week.

sbrannen avatar Aug 16 '22 15:08 sbrannen

Closing in light of #28967 and related recent changes.

If the need arises, we can reopen this issue at a later date.

sbrannen avatar Aug 17 '22 10:08 sbrannen