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

Open
wants to merge 4 commits into
base: 7.4
Choose a base branch
from

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.

use of an optimized role ustacking function
IMHO the test should not assert a particular role order, the method does not imply that
Co-authored-by: Christophe Coevoet <stof@notk.org>
@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
Co-authored-by: Nicolas Grekas <nicolas.grekas@gmail.com>
@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.

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
6 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy