From 2753c01392eb4b02f41234313f798125a2d4baa1 Mon Sep 17 00:00:00 2001 From: xjm Date: Thu, 21 Feb 2019 08:41:50 -0600 Subject: [PATCH] Issue #2994550 by tedbow, tim.plunkett, gapple, johndevman, Wim Leers, neclimdul, EclipseGc, catch: Filtering block plugins by context is slow --- .../Drupal/Core/Plugin/Context/ContextHandler.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php b/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php index 2c41ced3bb0..eb8b628276f 100644 --- a/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php +++ b/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php @@ -17,12 +17,19 @@ class ContextHandler implements ContextHandlerInterface { * {@inheritdoc} */ public function filterPluginDefinitionsByContexts(array $contexts, array $definitions) { - return array_filter($definitions, function ($plugin_definition) use ($contexts) { + $checked_requirements = []; + return array_filter($definitions, function ($plugin_definition) use ($contexts, &$checked_requirements) { $context_definitions = $this->getContextDefinitions($plugin_definition); - if ($context_definitions) { - // Check the set of contexts against the requirements. - return $this->checkRequirements($contexts, $context_definitions); + // Generate a unique key for the current context definitions. This will + // allow calling checkRequirements() once for all plugins that have the + // same context definitions. + $context_definitions_key = hash('sha256', serialize($context_definitions)); + if (!isset($checked_requirements[$context_definitions_key])) { + // Check the set of contexts against the requirements. + $checked_requirements[$context_definitions_key] = $this->checkRequirements($contexts, $context_definitions); + } + return $checked_requirements[$context_definitions_key]; } // If this plugin doesn't need any context, it is available to use. return TRUE;