-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Optimize reflection configuration in Native Image #3566
Comments
Related WIP PR #3637 |
Fixed in 21.3. |
sdeleuze
added a commit
to sdeleuze/spring-native
that referenced
this issue
Oct 26, 2021
This commit adds supports for reflective query introduced via oracle/graal#3566 which allows to reduce the memory footprint when only access to reflective metadata is required, without reflective invocation. They can be specified via @typehint using flags like with `@TypeHint(types = Bar.class, access = AccessBits.QUERY_DECLARED_METHODS))` or by listing explicitly the methods like with `@TypeHint(types = Foo.class, queriedMethods = @MethodHint(name = "toto", parameterTypes = String.class))`. Closes spring-atticgh-1156
sdeleuze
added a commit
to sdeleuze/spring-native
that referenced
this issue
Oct 26, 2021
This commit adds supports for reflective query introduced via oracle/graal#3566 which allows to reduce the memory footprint when only access to reflective metadata is required, without reflective invocation. They can be specified via @typehint using flags like with `@TypeHint(types = Bar.class, access = AccessBits.QUERY_DECLARED_METHODS))` or by listing explicitly the methods like with `@TypeHint(types = Foo.class, queriedMethods = @MethodHint(name = "toto", parameterTypes = String.class))`. Closes spring-atticgh-1156
sdeleuze
added a commit
to sdeleuze/spring-native
that referenced
this issue
Oct 26, 2021
This commit adds supports for reflective query introduced via oracle/graal#3566 which allows to reduce the memory footprint when only access to reflective metadata is required, without reflective invocation. They can be specified via @typehint using flags like with `@TypeHint(types = Bar.class, access = AccessBits.QUERY_DECLARED_METHODS))` or by listing explicitly the methods like with `@TypeHint(types = Foo.class, queriedMethods = @MethodHint(name = "toto", parameterTypes = String.class))`. Closes spring-atticgh-1156
sdeleuze
added a commit
to sdeleuze/spring-native
that referenced
this issue
Oct 26, 2021
This commit adds supports for reflective query introduced via oracle/graal#3566 which allows to reduce the memory footprint when only access to reflective metadata is required, without reflective invocation. They can be specified via @typehint using flags like with `@TypeHint(types = Bar.class, access = AccessBits.QUERY_DECLARED_METHODS))` or by listing explicitly the methods like with `@TypeHint(types = Foo.class, queriedMethods = @MethodHint(name = "toto", parameterTypes = String.class))`. See spring-atticgh-1156
sdeleuze
added a commit
to sdeleuze/spring-native
that referenced
this issue
Oct 26, 2021
This commit adds supports for reflective query introduced via oracle/graal#3566 which allows to reduce the memory footprint when only access to reflective metadata is required, without reflective invocation. They can be specified via @typehint using flags like with `@TypeHint(types = Bar.class, access = AccessBits.QUERY_DECLARED_METHODS))` or by listing explicitly the methods like with `@TypeHint(types = Foo.class, queriedMethods = @MethodHint(name = "toto", parameterTypes = String.class))`. See spring-atticgh-1156
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Goal
Reduce image size and configuration size by limiting useless reflection capabilities being generated for methods never invoked through reflection and enabling reflection queries without configuration, while maintaining the option to fine-tune the reflection metadata being included for image size-sensitive applications.
Context
Reflection operation types
The Native Image implementation of Java’s reflection API currently doesn’t distinguish between two types of reflective operations:
Shortcomings of the current implementation
This design decision leads to access capabilities being provided for members that are only queried but never accessed. This has an impact on the final image size mainly due to two factors:
This only happens for methods and constructors, so the following proposal focuses solely on those.
Example
Given the following code (lightly edited for space):
The current reflection configuration for this program is as follows:
As a consequence, useless reflection invocation capabilities are created for the
main
,callMethodsWithAnnotation
and, more importantly,deadCode
, which could force the inclusion of an arbitrary amount of unreachable code into the image.Evaluation of impact on image size and configuration
Protocol
We measured the impact of the following changes on various regular and microservice benchmarks:
Results
The results show that the reduction in image size gained through the elimination of reachability analysis false positives and the non-inclusion of useless proxy classes is larger than the size increase caused by full metadata inclusion in all evaluated cases, this difference being quite important on some microservice benchmarks, particularly those based on the Spring and Micronaut frameworks (up to 20%).
The impact on configuration size is also positive, with the added bonus that the configuration is simplified by not needing the
allDeclaredMethods
andallPublicMethods
fields. In most cases at most one of the methods included by these fields is actually invoked at runtime.Proposal
Based on the results and to address the shortcomings presented above, we propose the following:
--configure-reflection-metadata
Native Image option, and makes use of the following fields specifying methods being queried but not invoked:queriedMethods
, holding a list of methods;queryAllDeclaredMethods
,queryAllPublicMethods
,queryAllDeclaredConstructors
,queryAllPublicConstructors
.The Native Image agent will be updated to output valid configuration files with or without (by default) the fine-tuning fields. Fine-tuning can be enabled by specifying the track-reflection-metadata option. Example:
java -agentlib:native-image-agent=track-reflection-metadata ...
Example
Proposed new configuration for the example presented above:
Proposed configuration for the same code in optimized mode:
The text was updated successfully, but these errors were encountered: