Skip to content

Cache the results of isCacheable calls to improve performance #40112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 2.4-develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions lib/internal/Magento/Framework/View/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ class Layout extends \Magento\Framework\Simplexml\Config implements \Magento\Fra
*/
private ResponseHttp $response;

/**
* Property used to cache the results of the isCacheable() method.
*/
private bool|null $isCacheableCache = null;

/**
* @param ProcessorFactory $processorFactory
* @param ManagerInterface $eventManager
Expand Down Expand Up @@ -1138,18 +1143,22 @@ protected function _prepareMessageGroup($messageGroups)
*/
public function isCacheable()
{
$this->build();
$elements = $this->getXml()->xpath('//' . Element::TYPE_BLOCK . '[@cacheable="false"]');
$cacheable = $this->cacheable;
foreach ($elements as $element) {
$blockName = $element->getBlockName();
if ($blockName !== false && $this->structure->hasElement($blockName)) {
$cacheable = false;
break;
if (!isset($this->isCacheableCache)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using use comparator here? $this->isCacheableCache === null
We are sure that isCacheableCache property exists, so we can avoid this check

$this->build();
$elements = $this->getXml()->xpath('//' . Element::TYPE_BLOCK . '[@cacheable="false"]');
$cacheable = $this->cacheable;
foreach ($elements as $element) {
$blockName = $element->getBlockName();
if ($blockName !== false && $this->structure->hasElement($blockName)) {
$cacheable = false;
break;
}
}

$this->isCacheableCache = $cacheable;
}

return $cacheable;
return $this->isCacheableCache;
}

/**
Expand Down