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
ConfigFileApplicationListener logging is very inefficient #13436
ConfigFileApplicationListener creates a "description" of each resource that it attempts to load. In a tight loop 40% of sampled time goes to just creating the description (and 12% even when there is a single config location). It turns out to be extremely inefficient because of the use of String.format and ResourceUtils.toURI (both are expensive). The description is only logged by default if the file is found, so it isn't even used most of the time. I would recommend just using the "location" instead which is always available and always fairly descriptive of the resource, and costs nothing to compute.
The other main source of inefficiency is ClassPathResource.exists() (25% sampled time). To fix that would be more involved - we'd probably have to index the jars at build time or something. Might be worth it. There's a workaround for users, though - if you know the locations of the config files in the file system, you can skip searching the classpath by specifying spring.config.location explicitly.
Reacted by Tommy Ludwig, Christoph Dreis and kranthiboinpally
Here's another suggestion (it's obvious if you turn up logging to TRACE level): in ConfigFileApplicationListener.Loader.load(String, String, Profile, DocumentFilterFactory, DocumentConsumer) there needs to be a null check for the second loop, i.e:
then the ConfigFileApplicationListener uses much less CPU sample time on startup (it's only really the calls it makes to EnvironmentPostProcessor that costs anything, once this and the other problems above are addressed).
I didn't test this change other than in a startup benchmark, but it looks functionally identical. The CPU sample time in ConfigFileApplicationListener$Loader.load went down from 3.9% to 2.0%.
Update: I tried that change in the last comment and some tests fail, so the two versions are not functionally equivalent. Getting rid of the Binder there would be really helpful though, or optimizing the Binder.bind.
Looks like it was fixed in 1e7ed2f#13512. I suppose there are more possible optimizations: to create the description only once; to not use the resource URL.
Activity
philwebb commentedon Jun 11, 2018
I've also see
ClassPathResource.exists()a lot in past profiling adventures.dsyer commentedon Jun 22, 2018
Here's another suggestion (it's obvious if you turn up logging to TRACE level): in
ConfigFileApplicationListener.Loader.load(String, String, Profile, DocumentFilterFactory, DocumentConsumer)there needs to be a null check for the second loop, i.e:otherwise we just waste time looking for files called "applicationnull.yml" (etc.).
dsyer commentedon Jun 22, 2018
Another blip in the flame graph comes from excessive (I believe) use of
Binderto bind to profiles.Here:
and here:
If I replace these with, respectively,
and
where the
getProfiles()method is now:then the
ConfigFileApplicationListeneruses much less CPU sample time on startup (it's only really the calls it makes toEnvironmentPostProcessorthat costs anything, once this and the other problems above are addressed).I didn't test this change other than in a startup benchmark, but it looks functionally identical. The CPU sample time in
ConfigFileApplicationListener$Loader.loadwent down from 3.9% to 2.0%.dsyer commentedon Jun 22, 2018
Update: I tried that change in the last comment and some tests fail, so the two versions are not functionally equivalent. Getting rid of the
Binderthere would be really helpful though, or optimizing theBinder.bind.mbhave commentedon Aug 14, 2018
@dsyer Regarding the line below,
Isn't the description created only when TRACE is turned on so it shouldn't be computed in the default case?
dsyer commentedon Aug 15, 2018
Looks like it was fixed in 1e7ed2f #13512. I suppose there are more possible optimizations: to create the description only once; to not use the resource URL.
mbhave commentedon Aug 15, 2018
Thanks, Dave. I'd missed that issue.
Reduce GC pressure creating load descriptions
Reduce object creation when binding
Reduce GC pressure creating load descriptions