Skip to content

Commit

Permalink
Optimize user_get_all_accessible_projects()
Browse files Browse the repository at this point in the history
On instances having a large number of projects, this function would
consume significant resources while processing all the subprojects to
determine if one is accessible to the user (about 25 seconds to load
main_page.php for 5'000 projects, without subprojects).

The performance bottleneck was the array_merge() call in the loop. This
has been replaced by a foreach working on an associative array. The same
page now loads under 1 second.

Fixes #9876
  • Loading branch information
dregad committed May 1, 2013
1 parent 45a2d16 commit 3b392f7
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions core/user_api.php
Expand Up @@ -1019,12 +1019,18 @@ function user_get_all_accessible_subprojects( $p_user_id, $p_project_id ) {

function user_get_all_accessible_projects( $p_user_id, $p_project_id ) {
if( ALL_PROJECTS == $p_project_id ) {
$t_topprojects = $t_project_ids = user_get_accessible_projects( $p_user_id );
$t_topprojects = user_get_accessible_projects( $p_user_id );

# Create a combined array where key = value
$t_project_ids = array_combine( $t_topprojects, $t_topprojects );

# Add all subprojects user has access to
foreach( $t_topprojects as $t_project ) {
$t_project_ids = array_merge( $t_project_ids, user_get_all_accessible_subprojects( $p_user_id, $t_project ) );
$t_subprojects_ids = user_get_all_accessible_subprojects( $p_user_id, $t_project );
foreach( $t_subprojects_ids as $t_id ) {
$t_project_ids[$t_id] = $t_id;
}
}

$t_project_ids = array_unique( $t_project_ids );
} else {
access_ensure_project_level( VIEWER, $p_project_id );
$t_project_ids = user_get_all_accessible_subprojects( $p_user_id, $p_project_id );
Expand Down

0 comments on commit 3b392f7

Please sign in to comment.