### Problem/Motivation

The `FlattenedSiteSettingsLoader::loadAll()` method is called via `site_settings_preprocess()` for **every Twig
template** rendered on a page. Since the loader plugin is instantiated fresh each time via
`getActiveLoaderPlugin()`, the instance property `$this->settings` is always NULL, causing a cache backend lookup
on every single call.

On a typical node edit page with ~4,000 templates, this results in **~4,000 unnecessary cache lookups** per
request, significantly impacting performance.

### Performance Impact

Testing on a Drupal 10.5.6 node edit page:

| Metric | Without Fix | With Fix |
|--------|-------------|----------|
| `loadAll()` cache lookups | 4,085 | 1 |
| `MemcachedDriver::getMulti` calls | 4,933 | ~850 |
| Wall time for site_settings | 250ms | <1ms |
| Total page load | 3.1s | ~2.6s |

### Root Cause

In `site_settings.module`, the preprocess hook creates a new loader instance for each template, and since
`$this->settings` is an instance property (not static), each new instance makes a fresh cache lookup.

### Proposed Resolution

Add a static cache property that persists across plugin instances within a single request:

1. Add `protected static $staticSettingsCache = [];`
2. Check static cache before backend cache in `loadAll()`
3. Update `clearCache()` and `rebuildCache()` to maintain consistency

### Why More Visible in Drupal 10.5.6+

Drupal core changes (Issues #3548313, #3555720) between 10.5.4 and 10.5.6 changed entity revision queries,
resulting in ~3.4x more template renders on the same page, making this pre-existing inefficiency much more
noticeable.

### Related Issues

Similar fix for Metatag module: https://www.drupal.org/project/metatag/issues/357736

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

moneeshk created an issue. See original summary.

moneeshk’s picture

moneeshk’s picture

StatusFileSize
new2.51 KB
moneeshk’s picture

StatusFileSize
new2.17 KB

Patch for 2.0.5

scott_euser’s picture

Version: 2.0.5 » 2.0.x-dev
Category: Bug report » Task
Priority: Major » Normal
Status: Active » Needs review

Setting back to normal per https://www.drupal.org/docs/develop/issues/fields-and-other-parts-of-an-... - valid workaround is use the full site settings loader (recommended). Also changing to task since its already got a cache layer which is not broken and could be a negligible win if the cache backend you use is already e.g. a memory one

In any case this looks fine though thanks!

scott_euser’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

scott_euser’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.