#107922Refactor use of mergeRecursiveWithOverrule to define the features of the function explicitly + extend the function to use array_replace_recursive if applicable
Description
Compared to mergeRecursiveWithOverrule, array_replace_recursive is roughly 10x faster. This comparison becomes more favorable, the larger the arrays are, that are joined. The function mergeRecursiveWithOverrule has some settings, that are defined to default values making it “incompatible” with the specific mechanism of array_replace_recursive. Scanning through the core I found that almost all calls to mergeRecursiveWithOverrule are done without specific definition of the signature, using defaults. I think in most places the __UNSET feature is neither used, nor supported and therefore the function could be called to behave exactly like array_replace_recursive.
I am not knowledgeable enough in all these sections in their complete intent, but I would suggest that especially the newer sections of YAML processing, Site Config etc. can be quickly reviewed by their maintainers – given the fact their intent and memory is fresh enough – and at the very least change the function call of mergeRecursiveWithOverrule to behave specifically like they NEED it to. For example: If it is called in an area that doesn’t even make use of __UNSET, specify the parameter as false.
Then it would be possible to add this to the head of the function call of mergeRecursiveWithOverrule:
if ($addKeys && $includeEmptyValues && !$enableUnsetFeature) {
$original = array_replace_recursive($original, $overrule);
return;
}
Once the intent of the function call is really defined this condition would switch to the MUCH faster PHP core function. Either that or maybe if the maintainer is sure he/she could just switch to array_replace_recursive directly.
Since array merging happens A LOT in the core and with quite large and deep arrays, this change could speed up the core processing A LOT. A reasonably complex and deep array takes 2ms to merge with mergeRecursiveWithOverrule on an average server and 0.3ms with array_replace_recursive. Now multiply that by dozens of TCA merges, YAML merges, config merges, extension merges etc. etc. and you suddenly gain 50ms in a single execute.
I know micro optimization is a curse and all that jazz… but in this case… with a little bit of thought, this would even make the code more robust and so much faster with such little effort, I think it’s worth the time.
Reviews 0
No change resolves or names this issue in its commit message, and no comment links one.
History 2
-
Benni Mack changed target version <span data-processor="relativetime" data-value="2026-04-21T11:25:13+00:00" > 04/21/26, 11:25 </span>
-
Georg Ringer commented and changed status
NewClosed <span data-processor="relativetime" data-value="2026-11-20T21:36:13+00:00" > 11/20/26, 21:36 </span>
Hi Johannes, thanks a lot for the report and for looking into the performance of array merging!
I had a closer look and benchmarked this (PHP 8.5, 200 runs each, same structure and fully overlapping keys):
So
array_replace_recursiveis faster, but by about 4.6x rather than 10x, and the absolute saving stays well below a millisecond per merge, even for arrays with more than 10,000 nodes. Most of the merges in the Core also happen while building caches, not on every request, so the effect on a regular request is very small.More importantly, the proposed shortcut is not behaviour-compatible, even with
$enableUnsetFeature = false. If the original value is an array and the overrule value is a scalar,mergeRecursiveWithOverrule()keeps the array (see ArrayUtility.php), whilearray_replace_recursive()replaces it with the scalar:Switching the default path would therefore silently change the result for TCA, TypoScript, Site configuration and YAML merges whenever integrators override a structure with a scalar. In addition, there are around 86 callers in the Core, and the default call also honours
__UNSET, so deciding per call site whether the feature is needed would be a large audit with a high risk of subtle regressions, for a gain that is hard to measure in practice.For these reasons I am closing this as “won’t do”. Replacing single, profiled hotspots with
array_replace_recursive()where the semantics are known to be identical is still fine, of course, and would be a much more targeted change than touching the shared utility.Thanks again for the effort and the idea, feel free to pick this up again with profiling data pointing to a concrete hotspot.