Skip to content

[Security] Improve performance of RoleHierarchy::buildRoleMap method #61057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2025

Conversation

simonjamain
Copy link

@simonjamain simonjamain commented Jul 7, 2025

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 : #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.

@GromNaN GromNaN changed the title Fix 57322 [Security] Improve performance of RoleHierarchy::buildRoleMap method Jul 7, 2025
@carsonbot carsonbot changed the title [Security] Improve performance of RoleHierarchy::buildRoleMap method Improve performance of RoleHierarchy::buildRoleMap method Jul 7, 2025
@carsonbot carsonbot changed the title Improve performance of RoleHierarchy::buildRoleMap method [Security] Improve performance of RoleHierarchy::buildRoleMap method Jul 8, 2025
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please bench this patch? It should preserve the order while still providing the perf benefit:

--- a/src/Symfony/Component/Security/Core/Role/RoleHierarchy.php
+++ b/src/Symfony/Component/Security/Core/Role/RoleHierarchy.php
@@ -54,8 +54,11 @@ class RoleHierarchy implements RoleHierarchyInterface
             $this->map[$main] = $roles;
             $visited = [];
             $additionalRoles = $roles;
-            while ($role = array_shift($additionalRoles)) {
+            while (null !== $role = key($additionalRoles)) {
+                $role = $additionalRoles[$role];
+
                 if (!isset($this->hierarchy[$role])) {
+                    next($additionalRoles);
                     continue;
                 }
 
@@ -68,6 +71,8 @@ class RoleHierarchy implements RoleHierarchyInterface
                 foreach (array_diff($this->hierarchy[$role], $visited) as $additionalRole) {
                     $additionalRoles[] = $additionalRole;
                 }
+
+                next($additionalRoles);
             }
 

@symfony symfony deleted a comment from carsonbot Jul 8, 2025
@simonjamain
Copy link
Author

Can you please bench this patch? It should preserve the order while still providing the perf benefit:

--- a/src/Symfony/Component/Security/Core/Role/RoleHierarchy.php
+++ b/src/Symfony/Component/Security/Core/Role/RoleHierarchy.php
@@ -54,8 +54,11 @@ class RoleHierarchy implements RoleHierarchyInterface
             $this->map[$main] = $roles;
             $visited = [];
             $additionalRoles = $roles;
-            while ($role = array_shift($additionalRoles)) {
+            while (null !== $role = key($additionalRoles)) {
+                $role = $additionalRoles[$role];
+
                 if (!isset($this->hierarchy[$role])) {
+                    next($additionalRoles);
                     continue;
                 }
 
@@ -68,6 +71,8 @@ class RoleHierarchy implements RoleHierarchyInterface
                 foreach (array_diff($this->hierarchy[$role], $visited) as $additionalRole) {
                     $additionalRoles[] = $additionalRole;
                 }
+
+                next($additionalRoles);
             }
 

Great, I'll check that this week.

@fabpot
Copy link
Member

fabpot commented Aug 23, 2025

Thank you @simonjamain.

@fabpot fabpot merged commit 44f6c85 into symfony:7.4 Aug 23, 2025
10 of 12 checks passed
fabpot added a commit that referenced this pull request Aug 24, 2025
…nicolas-grekas)

This PR was merged into the 7.4 branch.

Discussion
----------

[Security] Preserve ordering of roles in RoleHierarchy

| Q             | A
| ------------- | ---
| Branch?       | 7.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        | -
| License       | MIT

As suggested in #61057

/cc `@simonjamain`

Commits
-------

d4338b1 [Security] Preserve ordering of roles in RoleHierarchy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

performance issues with RoleHierarchy
7 participants