Skip to content

Commit 5e14528

Browse files
committedFeb 26, 2013
Optimize performance of access_has_bug_level() api function
Through use of a static array to cache the reporter threshold when $g_limit_reporter = ON and more importantly reducing the total number of API calls, the performance of this function has been improved by 65% (stress tests executed over 2000 iterations, 0.34s vs 0.22s). Issue #15538, follow up on b3276bb
1 parent 84567ae commit 5e14528

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed
 

‎core/access_api.php

+22-15
Original file line numberDiff line numberDiff line change
@@ -412,37 +412,44 @@ function access_has_bug_level( $p_access_level, $p_bug_id, $p_user_id = null ) {
412412
}
413413

414414
$t_project_id = bug_get_field( $p_bug_id, 'project_id' );
415+
$t_bug_is_user_reporter = bug_is_user_reporter( $p_bug_id, $p_user_id );
416+
$t_access_level = access_get_project_level( $t_project_id, $p_user_id );
415417

416418
# check limit_Reporter (Issue #4769)
417419
# reporters can view just issues they reported
418420
$t_limit_reporters = config_get( 'limit_reporters', null, $p_user_id, $t_project_id );
419-
$t_report_bug_threshold = config_get( 'report_bug_threshold', null, $p_user_id, $t_project_id );
420-
if( !is_array( $t_report_bug_threshold ) ) {
421-
$t_report_bug_threshold = array( $t_report_bug_threshold );
422-
}
423-
if( $t_limit_reporters && !bug_is_user_reporter( $p_bug_id, $p_user_id ) ) {
424-
$t_has_access = false;
425-
foreach( $t_report_bug_threshold as $t_threshold ) {
426-
if( access_has_project_level( $t_threshold + 1, $t_project_id, $p_user_id ) ) {
427-
$t_has_access = true;
428-
break;
421+
if( $t_limit_reporters && !$t_bug_is_user_reporter ) {
422+
# Here we only need to check that the current user has an access level
423+
# higher than the lowest needed to report issues (report_bug_threshold).
424+
# To improve performance, esp. when processing for several projects, we
425+
# build a static array holding that threshold for each project
426+
static $s_thresholds = array();
427+
if( !isset( $s_thresholds[$t_project_id] ) ) {
428+
$t_report_bug_threshold = config_get( 'report_bug_threshold', null, $p_user_id, $t_project_id );
429+
if( !is_array( $t_report_bug_threshold ) ) {
430+
$s_thresholds[$t_project_id] = $t_report_bug_threshold;
431+
} else if ( empty( $t_report_bug_threshold ) ) {
432+
$s_thresholds[$t_project_id] = NOBODY;
433+
} else {
434+
sort( $t_report_bug_threshold );
435+
$s_thresholds[$t_project_id] = $t_report_bug_threshold[0];
429436
}
430437
}
431-
if( !$t_has_access ) {
438+
439+
if( !access_compare_level( $s_thresholds[$t_project_id], $t_access_level ) ) {
432440
return false;
433441
}
434442
}
435443

436444
# If the bug is private and the user is not the reporter, then
437445
# they must also have higher access than private_bug_threshold
438-
if( bug_get_field( $p_bug_id, 'view_state' ) == VS_PRIVATE && !bug_is_user_reporter( $p_bug_id, $p_user_id ) ) {
439-
$t_access_level = access_get_project_level( $t_project_id, $p_user_id );
446+
if( !$t_bug_is_user_reporter && bug_get_field( $p_bug_id, 'view_state' ) == VS_PRIVATE ) {
440447
$t_private_bug_threshold = config_get( 'private_bug_threshold', null, $p_user_id, $t_project_id );
441448
return access_compare_level( $t_access_level, $t_private_bug_threshold )
442-
&& access_compare_level( $t_access_level, $p_access_level );
449+
&& access_compare_level( $t_access_level, $p_access_level );
443450
}
444451

445-
return access_has_project_level( $p_access_level, $t_project_id, $p_user_id );
452+
return access_compare_level( $p_access_level, $t_access_level );
446453
}
447454

448455
/**

0 commit comments

Comments
 (0)
Please sign in to comment.