You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Binder performance could be improved or maybe short circuited in some cases #13565
Spring Boot uses Binder.bind() at a very early stage in quite a lot
of places. SpringApplication itself, LoggingApplicationListener, ConfigFileApplicationListener and AnsiOutputApplicationListener
all get used early and their combined use of Binder adds up to more
than 5% of the startup time in BuncApplication. If Binder could be
replaced with a simple call to Environment.getProperty() it would be
much faster - we tried this with ConfigFileApplicationListener with
positive results, but that caused test failures in Spring Boot, so the
binder is doing something clever that is necessary in at least some
corner cases.
Benchmark for extracting a String[] from an Environment property:
Here the "map" benchmark uses a MapConfigurationPropertySource instead of the full Environment (I noticed this pattern in XADataSourceAutoConfiguration). It's only 10x faster, but that's a step in the right direction. Maybe that's a hint about how to improve it, especially for the early bindings.
It's almost as fast if you just wrap the Environment in a ConfigurationPropertySource that simply mirrors Environment.getProperty():
This has boosted the performance of the "map" and "wrapped" benchmarks but the "binder" one is still hampered by something else, probably SpringConfigurationSource judging by the flame graphs (and the fact that it is not in the code path of the faster samples).
So, while commenting out that line obviously breaks something else, it wasn't needed for this use case and it speeds up the binder by a factor of 10. Maybe an optimization opportunity there.
Here's another one. Not sure if this makes a functional difference, but BindConverter creates a new instance of TypeConverterConversionService for every instance, and it looks like maybe it doesn't need to if the propertyEditorInitializer is null. Opting out of that (if the initializer is null) speeds things up a bit as well:
BindConverter creates a new instance of TypeConverterConversionService for every instance, and it looks like maybe it doesn't need to if the propertyEditorInitializer is null
I think we can go a step further. Each time a Context is created, it creates the BindConverter using the conversion service and property editor initializer from the Binder. As far as I can tell the conversion service is always the same ApplicationConversionService instance and the property editor initializer is either null or the lambda from ConfigurationPropertiesBinder. In short, I think there's only a need for two different BindConverter instances to have been created.
With that said, I'm not sure how much we will gain. In a minimal app (just a dependency on spring-boot-starter-web) only 10 Context instances are created. I think we need to be wary of looking at microbenchmarks that focus heavily on the binder as they may not translate into much when launching an app.
That's true. I did say in the beginning that I thought around 5% of startup time was being used up. If Binder was 10x faster that could be as little as 0.5%, so that would be worthwhile. Seems like the TypeConverterConversionService can be optimized easily, but won't get us the 10x (that's the value of the focused benchmarks).
The only place Binder shows up as a hotspot on startup of a vanilla empty Boot app now is in LoggingApplicationListener.setLogLevels() (about 2.5%). If we could squish that one it would be awesome - especially as 99% of the time there are 0 properties in logging.level.* and you still pay the cost.
I've replaced some Stream calls with simple Iterators but I'm not sure there's much we can do for 2.1. Some unscientific System.nanoTime logging on the Tomcat sample shows LoggingApplicationListener.setLogLevels() takes about 4 milliseconds when there's no entries.
Activity
dsyer commentedon Jun 25, 2018
Here's another benchmark (different hardware, so slower for the same use cases):
Here the "map" benchmark uses a
MapConfigurationPropertySourceinstead of the fullEnvironment(I noticed this pattern inXADataSourceAutoConfiguration). It's only 10x faster, but that's a step in the right direction. Maybe that's a hint about how to improve it, especially for the early bindings.It's almost as fast if you just wrap the
Environmentin aConfigurationPropertySourcethat simply mirrorsEnvironment.getProperty():dsyer commentedon Jun 25, 2018
More results where this time I short circuit
ArrayBinder.bindAggregate()to just return the value directly if it finds one that matches the spec:This has boosted the performance of the "map" and "wrapped" benchmarks but the "binder" one is still hampered by something else, probably
SpringConfigurationSourcejudging by the flame graphs (and the fact that it is not in the code path of the faster samples).dsyer commentedon Jun 25, 2018
Another round of experiments:
This time I short circuited
BindConverter.convertif the source type is already assignable to the target type (i.e. there is no conversion to do).dsyer commentedon Jun 26, 2018
Comment out this line, where the binder spends a lot of time looping through system properties and environment variables, finding nothing:
So, while commenting out that line obviously breaks something else, it wasn't needed for this use case and it speeds up the binder by a factor of 10. Maybe an optimization opportunity there.
dsyer commentedon Jun 26, 2018
Here's another one. Not sure if this makes a functional difference, but
BindConvertercreates a new instance ofTypeConverterConversionServicefor every instance, and it looks like maybe it doesn't need to if thepropertyEditorInitializeris null. Opting out of that (if the initializer is null) speeds things up a bit as well:wilkinsona commentedon Jun 29, 2018
I think we can go a step further. Each time a
Contextis created, it creates theBindConverterusing the conversion service and property editor initializer from theBinder. As far as I can tell the conversion service is always the sameApplicationConversionServiceinstance and the property editor initializer is eithernullor the lambda fromConfigurationPropertiesBinder. In short, I think there's only a need for two differentBindConverterinstances to have been created.With that said, I'm not sure how much we will gain. In a minimal app (just a dependency on
spring-boot-starter-web) only 10Contextinstances are created. I think we need to be wary of looking at microbenchmarks that focus heavily on the binder as they may not translate into much when launching an app.dsyer commentedon Jun 29, 2018
That's true. I did say in the beginning that I thought around 5% of startup time was being used up. If
Binderwas 10x faster that could be as little as0.5%, so that would be worthwhile. Seems like theTypeConverterConversionServicecan be optimized easily, but won't get us the 10x (that's the value of the focused benchmarks).philwebb commentedon Sep 21, 2018
I've created a new issue (#14562) based on Andy's comment above.
dsyer commentedon Oct 18, 2018
Things are much better now. It seems that
Binder.bind()to aString[]is 2-3 times faster, and the "map" and "wrapped" benchmarks are 10x faster:The only place
Bindershows up as a hotspot on startup of a vanilla empty Boot app now is inLoggingApplicationListener.setLogLevels()(about 2.5%). If we could squish that one it would be awesome - especially as 99% of the time there are 0 properties inlogging.level.*and you still pay the cost.philwebb commentedon Oct 26, 2018
I've replaced some
Streamcalls with simpleIteratorsbut I'm not sure there's much we can do for 2.1. Some unscientificSystem.nanoTimelogging on the Tomcat sample showsLoggingApplicationListener.setLogLevels()takes about 4 milliseconds when there's no entries.We can have another go in 2.2.