Speed up dynamic instantiation of objects with compiled SpEL expressions [SPR-12330] #16935
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
status: bulk-closed
An outdated, unresolved issue that's closed in bulk as part of a cleaning process
Thomas Darimont opened SPR-12330 and commented
In a lot of places within the Spring framework, objects are dynamically instantiated via reflection.
This way of creating object instances can be quite slow (since the JVM doesn't speed this up, yet).
With the advent of compiled SpEL expressions we have a way to create instances much faster.
Once #16931 is fixed we could just create SpEL
Expression
's that contains appropriate constructorinvocations, parse and compile them at bootstrap time and store them in a shared map read-only map.
Once a new instance of a type T is requested, we could simply lookup the compiled SpEL
Expression
with the appropriate constructor invocation for T and just call it's getValue(...) method to obtain the actual instances.
We currently do something similar in Spring Data Commons: (spring-projects/spring-data-commons#98)
In our case we generate ASM code by hand since we cannot rely on Spring 4.1+ being available.
Of course not all reflective object instantiations can be replaced with this approach (e.g. classes with non-public ctors), but since the relevant types are often public(*1) and have public ctors it should be possible in the majority of cases.
The dynamic expression compilation/generation could introduce a performance penalty (at startup) which should payoff (IMHO) quite quickly in the long run.
(*1) if one could adjust the package name of the classes generated for compiled SpEL expression one could even instantiate package scoped classes with public ctors.
Affects: 4.1.1
0 votes, 5 watchers
The text was updated successfully, but these errors were encountered: