Repository navigation
Open large category selectors faster after cache invalidation - #22
Open
lbajsarowicz wants to merge 1 commit into
Open
lbajsarowicz wants to merge 1 commit into
lbajsarowicz wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 -lon 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:compileandsetup:static-content:deploy -f en_USon Magento Open Source 2.4.9 / PHP 8.5 with this branch installed: all pass.