Cache tags management micro-optimisations

Created on 18 May 2018, over 6 years ago
Updated 28 March 2023, over 1 year ago

Problem/Motivation

There are 2 small performance hogs in the class \Drupal\Core\Cache\Cache

1. PHP's array_unique does not scale well with lists bigger than 2 items in:

public static function mergeTags(array $a = [], array $b = [])

2. Excessive string concatenations in:

public static function buildTags($prefix, array $suffixes, $glue = ':')

Proposed resolution

Providing a patch for it. It's an implementation detail.

There is an alternative to

array_unique(...)

and it's

array_keys(array_flip(...))

. Attaching a standalone php benchmark script for the 4 options that were examined. Uploaded as txt (due to d.o limitations).
Results from the benchmark on my laptop with PHP 7.1:

items: 1
array_unique($arr) (average of 10000): 84.83%
foreach + array_keys (average of 10000): 100%
array_keys(array_flip($arr)) (average of 10000): 90.02%
array_flip(array_flip($arr)) (average of 10000): 98.31%

items: 2
array_unique($arr) (average of 10000): 85.31%
foreach + array_keys (average of 10000): 100%
array_keys(array_flip($arr)) (average of 10000): 86.99%
array_flip(array_flip($arr)) (average of 10000): 99.5%

items: 4
array_unique($arr) (average of 10000): 86.02%
foreach + array_keys (average of 10000): 92.35%
array_keys(array_flip($arr)) (average of 10000): 73.98%
array_flip(array_flip($arr)) (average of 10000): 100%

items: 8
array_unique($arr) (average of 10000): 100%
foreach + array_keys (average of 10000): 98.86%
array_keys(array_flip($arr)) (average of 10000): 69.92%
array_flip(array_flip($arr)) (average of 10000): 90.4%

items: 16
array_unique($arr) (average of 10000): 100%
foreach + array_keys (average of 10000): 72.03%
array_keys(array_flip($arr)) (average of 10000): 39.54%
array_flip(array_flip($arr)) (average of 10000): 51.5%

items: 32
array_unique($arr) (average of 10000): 100%
foreach + array_keys (average of 10000): 67.37%
array_keys(array_flip($arr)) (average of 10000): 29.75%
array_flip(array_flip($arr)) (average of 10000): 38.92%

items: 64
array_unique($arr) (average of 10000): 100%
foreach + array_keys (average of 10000): 43.77%
array_keys(array_flip($arr)) (average of 10000): 17.9%
array_flip(array_flip($arr)) (average of 10000): 24.53%

items: 128
array_unique($arr) (average of 10000): 100%
foreach + array_keys (average of 10000): 42.53%
array_keys(array_flip($arr)) (average of 10000): 15.7%
array_flip(array_flip($arr)) (average of 10000): 21.6%

items: 1024
array_unique($arr) (average of 1000): 100%
foreach + array_keys (average of 1000): 25.32%
array_keys(array_flip($arr)) (average of 1000): 10.45%
array_flip(array_flip($arr)) (average of 1000): 12.06%

items: 10000
array_unique($arr) (average of 100): 100%
foreach + array_keys (average of 100): 21.71%
array_keys(array_flip($arr)) (average of 100): 8.73%
array_flip(array_flip($arr)) (average of 100): 10.26%

items: 100000
array_unique($arr) (average of 10): 100%
foreach + array_keys (average of 10): 26.23%
array_keys(array_flip($arr)) (average of 10): 12.09%
array_flip(array_flip($arr)) (average of 10): 14.48%

Remaining tasks

Review, commit.

User interface changes

None.

API changes

None.

Data model changes

None.

📌 Task
Status

Postponed: needs info

Version

10.1

Component
Cache 

Last updated about 11 hours ago

Created by

🇧🇬Bulgaria ndobromirov

Live updates comments and jobs are added and updated live.
  • Performance

    It affects performance. It is often combined with the Needs profiling tag.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • First commit to issue fork.
  • @arunkumark opened merge request.
  • Status changed to Needs review over 1 year ago
  • 🇮🇳India arunkumark Coimbatore

    I have rebased the 10.1.x version and tried to reroll the patch. Found the update for mergeTags() has been already done. So, updated the buildTags() method.

    Find the MR for the rerolled patch.

  • Status changed to Postponed: needs info over 1 year ago
  • 🇺🇸United States smustgrave

    Wonder if this is still a valid task.

    Not seeing on the MR 3721 is suppose to make the cache better?

  • 🇫🇷France nod_ Lille

    Wouldn't a Set be more appropriate here instead of array functions?

Production build 0.71.5 2024