Skip to content

Commit 2129de0

Browse files
Reduce memory usage during coverage merging by avoiding intermediate array
Instead of deserializing all coverage files into an array upfront and then iterating over them twice (once for validation, once for merging), process each file incrementally in a single pass. Test results are collected as a list of arrays and merged with array_merge(...$mergedTestResults) at the end. This change removes the $items array that held all deserialized data simultaneously, reducing peak memory usage when merging many coverage files. It also eliminates the redundant second loop by combining validation and merging into one pass.
1 parent 139c91b commit 2129de0

1 file changed

Lines changed: 9 additions & 15 deletions

File tree

src/Serialization/Merger.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,17 @@ public function merge(array $paths): array
4141

4242
$unserializer = new Unserializer;
4343

44-
$items = [];
45-
46-
foreach ($paths as $path) {
47-
$items[] = $unserializer->unserialize($path);
48-
}
49-
50-
$first = $items[0];
44+
$first = $unserializer->unserialize($paths[0]);
5145
$refRuntime = $first['buildInformation']['runtime'];
5246
$refDriver = $first['buildInformation']['phpCodeCoverage']['driverInformation'];
5347
$refHasGit = array_key_exists('git', $first['buildInformation']);
5448
$refGit = $first['buildInformation']['git'] ?? null;
5549

56-
foreach (array_slice($items, 1) as $item) {
50+
$mergedCoverage = clone $first['codeCoverage'];
51+
$mergedTestResults = [$first['testResults']];
52+
53+
foreach (array_slice($paths, 1) as $path) {
54+
$item = $unserializer->unserialize($path);
5755
$runtime = $item['buildInformation']['runtime'];
5856

5957
if ($runtime['name'] !== $refRuntime['name'] || $runtime['version'] !== $refRuntime['version']) {
@@ -89,14 +87,10 @@ public function merge(array $paths): array
8987
);
9088
}
9189
}
92-
}
93-
94-
$mergedCoverage = clone $first['codeCoverage'];
95-
$mergedTestResults = $first['testResults'];
9690

97-
foreach (array_slice($items, 1) as $item) {
9891
$mergedCoverage->merge($item['codeCoverage']);
99-
$mergedTestResults = array_merge($mergedTestResults, $item['testResults']);
92+
93+
$mergedTestResults[] = $item['testResults'];
10094
}
10195

10296
$buildInformation = [
@@ -116,7 +110,7 @@ public function merge(array $paths): array
116110
'buildInformation' => $buildInformation,
117111
'basePath' => $first['basePath'],
118112
'codeCoverage' => $mergedCoverage,
119-
'testResults' => $mergedTestResults,
113+
'testResults' => array_merge(...$mergedTestResults),
120114
];
121115
}
122116
}

0 commit comments

Comments
 (0)