Build flat table column and index maps in one array_merge call (#41098) - #41096
Open
lbajsarowicz wants to merge 1 commit into
Open
Build flat table column and index maps in one array_merge call (#41098)#41096lbajsarowicz wants to merge 1 commit into
lbajsarowicz wants to merge 1 commit into
Conversation
getFlatColumns() and getFlatIndexes() merged every attribute's contribution into the accumulated map on each iteration, so each iteration copied the whole map built so far. Both loops run once per flat attribute, which makes the cost grow with the square of the attribute count. Collect the per-attribute maps and merge them once with argument unpacking. The merge order is unchanged, so a later attribute still overrides an earlier key exactly as before, and the phpcs:ignore annotations for Magento2.Performance.ForeachArrayMerge are no longer needed.
|
Hi @lbajsarowicz. Thank you for your contribution!
Allowed build names are:
You can find more information about the builds here For more details, review the Code Contributions documentation. |
Contributor
Author
|
@magento run all tests |
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.
Fixed Issues (if relevant)
That issue covers three modules, so this pull request is one of three and does not close it on its own.
Description (*)
Catalog\Helper\Product\Flat\Indexer::getFlatColumns()and::getFlatIndexes()build the flattable structure by merging each attribute's contribution into the map accumulated so far:
array_merge()allocates a new array and copies both operands, so a loop of this shape copiesthe entire partial map once per attribute. The work grows with the square of the attribute
count while the useful output grows linearly. Both loops run once per flat attribute, and the
methods are called per store while (re)building
catalog_product_flat_*.Both loops now collect the per-attribute maps and merge them once with argument unpacking:
The merge order is unchanged, so a later attribute still overrides an earlier key exactly as
before. The
// phpcs:ignore Magento2.Performance.ForeachArrayMergeannotations on both linesare no longer needed.
Measured effect
Merge cost only (attribute loading excluded, so this isolates what the change touches), PHP
8.5, median of 5 runs, one column per attribute:
Modest per call in absolute terms, and it is paid per store on every flat rebuild — a
multi-store installation with a wide attribute set pays it repeatedly. The shape is also the
thing the coding standard asks contributors not to write, so the annotations disappearing is
part of the point.
Related
Magento2.Performance.ForeachArrayMergeis the sniff meant to catch this shape, but it warnson every
array_mergeinside afor/foreachbody — including calls that copy a constantamount of data — and never inspects
while/dobodies. 2.4-develop carries 77phpcs:ignore Magento2.Performance.ForeachArrayMergeannotations as a result, these two amongthem.
magento/magento-coding-standard#503 narrows the sniff to the accumulating shape and adds
while/docoverage (126 raw detections in 2.4-develop → 74, plus one new true finding).Reviewing and merging it would make the remaining suppressions meaningful again — input from
the core team there is very welcome.
Manual testing scenarios (*)
bin/magento indexer:reindex catalog_product_flatand confirm the generatedcatalog_product_flat_*tables have the same columns and indexes as before the change(
SHOW CREATE TABLE catalog_product_flat_1).column and its index appear.
vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Magento/Catalog/Test/Unit/Helper/— 44 tests green.
Contribution checklist (*)