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

Issue fork drupal-3540077

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mglaman created an issue. See original summary.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

mglaman’s picture

I 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

mglaman’s picture

Status: Active » Needs review

smustgrave’s picture

Could this impact contrib that could be calling the config?

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Regarding #6 not sure if it's a concern but won't let it be a blocker. Let me know if I'm wrong.

catch’s picture

Status: Reviewed & tested by the community » Needs review

We 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?

nicxvan’s picture

Status: Needs review » Needs work

These 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.

longwave’s picture

With 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?

longwave-bot made their first commit to this issue’s fork.

longwave’s picture

MR!16718 demonstrates the getter idea but PHPCS does not like this syntax.

berdir’s picture

yes, 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?

longwave’s picture

What 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)?