Optimize \Drupal\Core\Plugin\Context\ContextHandler::checkRequirements()

Created on 2 December 2020, over 3 years ago
Updated 30 January 2023, over 1 year ago

Problem/Motivation

\Drupal\Core\Plugin\Context\ContextHandler::checkRequirements() is a low-level part of the plugin and context systems. Given a set of requirements (in the form of context definitions), it checks a provided set of candidate contexts to see if the requirements are met.

Internally ::checkRequirements() uses ::getMatchingContexts() to find the full set of matching contexts, and then checks to see if the set it empty.

With this being called within several nested loops, this means that ContextDefinition::isSatisfiedBy() will be called once for each requirement and each candidate context, once per section storage, once per entity rendered.

With a view of 8 entities all rendered by Layout Builder, this means 512 calls per page.

With this patch, the calls drops to 112.

Steps to reproduce

Add step debugging in \Drupal\Core\Plugin\Context\ContextDefinition::isSatisfiedBy() and note how many calls are made in a given request on a page built with Layout Builder.

Proposed resolution

Return once the first matching context is found.

Remaining tasks

N/A

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

πŸ“Œ Task
Status

Needs work

Version

10.1 ✨

Component
PluginΒ  β†’

Last updated about 18 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States tim.plunkett Philadelphia

Live updates comments and jobs are added and updated live.
  • Blocks-Layouts

    Blocks and Layouts Initiative. See the #2811175 Add layouts to Drupal issue.

  • Performance

    It affects performance. It is often combined with the Needs profiling tag.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot β†’ tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

Production build 0.69.0 2024