.../Core/Render/PlaceholderingRenderCache.php | 82 +++++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/core/lib/Drupal/Core/Render/PlaceholderingRenderCache.php b/core/lib/Drupal/Core/Render/PlaceholderingRenderCache.php index 8460f0a..9d446ea 100644 --- a/core/lib/Drupal/Core/Render/PlaceholderingRenderCache.php +++ b/core/lib/Drupal/Core/Render/PlaceholderingRenderCache.php @@ -56,6 +56,31 @@ class PlaceholderingRenderCache extends RenderCache { protected $placeholderGenerator; /** + * Stores rendered results for automatically placeholdered elements. + * + * This allows us to avoid talking to the cache twice per auto-placeholdered + * element, or in case of an uncacheable element, to render it twice. + * + * Scenario A. The double cache read would happen because: + * 1. when rendering, cache read, but auto-placeholdered + * 2. when rendering placeholders, again cache read + * + * Scenario B. The cache write plus read would happen because: + * 1. when rendering, cache write, but auto-placeholdered + * 2. when rendering placeholders, cache read + * + * Scenario C. The double rendering for an uncacheable element would happen because: + * 1. when rendering, not cacheable, but auto-placeholdered + * 2. when rendering placeholders, rendered again + * + * In all three scenarios, this static cache avoids the second step, thus + * avoiding expensive work. + * + * @var array + */ + protected $placeholderResultsCache = []; + + /** * Constructs a new PlaceholderingRenderCache object. * * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack @@ -76,6 +101,15 @@ public function __construct(RequestStack $request_stack, CacheFactoryInterface $ * {@inheritdoc} */ public function get(array $elements) { + // When rendering placeholders, special case auto-placeholdered elements: + // avoid retrieving them from cache again, or rendering them again. + if (isset($elements['#create_placeholder']) && $elements['#create_placeholder'] === FALSE) { + $cached_placeholder_result = $this->getFromPlaceholderResultsCache($elements); + if ($cached_placeholder_result !== FALSE) { + return $cached_placeholder_result; + } + } + $cached_element = parent::get($elements); if ($cached_element === FALSE) { @@ -83,7 +117,7 @@ public function get(array $elements) { } else { if ($this->placeholderGenerator->canCreatePlaceholder($elements) && $this->placeholderGenerator->shouldAutomaticallyPlaceholder($cached_element)) { - return $this->placeholderGenerator->createPlaceholder($elements); + return $this->createPlaceholderAndRemember($cached_element, $elements); } return $cached_element; @@ -100,10 +134,54 @@ public function set(array &$elements, array $pre_bubbling_elements) { // Overwrite $elements with a placeholder. The Renderer (which called this // method) will update the context with the bubbleable metadata of the // overwritten $elements. - $elements = $this->placeholderGenerator->createPlaceholder($pre_bubbling_elements); + $elements = $this->createPlaceholderAndRemember($elements, $pre_bubbling_elements); } return $result; } + /** + * Create a placeholder for a renderable array and remember in a static cache. + * + * @param array $rendered_elements + * A fully rendered renderable array. + * @param array $pre_bubbling_elements + * A renderable array corresponding to the state (in particular, the + * cacheability metadata) of $rendered_elements prior to the beginning of + * its rendering process, and therefore before any bubbling of child + * information has taken place. Only the #cache property is used by this + * function, so the caller may omit all other properties and children from + * this array. + * + * @return array + * Renderable array with placeholder markup and the attached placeholder + * replacement metadata. + */ + protected function createPlaceholderAndRemember(array $rendered_elements, array $pre_bubbling_elements) { + $placeholder_element = $this->placeholderGenerator->createPlaceholder($pre_bubbling_elements); + // Remember the result for this placeholder to avoid double work. + $placeholder = $placeholder_element['#markup']; + $this->placeholderResultsCache[$placeholder] = $rendered_elements; + return $placeholder_element; + } + + /** + * Retrieves an auto-placeholdered renderable array from the static cache. + * + * @param array $elements + * A renderable array. + * + * @return array|false + * A renderable array, with the original element and all its children pre- + * rendered, or FALSE if no cached copy of the element is available. + */ + protected function getFromPlaceholderResultsCache(array $elements) { + $placeholder_element = $this->placeholderGenerator->createPlaceholder($elements); + $placeholder = $placeholder_element['#markup']; + if (isset($this->placeholderResultsCache[$placeholder])) { + return $this->placeholderResultsCache[$placeholder]; + } + return FALSE; + } + }