Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a9810e0

Browse filesBrowse files
minor #43290 Move array_merge calls out of loops to improve performance (simonberger)
This PR was merged into the 5.4 branch. Discussion ---------- Move array_merge calls out of loops to improve performance | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | no | Deprecations? | no | License | MIT See #43268 (comment) for a small performance benchmark. Commits ------- d0a6bfe Move array_merge calls out of loops to improve performance
2 parents 3e387cf + d0a6bfe commit a9810e0
Copy full SHA for a9810e0

File tree

Expand file treeCollapse file tree

19 files changed

+113
-75
lines changed
Filter options
Expand file treeCollapse file tree

19 files changed

+113
-75
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
+5-7Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,15 +1262,13 @@ private function addLockSection(ArrayNodeDefinition $rootNode, callable $enableI
12621262
->then(function ($v) {
12631263
$resources = [];
12641264
foreach ($v as $resource) {
1265-
$resources = array_merge_recursive(
1266-
$resources,
1267-
\is_array($resource) && isset($resource['name'])
1268-
? [$resource['name'] => $resource['value']]
1269-
: ['default' => $resource]
1270-
);
1265+
$resources[] = \is_array($resource) && isset($resource['name'])
1266+
? [$resource['name'] => $resource['value']]
1267+
: ['default' => $resource]
1268+
;
12711269
}
12721270

1273-
return $resources;
1271+
return array_merge_recursive([], ...$resources);
12741272
})
12751273
->end()
12761274
->prototype('array')

‎src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/SerializerCacheWarmerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/SerializerCacheWarmerTest.php
+25-6Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@
1515
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1616
use Symfony\Component\Cache\Adapter\NullAdapter;
1717
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
18+
use Symfony\Component\Serializer\Mapping\Loader\LoaderChain;
1819
use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader;
1920
use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader;
2021

2122
class SerializerCacheWarmerTest extends TestCase
2223
{
23-
public function testWarmUp()
24+
/**
25+
* @dataProvider loaderProvider
26+
*/
27+
public function testWarmUp(array $loaders)
2428
{
25-
$loaders = [
26-
new XmlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/person.xml'),
27-
new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/author.yml'),
28-
];
29-
3029
$file = sys_get_temp_dir().'/cache-serializer.php';
3130
@unlink($file);
3231

@@ -41,6 +40,26 @@ public function testWarmUp()
4140
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit());
4241
}
4342

43+
public function loaderProvider()
44+
{
45+
return [
46+
[
47+
[
48+
new LoaderChain([
49+
new XmlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/person.xml'),
50+
new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/author.yml'),
51+
]),
52+
],
53+
],
54+
[
55+
[
56+
new XmlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/person.xml'),
57+
new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/author.yml'),
58+
],
59+
],
60+
];
61+
}
62+
4463
public function testWarmUpWithoutLoader()
4564
{
4665
$file = sys_get_temp_dir().'/cache-serializer-without-loader.php';

‎src/Symfony/Bundle/TwigBundle/TemplateIterator.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/TemplateIterator.php
+7-8Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function getIterator(): \Traversable
4545
return $this->templates;
4646
}
4747

48-
$templates = null !== $this->defaultPath ? $this->findTemplatesInDirectory($this->defaultPath, null, ['bundles']) : [];
48+
$templates = null !== $this->defaultPath ? [$this->findTemplatesInDirectory($this->defaultPath, null, ['bundles'])] : [];
4949

5050
foreach ($this->kernel->getBundles() as $bundle) {
5151
$name = $bundle->getName();
@@ -55,18 +55,17 @@ public function getIterator(): \Traversable
5555

5656
$bundleTemplatesDir = is_dir($bundle->getPath().'/Resources/views') ? $bundle->getPath().'/Resources/views' : $bundle->getPath().'/templates';
5757

58-
$templates = array_merge(
59-
$templates,
60-
$this->findTemplatesInDirectory($bundleTemplatesDir, $name),
61-
null !== $this->defaultPath ? $this->findTemplatesInDirectory($this->defaultPath.'/bundles/'.$bundle->getName(), $name) : []
62-
);
58+
$templates[] = $this->findTemplatesInDirectory($bundleTemplatesDir, $name);
59+
if (null !== $this->defaultPath) {
60+
$templates[] = $this->findTemplatesInDirectory($this->defaultPath.'/bundles/'.$bundle->getName(), $name);
61+
}
6362
}
6463

6564
foreach ($this->paths as $dir => $namespace) {
66-
$templates = array_merge($templates, $this->findTemplatesInDirectory($dir, $namespace));
65+
$templates[] = $this->findTemplatesInDirectory($dir, $namespace);
6766
}
6867

69-
return $this->templates = new \ArrayIterator(array_unique($templates));
68+
return $this->templates = new \ArrayIterator(array_unique(array_merge([], ...$templates)));
7069
}
7170

7271
/**

‎src/Symfony/Component/Console/Application.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Application.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,14 +573,14 @@ public function getNamespaces()
573573
continue;
574574
}
575575

576-
$namespaces = array_merge($namespaces, $this->extractAllNamespaces($command->getName()));
576+
$namespaces[] = $this->extractAllNamespaces($command->getName());
577577

578578
foreach ($command->getAliases() as $alias) {
579-
$namespaces = array_merge($namespaces, $this->extractAllNamespaces($alias));
579+
$namespaces[] = $this->extractAllNamespaces($alias);
580580
}
581581
}
582582

583-
return array_values(array_unique(array_filter($namespaces)));
583+
return array_values(array_unique(array_filter(array_merge([], ...$namespaces))));
584584
}
585585

586586
/**

‎src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/ContainerBuilder.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,10 +1280,10 @@ public function findTags()
12801280
{
12811281
$tags = [];
12821282
foreach ($this->getDefinitions() as $id => $definition) {
1283-
$tags = array_merge(array_keys($definition->getTags()), $tags);
1283+
$tags[] = array_keys($definition->getTags());
12841284
}
12851285

1286-
return array_unique($tags);
1286+
return array_unique(array_merge([], ...$tags));
12871287
}
12881288

12891289
/**

‎src/Symfony/Component/DependencyInjection/Dumper/GraphvizDumper.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Dumper/GraphvizDumper.php
+8-9Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,22 @@ private function findEdges(string $id, array $arguments, bool $required, string
130130
$lazyEdge = $lazy || $this->container->getDefinition((string) $argument)->isLazy();
131131
}
132132

133-
$edges[] = ['name' => $name, 'required' => $required, 'to' => $argument, 'lazy' => $lazyEdge];
133+
$edges[] = [['name' => $name, 'required' => $required, 'to' => $argument, 'lazy' => $lazyEdge]];
134134
} elseif ($argument instanceof ArgumentInterface) {
135-
$edges = array_merge($edges, $this->findEdges($id, $argument->getValues(), $required, $name, true));
135+
$edges[] = $this->findEdges($id, $argument->getValues(), $required, $name, true);
136136
} elseif ($argument instanceof Definition) {
137-
$edges = array_merge($edges,
138-
$this->findEdges($id, $argument->getArguments(), $required, ''),
139-
$this->findEdges($id, $argument->getProperties(), false, '')
140-
);
137+
$edges[] = $this->findEdges($id, $argument->getArguments(), $required, '');
138+
$edges[] = $this->findEdges($id, $argument->getProperties(), false, '');
139+
141140
foreach ($argument->getMethodCalls() as $call) {
142-
$edges = array_merge($edges, $this->findEdges($id, $call[1], false, $call[0].'()'));
141+
$edges[] = $this->findEdges($id, $call[1], false, $call[0].'()');
143142
}
144143
} elseif (\is_array($argument)) {
145-
$edges = array_merge($edges, $this->findEdges($id, $argument, $required, $name, $lazy));
144+
$edges[] = $this->findEdges($id, $argument, $required, $name, $lazy);
146145
}
147146
}
148147

149-
return $edges;
148+
return array_merge([], ...$edges);
150149
}
151150

152151
private function findNodes(): array

‎src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,18 @@ public function testReferencingDeprecatedPublicService()
17531753

17541754
$this->addToAssertionCount(1);
17551755
}
1756+
1757+
public function testFindTags()
1758+
{
1759+
$container = new ContainerBuilder();
1760+
$container
1761+
->register(A::class)
1762+
->addTag('tag1')
1763+
->addTag('tag2')
1764+
->addTag('tag3');
1765+
1766+
$this->assertSame(['tag1', 'tag2', 'tag3'], $container->findTags());
1767+
}
17561768
}
17571769

17581770
class FooClass

‎src/Symfony/Component/DomCrawler/Form.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DomCrawler/Form.php
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ public function getPhpValues()
143143
if (!empty($qs)) {
144144
parse_str($qs, $expandedValue);
145145
$varName = substr($name, 0, \strlen(key($expandedValue)));
146-
$values = array_replace_recursive($values, [$varName => current($expandedValue)]);
146+
$values[] = [$varName => current($expandedValue)];
147147
}
148148
}
149149

150-
return $values;
150+
return array_replace_recursive([], ...$values);
151151
}
152152

153153
/**
@@ -182,11 +182,11 @@ function (&$value, $key) {
182182

183183
reset($expandedValue);
184184

185-
$values = array_replace_recursive($values, [$varName => current($expandedValue)]);
185+
$values[] = [$varName => current($expandedValue)];
186186
}
187187
}
188188

189-
return $values;
189+
return array_replace_recursive([], ...$values);
190190
}
191191

192192
/**

‎src/Symfony/Component/ErrorHandler/DebugClassLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/ErrorHandler/DebugClassLoader.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ private function setReturnType(string $types, string $class, string $method, str
800800
continue;
801801
}
802802

803-
$docTypes = array_merge($docTypes, $t);
803+
$docTypes[] = $t;
804804

805805
if ('mixed' === $n || 'void' === $n) {
806806
$nullable = false;
@@ -817,6 +817,7 @@ private function setReturnType(string $types, string $class, string $method, str
817817
$phpTypes[] = $n;
818818
}
819819
}
820+
$docTypes = array_merge([], ...$docTypes);
820821

821822
if (!$phpTypes) {
822823
return;

‎src/Symfony/Component/ErrorHandler/ErrorEnhancer/ClassNotFoundErrorEnhancer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/ErrorHandler/ErrorEnhancer/ClassNotFoundErrorEnhancer.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,19 @@ private function getClassCandidates(string $class): array
9393
if ($function[0] instanceof ClassLoader) {
9494
foreach ($function[0]->getPrefixes() as $prefix => $paths) {
9595
foreach ($paths as $path) {
96-
$classes = array_merge($classes, $this->findClassInPath($path, $class, $prefix));
96+
$classes[] = $this->findClassInPath($path, $class, $prefix);
9797
}
9898
}
9999

100100
foreach ($function[0]->getPrefixesPsr4() as $prefix => $paths) {
101101
foreach ($paths as $path) {
102-
$classes = array_merge($classes, $this->findClassInPath($path, $class, $prefix));
102+
$classes[] = $this->findClassInPath($path, $class, $prefix);
103103
}
104104
}
105105
}
106106
}
107107

108-
return array_unique($classes);
108+
return array_unique(array_merge([], ...$classes));
109109
}
110110

111111
private function findClassInPath(string $path, string $class, string $prefix): array

‎src/Symfony/Component/Finder/Finder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/Finder.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,16 +584,16 @@ public function in($dirs)
584584

585585
foreach ((array) $dirs as $dir) {
586586
if (is_dir($dir)) {
587-
$resolvedDirs[] = $this->normalizeDir($dir);
587+
$resolvedDirs[] = [$this->normalizeDir($dir)];
588588
} elseif ($glob = glob($dir, (\defined('GLOB_BRACE') ? \GLOB_BRACE : 0) | \GLOB_ONLYDIR | \GLOB_NOSORT)) {
589589
sort($glob);
590-
$resolvedDirs = array_merge($resolvedDirs, array_map([$this, 'normalizeDir'], $glob));
590+
$resolvedDirs[] = array_map([$this, 'normalizeDir'], $glob);
591591
} else {
592592
throw new DirectoryNotFoundException(sprintf('The "%s" directory does not exist.', $dir));
593593
}
594594
}
595595

596-
$this->dirs = array_merge($this->dirs, $resolvedDirs);
596+
$this->dirs = array_merge($this->dirs, ...$resolvedDirs);
597597

598598
return $this;
599599
}

‎src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,18 @@ public function mapViolation(ConstraintViolation $violation, FormInterface $form
185185

186186
if (null !== $this->translator) {
187187
$form = $scope;
188-
$translationParameters = $form->getConfig()->getOption('label_translation_parameters', []);
188+
$translationParameters[] = $form->getConfig()->getOption('label_translation_parameters', []);
189189

190190
do {
191191
$translationDomain = $form->getConfig()->getOption('translation_domain');
192-
$translationParameters = array_merge($form->getConfig()->getOption('label_translation_parameters', []), $translationParameters);
192+
array_unshift(
193+
$translationParameters,
194+
$form->getConfig()->getOption('label_translation_parameters', [])
195+
);
193196
} while (null === $translationDomain && null !== $form = $form->getParent());
194197

198+
$translationParameters = array_merge([], ...$translationParameters);
199+
195200
$label = $this->translator->trans(
196201
$label,
197202
$translationParameters,

‎src/Symfony/Component/Form/FormRegistry.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/FormRegistry.php
+3-6Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ public function getType(string $name)
100100
*/
101101
private function resolveType(FormTypeInterface $type): ResolvedFormTypeInterface
102102
{
103-
$typeExtensions = [];
104103
$parentType = $type->getParent();
105104
$fqcn = \get_class($type);
106105

@@ -111,17 +110,15 @@ private function resolveType(FormTypeInterface $type): ResolvedFormTypeInterface
111110

112111
$this->checkedTypes[$fqcn] = true;
113112

113+
$typeExtensions = [];
114114
try {
115115
foreach ($this->extensions as $extension) {
116-
$typeExtensions = array_merge(
117-
$typeExtensions,
118-
$extension->getTypeExtensions($fqcn)
119-
);
116+
$typeExtensions[] = $extension->getTypeExtensions($fqcn);
120117
}
121118

122119
return $this->resolvedTypeFactory->createResolvedType(
123120
$type,
124-
$typeExtensions,
121+
array_merge([], ...$typeExtensions),
125122
$parentType ? $this->getType($parentType) : null
126123
);
127124
} finally {

‎src/Symfony/Component/Form/FormTypeGuesserChain.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/FormTypeGuesserChain.php
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@ class FormTypeGuesserChain implements FormTypeGuesserInterface
2525
*/
2626
public function __construct(iterable $guessers)
2727
{
28+
$tmpGuessers = [];
2829
foreach ($guessers as $guesser) {
2930
if (!$guesser instanceof FormTypeGuesserInterface) {
3031
throw new UnexpectedTypeException($guesser, FormTypeGuesserInterface::class);
3132
}
3233

3334
if ($guesser instanceof self) {
34-
$this->guessers = array_merge($this->guessers, $guesser->guessers);
35+
$tmpGuessers[] = $guesser->guessers;
3536
} else {
36-
$this->guessers[] = $guesser;
37+
$tmpGuessers[] = [$guesser];
3738
}
3839
}
40+
41+
$this->guessers = array_merge([], ...$tmpGuessers);
3942
}
4043

4144
/**

‎src/Symfony/Component/HttpFoundation/Response.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Response.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,10 +1048,10 @@ public function getVary(): array
10481048

10491049
$ret = [];
10501050
foreach ($vary as $item) {
1051-
$ret = array_merge($ret, preg_split('/[\s,]+/', $item));
1051+
$ret[] = preg_split('/[\s,]+/', $item);
10521052
}
10531053

1054-
return $ret;
1054+
return array_merge([], ...$ret);
10551055
}
10561056

10571057
/**

‎src/Symfony/Component/HttpKernel/DependencyInjection/AddAnnotatedClassesToCachePass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/DependencyInjection/AddAnnotatedClassesToCachePass.php
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ public function __construct(Kernel $kernel)
3737
*/
3838
public function process(ContainerBuilder $container)
3939
{
40-
$annotatedClasses = $this->kernel->getAnnotatedClassesToCompile();
40+
$annotatedClasses = [];
4141
foreach ($container->getExtensions() as $extension) {
4242
if ($extension instanceof Extension) {
43-
$annotatedClasses = array_merge($annotatedClasses, $extension->getAnnotatedClassesToCompile());
43+
$annotatedClasses[] = $extension->getAnnotatedClassesToCompile();
4444
}
4545
}
4646

47+
$annotatedClasses = array_merge($this->kernel->getAnnotatedClassesToCompile(), ...$annotatedClasses);
48+
4749
$existingClasses = $this->getClassesInComposerClassMaps();
4850

4951
$annotatedClasses = $container->getParameterBag()->resolveValue($annotatedClasses);

‎src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,15 @@ public function getTypesFromConstructor(string $class, string $property): ?array
196196
/** @var DocBlock\Tags\Var_|DocBlock\Tags\Return_|DocBlock\Tags\Param $tag */
197197
foreach ($docBlock->getTagsByName('param') as $tag) {
198198
if ($tag && null !== $tag->getType()) {
199-
$types = array_merge($types, $this->phpDocTypeHelper->getTypes($tag->getType()));
199+
$types[] = $this->phpDocTypeHelper->getTypes($tag->getType());
200200
}
201201
}
202202

203203
if (!isset($types[0])) {
204204
return null;
205205
}
206206

207-
return $types;
207+
return array_merge([], ...$types);
208208
}
209209

210210
private function getDocBlockFromConstructor(string $class, string $property): ?DocBlock

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.