Problem/Motivation

The line $cache_tags = array_values(array_unique(array_merge(...$cache_tags))); inside Drupal's \Drupal\Core\Cache\Cache::mergeTags function can cause performance bottlenecks when handling massive arrays because array_merge with the unpacking operator (...) and array_unique process every single item linearly in memory.

Steps to reproduce

  • Create a entity reference view display for a content type which has a large number of nodes(10000 plus nodes)
  • Enable tag based caching for the view
  • Create a field reference using the view display in an another content type.
  • Try to create new content for the second type. Notice that the page load is slow

The issue is not limited to this scenario. It can happen when the drupal site runs on a slower cpu/memory system when using the function mergeTags with a lot of cache tags.

Proposed resolution

Optimize the code below in \Drupal\Core\Cache\Cache::mergeTags
$cache_tags = array_values(array_unique(array_merge(...$cache_tags)));

Comments

ramarajuk created an issue. See original summary.

longwave’s picture