Skip to content

performance issues with RoleHierarchy #57322

@simonjamain

Description

@simonjamain

Symfony version(s) affected

7.1.1

Description

The RoleHierarchy::buildRoleMap() can be slow when there is a big hierrachy to build.

For context, in our project (https://gitlab.com/incubateur-territoires/france-relance/silab/silab-front) we have a dynamic role tree that rely extensively on the symfony implementation of RoleHierarchy.

After using a profiler we saw that it took 9 seconds to build our role map (~4s in production).

The use of array_shift in the folowing line is the culprit :

while ($role = array_shift($additionalRoles)) {

We changed it for an array_pop which is known to have a better complexity and we dropped from ~9s to ~50ms in dev env (RoleHierarchy::buildRoleMap() execution time).

AFAIK the array_shift behavior is not needed in that loop and array_pop return the same elements in the role map.

How to reproduce

You can time the execution of the main buildRoleMap loop (either with a profiler or with your logger) with the original array_shift and with the array_pop

Possible Solution

replace the use of array_shift with array_pop in the folowing line

while ($role = array_shift($additionalRoles)) {

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions