Skip to content

Performance impact because of category lookup in Model/Config/TemplateFinder.php #291

@igorwulff

Description

@igorwulff

The Model/Config/TemplateFinder.php class causes lots of recursive lookups in the category tree. It would be similar to having a look doing heavy db calls in it:

Image

    public function forCategory(Category $category, $type)
    {
        $attribute = $this->getAttribute($type);
        $templateId = (int) $category->getData($attribute);

        if ($templateId === RecommendationOption::OPTION_CODE) {
            $groupAttribute = $this->getGroupCodeAttribute($type);
            return (string) $category->getData($groupAttribute);
        }

        if ($templateId) {
            return $templateId;
        }

        if ($category->getParentId()) {
            $parent = $category->getParentCategory();
            return $this->forCategory($parent, $type);
        }

        return null;
    }

This is of course a well-known bad practice. My advice would be to do a smarter lookup, probably by not utilizing the repository pattern and using a dedicated prepared query for this, to do the lookup in one call. I've seen projects were almost a dozen category repository calls were done on a single PDP.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions