Skip to content

Commit 44f6c85

Browse files
committed
feature #61057 [Security] Improve performance of RoleHierarchy::buildRoleMap method (simonjamain-gp, simonjamain)
This PR was squashed before being merged into the 7.4 branch. Discussion ---------- [Security] Improve performance of `RoleHierarchy::buildRoleMap` method | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | no (perf) | Deprecations? | no | Issues | Fix #57322 | License | MIT use of an optimized role ustacking function ### What it does and why it's needed _Note : Better in-detail explanation in there : https://github.com/symfony/symfony/issues/57322_ Uses the way faster `array_pop()` function to build the role map instead of `array shift` ### If it modifies existing behavior, include a before/after comparison At first, it would look like this function swap could change slightly the ordering of the array produced by the `RoleHierarchy::buildRoleMap` method and it does it in a way. I find that it does not change the behaviour of our app. I would not expect most other apps too break because I don't find many reasons to rely on the ordering of roles in hierarchies. Furthermore, `buildRoleMap` is a protected method serving the public `getReachableRoleNames` which does not imply a particular ordering (rightfully so IMHO). ### Testing As it does not change or introduce any behavious per say, this performance increase rely on old tests passing again. If I am not mistaken, the current `testGetReachableRoleNames()` still passes even tho it asserts a specific ordering. I still changed the assertion so it does not convey any false premises. Commits ------- 508b62f [Security] Improve performance of `RoleHierarchy::buildRoleMap` method
2 parents ff682f8 + 508b62f commit 44f6c85

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/Symfony/Component/Security/Core/Role/RoleHierarchy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function buildRoleMap(): void
5454
$this->map[$main] = $roles;
5555
$visited = [];
5656
$additionalRoles = $roles;
57-
while ($role = array_shift($additionalRoles)) {
57+
while ($role = array_pop($additionalRoles)) {
5858
if (!isset($this->hierarchy[$role])) {
5959
continue;
6060
}

src/Symfony/Component/Security/Core/Tests/Role/RoleHierarchyTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public function testGetReachableRoleNames()
2323
'ROLE_SUPER_ADMIN' => ['ROLE_ADMIN', 'ROLE_FOO'],
2424
]);
2525

26-
$this->assertEquals(['ROLE_USER'], $role->getReachableRoleNames(['ROLE_USER']));
27-
$this->assertEquals(['ROLE_FOO'], $role->getReachableRoleNames(['ROLE_FOO']));
28-
$this->assertEquals(['ROLE_ADMIN', 'ROLE_USER'], $role->getReachableRoleNames(['ROLE_ADMIN']));
29-
$this->assertEquals(['ROLE_FOO', 'ROLE_ADMIN', 'ROLE_USER'], $role->getReachableRoleNames(['ROLE_FOO', 'ROLE_ADMIN']));
30-
$this->assertEquals(['ROLE_SUPER_ADMIN', 'ROLE_ADMIN', 'ROLE_FOO', 'ROLE_USER'], $role->getReachableRoleNames(['ROLE_SUPER_ADMIN']));
31-
$this->assertEquals(['ROLE_SUPER_ADMIN', 'ROLE_ADMIN', 'ROLE_FOO', 'ROLE_USER'], $role->getReachableRoleNames(['ROLE_SUPER_ADMIN', 'ROLE_SUPER_ADMIN']));
26+
$this->assertEqualsCanonicalizing(['ROLE_USER'], $role->getReachableRoleNames(['ROLE_USER']));
27+
$this->assertEqualsCanonicalizing(['ROLE_FOO'], $role->getReachableRoleNames(['ROLE_FOO']));
28+
$this->assertEqualsCanonicalizing(['ROLE_ADMIN', 'ROLE_USER'], $role->getReachableRoleNames(['ROLE_ADMIN']));
29+
$this->assertEqualsCanonicalizing(['ROLE_FOO', 'ROLE_ADMIN', 'ROLE_USER'], $role->getReachableRoleNames(['ROLE_FOO', 'ROLE_ADMIN']));
30+
$this->assertEqualsCanonicalizing(['ROLE_SUPER_ADMIN', 'ROLE_ADMIN', 'ROLE_FOO', 'ROLE_USER'], $role->getReachableRoleNames(['ROLE_SUPER_ADMIN']));
31+
$this->assertEqualsCanonicalizing(['ROLE_SUPER_ADMIN', 'ROLE_ADMIN', 'ROLE_FOO', 'ROLE_USER'], $role->getReachableRoleNames(['ROLE_SUPER_ADMIN', 'ROLE_SUPER_ADMIN']));
3232
}
3333
}

0 commit comments

Comments
 (0)