- Every call to hasPermission() does
$roles = \Drupal::entityManager()->getStorageController('user_role')->loadMultiple($this->getRoles());
for both logged in users and anonymous users. - The config records themselves are statically cached within ConfigFactory, so successive loads of the same role don't require additional IO.
- However, each load requires invoking the entity constructor, Role::postLoad(), hook_entity_load(), and hook_user_role_load().
- The entity system includes a static cache feature (enabled via the
static_cache
annotation key) that allows the above to be bypassed for static cache hits. - Currently in HEAD, @ContentEntityType defaults static_cache to TRUE (since for content entities, there's a significant IO savings) and @ConfigEntityType defaults it to FALSE (since for config entities, it only saves on a few function calls, not on IO). There are no individual entity types in HEAD that override this default.
- Because hasPermission() is called so frequently, this issue makes Role the first config entity type to use static_cache.
- Xhprof screenshot from a few months ago (possibly outdated): Anon frontpage get comparison of UserSession::hasPermission().
↧
Statically cache Role entities to speed up (User|UserSession)::hasPermission()
↧