Skip to content

Build cache tag id lists in one array_merge call (#41098) - #41095

Open
lbajsarowicz wants to merge 1 commit into
magento:2.4-developfrom
lbajsarowicz:perf/cache-tag-adapters-array-merge
Open

Build cache tag id lists in one array_merge call (#41098)#41095
lbajsarowicz wants to merge 1 commit into
magento:2.4-developfrom
lbajsarowicz:perf/cache-tag-adapters-array-merge

Conversation

@lbajsarowicz

@lbajsarowicz lbajsarowicz commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Fixed Issues (if relevant)

  1. Relates to array_merge() accumulators in loops make cache tag cleaning, flat indexing and Analytics collection quadratic #41098 — scope analysis and measurements for the whole pattern.
    That issue covers three modules, so this pull request is one of three and does not close it on its own.

Description (*)

Cleaning the cache by tags goes through Symfony::clean(), which calls
TagAdapterInterface::getIdsMatchingAnyTags() (CLEANING_MODE_MATCHING_ANY_TAG) or
::getIdsNotMatchingTags() (CLEANING_MODE_NOT_MATCHING_TAG). Three of those loops merged
each tag's id list into an accumulator on every iteration:

$ids = [];
foreach ($tags as $tag) {
    $ids = array_merge($ids, $this->getTagIds($tag));   // copies everything collected so far
}

array_merge() allocates a new array and copies both operands, so a loop of this shape
copies the whole partial result once per iteration — the total work grows with the square of
the number of tags, not linearly. FilesystemTagAdapter::getAllIds() does it once per tag
file in the cache directory, and a store that invalidates by entity tags accumulates a large
number of those.

The three loops now collect the per-tag lists and merge them once with argument unpacking:

$idsPerTag = [];
foreach ($tags as $tag) {
    $idsPerTag[] = $this->getTagIds($tag);
}

return array_values(array_unique(array_merge(...$idsPerTag)));

Same arrays, same order, same de-duplication — only the number of copies changes. The
// phpcs:ignore Magento2.Performance.ForeachArrayMerge annotations that were needed to keep
these lines quiet are gone.

RedisTagAdapter::getIdsMatchingAnyTags() gets the same treatment for its chunked SUNION
path (chunks of 500 tags, so the loop runs once per 500 tags).

Measured effect

FilesystemTagAdapter against a synthetic tag directory (20 ids per tag), PHP 8.5, median of
5 runs for 5 000 tags, single run for 20 000:

tags method before after
5 000 getIdsMatchingAnyTags() 3 097 ms 1 013 ms 3.1×
5 000 getIdsNotMatchingTags() 1 998 ms 395 ms 5.1×
20 000 getIdsMatchingAnyTags() 33 271 ms 4 185 ms 7.9×
20 000 getIdsNotMatchingTags() 27 823 ms 1 686 ms 16.5×

The remaining time is file reads and array_unique(), which this change does not touch. The
benchmark script is a plain PHP file that instantiates the adapter with a stub
CacheItemPoolInterface and a temporary tag directory — happy to add it to the description
if that helps review.

Related

The sniff that is supposed to catch this shape, Magento2.Performance.ForeachArrayMerge,
warns on every array_merge inside a for/foreach body — including calls that copy a
constant amount of data — and never looks inside while/do bodies at all. That is why
2.4-develop carries 77 phpcs:ignore Magento2.Performance.ForeachArrayMerge annotations,
and why these three loops were annotated instead of fixed.

magento/magento-coding-standard#503 narrows the sniff to the accumulating shape and adds
while/do coverage (126 raw detections in 2.4-develop → 74, one new true finding). Reviewing
and merging it would make the remaining suppressions in this repository meaningful again — the
core team's input there is very welcome.

Manual testing scenarios (*)

  1. Configure the Symfony filesystem cache adapter, warm the cache so that the tag directory
    holds a large number of tag files.
  2. Invalidate by tag (bin/magento cache:clean <type>, or save a product so catalog_product_*
    tags are cleaned) and confirm exactly the same cache entries are removed as before.
  3. vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist lib/internal/Magento/Framework/Cache/Test/Unit/
    — 336 tests green, including FilesystemTagAdapterTest and RedisTagAdapterTest.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All automated tests passed successfully (all builds are green)

FilesystemTagAdapter::getIdsMatchingAnyTags() and ::getAllIds() and
RedisTagAdapter::getIdsMatchingAnyTags() merged each tag's id list into an
accumulator on every iteration, so every iteration copied the whole result
built so far. Cleaning the cache by tags walks one iteration per tag, which
makes the cost grow with the square of the number of tags.

Collect the per-tag lists and merge them once with argument unpacking. The
resulting arrays and their order are identical, and the phpcs:ignore
annotations for Magento2.Performance.ForeachArrayMerge are no longer needed.
@m2-assistant

m2-assistant Bot commented Aug 5, 2026

Copy link
Copy Markdown

Hi @lbajsarowicz. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.
❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@lbajsarowicz

Copy link
Copy Markdown
Contributor Author

@magento run all tests

@lbajsarowicz lbajsarowicz changed the title Build cache tag id lists in one array_merge call Build cache tag id lists in one array_merge call (#41098) Aug 5, 2026
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