Skip to content
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

SSViewer::chooseTemplate sometimes do the work twice #10891

Open
lekoala opened this issue Jul 28, 2023 · 1 comment
Open

SSViewer::chooseTemplate sometimes do the work twice #10891

lekoala opened this issue Jul 28, 2023 · 1 comment

Comments

@lekoala
Copy link
Contributor

lekoala commented Jul 28, 2023

Affected Version

4/5

Description

Another finding from the new cache collector in the debugbar for v5.

When calling an include, it's first resolved based on an array of argument, then as a path. Typically, this happens for LeftAndMain_Menu

  • First, a call is made passing an array with a bunch of stuff
  • Then, another call is made with a path like this ...\silverstripe\subsites\templates\SilverStripe\Admin\LeftAndMain_Menu.ss"

This leads to two cache keys being created for the same entry (one when you give an array, one when you pass a file path)

// Look for a cached result for this data set
$cacheKey = md5(json_encode($template) . json_encode($themes));
if ($this->getCache()->has($cacheKey)) {
return $this->getCache()->get($cacheKey);
}

Setting the cache key itself is a good idea to avoid file_exists checks, but it feels like doing the work twice. Once resolved from the array, you know the file_exists, and it could be cached in a static array instead and avoid cache checks completely.

Proposal

So this is a quicky and dirty proposal, but it gives an idea

public static function chooseTemplate($templates)
{
    static $arr = [];
    if (is_string($templates) && isset($arr[$templates])) {
        return $templates;
    }
    $resolved = ThemeResourceLoader::inst()->findTemplate($templates, self::get_themes());
    $arr[$resolved] = true;
    return $resolved;
}

When a file is resolved, it's stored a in a static array. If we ask for this file again, simply return the file path directly without any cache checks.
If the team is interested, i'm happy to work on a PR.

It's by no means a big issue, but it saves 5 cache calls on a typical admin page for example, but it's probably more if you have lots of includes

@lekoala
Copy link
Contributor Author

lekoala commented Aug 11, 2023

side note: file_exists is also cached at php level directly, so maybe it's even simpler to simply return early and use file_exists checks when given a file path ending with .ss

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants