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 constructor
invocations, 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.
0 votes, 5 watchers
The text was updated successfully, but these errors were encountered:
I wonder whether this makes a significant difference for the typical instances that the Spring application context instantiates. Since we're usually injecting a bunch of fields and/or methods via reflection afterwards, the gain from non-reflective instantiation may not really show for those kinds of objects. Even without field/method injection, all the handling that the container applies around the actual bean instantiation itself might be more significant than instantiation efficiency.
That said, for highly demanded request-scoped beans with constructor injection only, it might nevertheless deliver a significant enough benefit. We should at least evaluate that option.