Problem/Motivation
FinishResponseSubscriber loads the system.performance config in its constructor. This config is only used for cacheable responses to check if cache.page.max_age is > 0.
if ($is_cacheable && $this->config->get('cache.page.max_age') > 0) {
It's not needed in \Drupal\Core\EventSubscriber\FinishResponseSubscriber::onRespond if:
- The response is for a subrequest
- !$response instanceof CacheableResponseInterface
- Request/Response policies deem it as uncacheable
This avoids unnecessary config loading and hitting cached config for an unused config object.
Steps to reproduce
Proposed resolution
Do not load config in the constructor, only if the page is cacheable.
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Comments
Comment #3
mglamanI asked Claude Code to help me analyze the impact of this and the request/response lifecycle with loading system.performance
The real savings from lazy-loading are limited to non-cacheable, non-HTML responses (redirects, file downloads, form POSTs, REST/JSON responses that exit early at the !$response instanceof CacheableResponseInterface check on line 129). On these paths, nothing else in the response pipeline loads system.performance.
It's minor but lazy loading the config load in FinishResponseSubscriber can improve some performance
Comment #4
mglamanComment #6
smustgrave commentedCould this impact contrib that could be calling the config?
Comment #7
smustgrave commentedRegarding #6 not sure if it's a concern but won't let it be a blocker. Let me know if I'm wrong.
Comment #8
catchWe could add bc for the FinishResponseSubscriber constructor, but it will be a pain because the argument is right in the middle of the list. Can we check if there are any contrib subclasses?
HtmlResponseAttachmentsProcessor also checks system.performance but it won't if there is no CSS or JS to add, or if the site is in maintenance mode - could we make that lazy at the same time maybe?
Comment #9
nicxvan commentedThese are the only contrib users I found:
https://git.drupalcode.org/project/adv_varnish/-/blob/8cea40b68c6323586e...
https://git.drupalcode.org/project/cache_control_headers/-/blob/8902ed0b...
A lot of comments referencing it though.
Needs work for HtmlResponseAttachmentsProcessor i didn't review usages there.
Comment #10
longwaveWith PHP 8.5 property hooks I think maybe we can do this in a backward compatible way, by moving the config load to a getter?
Comment #13
longwaveMR!16718 demonstrates the getter idea but PHPCS does not like this syntax.
Comment #14
berdiryes, but property hooks require us to exclude the entire file from phpcs because it doesn't support that at all. I think a magic __get() is enough?
Comment #15
longwaveWhat about a wider architectural change: what if Config objects become lazy and only hydrate themselves when you
get()a key (or call any of the other methods)?