Skip to content

Commit 5ef0ebc

Browse files
committed
Issue #2994550 by tedbow, tim.plunkett, gapple, johndevman, Wim Leers, neclimdul, EclipseGc, catch: Filtering block plugins by context is slow
(cherry picked from commit 2753c01)
1 parent 768c5ab commit 5ef0ebc

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

lib/Drupal/Core/Plugin/Context/ContextHandler.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,19 @@ class ContextHandler implements ContextHandlerInterface {
1717
* {@inheritdoc}
1818
*/
1919
public function filterPluginDefinitionsByContexts(array $contexts, array $definitions) {
20-
return array_filter($definitions, function ($plugin_definition) use ($contexts) {
20+
$checked_requirements = [];
21+
return array_filter($definitions, function ($plugin_definition) use ($contexts, &$checked_requirements) {
2122
$context_definitions = $this->getContextDefinitions($plugin_definition);
22-
2323
if ($context_definitions) {
24-
// Check the set of contexts against the requirements.
25-
return $this->checkRequirements($contexts, $context_definitions);
24+
// Generate a unique key for the current context definitions. This will
25+
// allow calling checkRequirements() once for all plugins that have the
26+
// same context definitions.
27+
$context_definitions_key = hash('sha256', serialize($context_definitions));
28+
if (!isset($checked_requirements[$context_definitions_key])) {
29+
// Check the set of contexts against the requirements.
30+
$checked_requirements[$context_definitions_key] = $this->checkRequirements($contexts, $context_definitions);
31+
}
32+
return $checked_requirements[$context_definitions_key];
2633
}
2734
// If this plugin doesn't need any context, it is available to use.
2835
return TRUE;

0 commit comments

Comments
 (0)