8000 minor #43290 Move array_merge calls out of loops to improve performan… · symfony/symfony@a9810e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit a9810e0

Browse 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

File tree

19 files changed

+113
-75
lines changed

19 files changed

+113
-75
lines changed

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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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+
< D306 span class=pl-c1>$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

Lines 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

Lines 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

Lines 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

Lines 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

0 commit comments

Comments
 (0)
0