10000 minor #24556 [Routing] Ensure uniqueness without repeated check (endr… · symfony/symfony@f757cd6 · GitHub
[go: up one dir, main page]

Skip to content

Commit f757cd6

Browse files
committed
minor #24556 [Routing] Ensure uniqueness without repeated check (endroid)
This PR was squashed before being merged into the 3.4 branch (closes #24556). Discussion ---------- [Routing] Ensure uniqueness without repeated check | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - The RouteCollection ensures uniqueness of resources by using array_unique. The current implementation suffers from the following problems. 1. The array_unique method calls __toString for every element in the array but it is very inefficient in terms of the number of calls made. I.e. if the array has 50 elements it does not do 50 __toString calls. I did some tests with Blackfire in PHP 7.1 and found the following numbers. 25 elements => 240 calls 50 elements => 607 calls 100 elements => 1382 calls 200 elements => 3333 calls 2. The array_unique function is called every time the getResources() method is called, even when the resources did not change in the mean time. Combined with the above this leads to lower performance. Many applications have a low number of routes so this is not a big issue. But for larger applications or bundles that generate routes (i.e. for CRUD or API / doc generation) this will have a bigger impact. Commits ------- 16f7281 [Routing] Ensure uniqueness without repeated check
2 parents 4a68775 + 16f7281 commit f757cd6

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/Symfony/Component/Routing/RouteCollection.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ public function addCollection(RouteCollection $collection)
128128
$this->routes[$name] = $route;
129129
}
130130

131-
$this->resources = array_merge($this->resources, $collection->getResources());
131+
foreach ($collection->getResources() as $resource) {
132+
$this->addResource($resource);
133+
}
132134
}
133135

134136
/**
@@ -262,16 +264,21 @@ public function setMethods($methods)
262264
*/
263265
public function getResources()
264266
{
265-
return array_unique($this->resources);
267+
return array_values($this->resources);
266268
}
267269

268270
/**
269-
* Adds a resource for this collection.
271+
* Adds a resource for this collection. If the resource already exists
272+
* it is not added.
270273
*
271274
* @param ResourceInterface $resource A resource instance
272275
*/
273276
public function addResource(ResourceInterface $resource)
274277
{
275-
$this->resources[] = $resource;
278+
$key = (string) $resource;
279+
280+
if (!isset($this->resources[$key])) {
281+
$this->resources[$key] = $resource;
282+
}
276283
}
277284
}

0 commit comments

Comments
 (0)
0