Skip to content

Commit f977b3c

Browse files
committedJan 23, 2013
Fix huge memory consumption for print_user_option_list()
Following the implementation of the fix for 0010130, calling this function when the current project is ALL_PROJECTS causes a massive surge in memory usage as the code builds a large array containing the list of all users in all projects accessible to the current user, and then reduces it to remove duplicates. This commit reduces the problem by removing calls to array_merge() and building the consolidated user list in a single pass, using a while loop. No-longer-used arrays are unset to free up memory. Fixes #15411
1 parent 5f641fc commit f977b3c

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed
 

‎core/print_api.php

+12-7
Original file line numberDiff line numberDiff line change
@@ -254,16 +254,20 @@ function print_user_option_list( $p_user_id, $p_project_id = null, $p_access = A
254254
$t_projects = user_get_accessible_projects( $t_current_user );
255255

256256
# Get list of users having access level for all accessible projects
257-
$t_users_temp = array();
257+
$t_users = array();
258258
foreach( $t_projects as $t_project_id ) {
259259
$t_project_users_list = project_get_all_user_rows( $t_project_id, $p_access );
260-
$t_users_temp = array_merge( $t_users_temp, $t_project_users_list );
260+
# Do a 'smart' merge of the project's user list, into an
261+
# associative array (to remove duplicates)
262+
# Use a while loop for better performance
263+
$i = 0;
264+
while( isset( $t_project_users_list[$i] ) ) {
265+
$t_users[ $t_project_users_list[$i]['id'] ] = $t_project_users_list[$i];
266+
$i++;
261267
}
262-
263-
# Build list of users as an associative array (to remove duplicates)
264-
foreach( $t_users_temp as $t_user ) {
265-
$t_users[$t_user['id']] = $t_user;
268+
unset( $t_project_users_list );
266269
}
270+
unset( $t_projects );
267271
} else {
268272
$t_users = project_get_all_user_rows( $p_project_id, $p_access );
269273
}
@@ -288,7 +292,8 @@ function print_user_option_list( $p_user_id, $p_project_id = null, $p_access = A
288292
$t_sort[] = $t_sort_name;
289293
}
290294
array_multisort( $t_sort, SORT_ASC, SORT_STRING, $t_users, $t_display );
291-
$t_count = count( $t_sort );
295+
unset( $t_sort );
296+
$t_count = count( $t_users );
292297
for( $i = 0;$i < $t_count;$i++ ) {
293298
$t_row = $t_users[$i];
294299
echo '<option value="' . $t_row['id'] . '" ';

0 commit comments

Comments
 (0)
Please sign in to comment.