Skip to content

Open large category selectors faster after cache invalidation - #22

Open
lbajsarowicz wants to merge 1 commit into
LokiExtensions:masterfrom
lbajsarowicz:perf/open-large-category-selectors-faster-aft
Open

lbajsarowicz wants to merge 1 commit into
LokiExtensions:masterfrom
lbajsarowicz:perf/open-large-category-selectors-faster-aft

Conversation

@lbajsarowicz

@lbajsarowicz lbajsarowicz commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Building an uncached category selection tree scans the complete category list for every node, causing N × (N + 1) parent comparisons for a connected N-node tree. A search whose store, role and filter cache key has no saved tree also triggers this construction.

Fix

Group categories by parent ID once, then recurse through each parent's children in their original order. Keep levels, ancestor IDs, active flags and the serialized tree format unchanged.

Why this approach

The parent lookup removes repeated scans without changing category loading or caching; each category's parent_id is read once during indexing. Tests exercise public build() on a cache miss and cover empty and disconnected inputs, children loaded before parents, multiple roots, inactive categories, ancestor metadata, and flat trees of 1,000 and 5,000 categories.

Test plan

PHPUnit 10.5.67 against a Magento 2.4.9 install's unit test bootstrap on PHP 8.5.12, running Test/Unit: passed, 150 tests and 329 assertions.

phpcs --standard=Magento2 --ignore=Test/,node_modules/ --extensions=php,phtml --severity=9 Service/CategorySelection/CategoryTreeBuilder.php: passed.

PHPStan 2 with bitexpert/phpstan-magento at the configured level 1 against a Magento 2.4.9 install, scoped to the builder and its unit test: passed with no errors.

php -d error_reporting=E_ALL -l on both PHP files: passed.

The behavior tests pass against both the original and indexed builders; performance was verified by measurement.

On Magento Open Source 2.4.9 / PHP 8.5, the median of five uncached tree builds with 1,000 / 5,000 fixture categories improved from 17.962 → 14.920 ms / 263.805 → 68.346 ms; SQL query counts remained 3 → 3. Full serialized trees were byte-identical for unfiltered, broad-search and narrow-search cases at both sizes; cache-hit results matched. Setup upgrade, DI compilation and static content deployment passed. bin/magento setup:upgrade, setup:di:compile and setup:static-content:deploy -f en_US on Magento Open Source 2.4.9 / PHP 8.5 with this branch installed: all pass.

Each recursive node scans the full category list on a cache miss,
causing quadratic parent comparisons as the catalog grows.
Group categories by parent once while retaining their original order.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant