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.