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
(This issue is a follow-up to #5439, which I closed to focus more globally on this problem.)
When loading all entities of a specific type more than once in a single page request (e.g. by using entity_load_multiple('node')), Backdrop does not make use the static cache built in DefaultEntityController. Instead, it queries the database every single time.
There are instances when two or more modules need to load all entities of a type, so this bug can result in a big drag on performance, especially if you are dealing with hundreds of entities.
Steps To Reproduce
The following steps will show that the static cache is not being used on the second request to load all entities in a single page request:
Enable Devel
Edit DefaultEntityController::cacheGet within entity.controller.inc by inserting the following code right before return $entities;:
dpm($this->entityCache, 'Static cache content');
dpm($entities, 'Entities from cache to be returned');
Go to admin/devel/php and run the following to load all nodes TWICE in a single page request:
(After testing be sure to remove the dpm you added to entity.controller.inc)
Actual behavior
You will see four array outputs for each dpm:
As you can see above, the static cache contains two nodes in the second call, yet the $entities array to be returned is empty. This is evidence that the entities ARE being cached in the static cache, but they are not being returned.
Expected behavior
The method should return the nodes in the static cache after the second call.
Additional information
Two fixes are needed to allow DefaultEntityController::load to use the static cache when loading all entities of a type:
Fix DefaultEntityController::cacheGet() to return all the cached entities when no $ids ar eprovided, AND
Fix DefaultEntityControler::load() to use a flag to indicate that all entities have been cached in the static cache, so that the database is not queried unnecessarily. This is the approach that Drupal 7 Entity API took (see the flag $this->cacheComplete over there) and the approach that Entity Plus still takes.
I will work on providing a PR for this.
The text was updated successfully, but these errors were encountered:
Description of the bug
(This issue is a follow-up to #5439, which I closed to focus more globally on this problem.)
When loading all entities of a specific type more than once in a single page request (e.g. by using
entity_load_multiple('node')
), Backdrop does not make use the static cache built inDefaultEntityController
. Instead, it queries the database every single time.There are instances when two or more modules need to load all entities of a type, so this bug can result in a big drag on performance, especially if you are dealing with hundreds of entities.
Steps To Reproduce
The following steps will show that the static cache is not being used on the second request to load all entities in a single page request:
DefaultEntityController::cacheGet
withinentity.controller.inc
by inserting the following code right beforereturn $entities;
:admin/devel/php
and run the following to load all nodes TWICE in a single page request:(After testing be sure to remove the
dpm
you added toentity.controller.inc
)Actual behavior
You will see four array outputs for each dpm:
As you can see above, the static cache contains two nodes in the second call, yet the
$entities
array to be returned is empty. This is evidence that the entities ARE being cached in the static cache, but they are not being returned.Expected behavior
The method should return the nodes in the static cache after the second call.
Additional information
Two fixes are needed to allow
DefaultEntityController::load
to use the static cache when loading all entities of a type:DefaultEntityController::cacheGet()
to return all the cached entities when no$ids
ar eprovided, ANDDefaultEntityControler::load()
to use a flag to indicate that all entities have been cached in the static cache, so that the database is not queried unnecessarily. This is the approach that Drupal 7 Entity API took (see the flag$this->cacheComplete
over there) and the approach that Entity Plus still takes.I will work on providing a PR for this.
The text was updated successfully, but these errors were encountered: