Problem/Motivation
When an entity is being viewed. The entity type behaviors are also loaded. This uses the BehaviorConfigFactory to load all supported entity type behaviors for a given entity type / bundle.
However, the result of a scan is not cached. So when loading a page with a lot of entities, this leads to an unnecessary amount of of listing the config.
Example. We have 435 entities that are being loaded on an empty render cache.
This leads to almost 11k calls to getConfiguredBehaviors.

The getConfiguredBehaviors calls 11k times the getConfigDataForEntityTypeAndBundle. While the getConfigDataForEntityTypeAndBundle 88.5k times calls unserialize on the data.

getConfiguredEntityTypesAndBundles (called 435 times) also requests a listing of all the config every time and scans the list for behaviors.
$configNames = $this->configFactory->listAll($prefix);
The impact here isn't the same as the getConfiguredBehaviors, as core has static cache for the listing. But we shouldn't request it, if we already have calculated the configured ones before.
Steps to reproduce
Clear the cache and go to a random page with multiple entities on. After that also go to a second page.
Proposed resolution
At least introduce static cache. But we probably can also extend it to cache the result permanently. Configured behaviors only change when changing config. So there is no need to calculate this every time.
Remaining tasks
Implement cache
| Comment | File | Size | Author |
|---|---|---|---|
| blackfire-entity-type-behaviors-2.png | 83.56 KB | nils.destoop | |
| blackfire-entity-type-behaviors-1.png | 119.4 KB | nils.destoop |
Issue fork entity_type_behaviors-3526218
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
Comment #2
nils.destoop commentedComment #3
nils.destoop commentedComment #4
nils.destoop commentedComment #5
nils.destoop commentedComment #7
nils.destoop commentedMerge request adds a cache layer
Comment #9
jeroentMerged to 2.1.x. Thanks!