Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d42e80c

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 d6619f7 commit d42e80c

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
@@ -417,37 +417,44 @@ function access_has_bug_level( $p_access_level, $p_bug_id, $p_user_id = null ) {
417417
}
418418

419419
$t_project_id = bug_get_field( $p_bug_id, 'project_id' );
420+
$t_bug_is_user_reporter = bug_is_user_reporter( $p_bug_id, $p_user_id );
421+
$t_access_level = access_get_project_level( $t_project_id, $p_user_id );
420422

421423
# check limit_Reporter (Issue #4769)
422424
# reporters can view just issues they reported
423425
$t_limit_reporters = config_get( 'limit_reporters', null, $p_user_id, $t_project_id );
424-
$t_report_bug_threshold = config_get( 'report_bug_threshold', null, $p_user_id, $t_project_id );
425-
if( !is_array( $t_report_bug_threshold ) ) {
426-
$t_report_bug_threshold = array( $t_report_bug_threshold );
427-
}
428-
if( $t_limit_reporters && !bug_is_user_reporter( $p_bug_id, $p_user_id ) ) {
429-
$t_has_access = false;
430-
foreach( $t_report_bug_threshold as $t_threshold ) {
431-
if( access_has_project_level( $t_threshold + 1, $t_project_id, $p_user_id ) ) {
432-
$t_has_access = true;
433-
break;
426+
if( $t_limit_reporters && !$t_bug_is_user_reporter ) {
427+
# Here we only need to check that the current user has an access level
428+
# higher than the lowest needed to report issues (report_bug_threshold).
429+
# To improve performance, esp. when processing for several projects, we
430+
# build a static array holding that threshold for each project
431+
static $s_thresholds = array();
432+
if( !isset( $s_thresholds[$t_project_id] ) ) {
433+
$t_report_bug_threshold = config_get( 'report_bug_threshold', null, $p_user_id, $t_project_id );
434+
if( !is_array( $t_report_bug_threshold ) ) {
435+
$s_thresholds[$t_project_id] = $t_report_bug_threshold;
436+
} else if ( empty( $t_report_bug_threshold ) ) {
437+
$s_thresholds[$t_project_id] = NOBODY;
438+
} else {
439+
sort( $t_report_bug_threshold );
440+
$s_thresholds[$t_project_id] = $t_report_bug_threshold[0];
434441
}
435442
}
436-
if( !$t_has_access ) {
443+
444+
if( !access_compare_level( $s_thresholds[$t_project_id], $t_access_level ) ) {
437445
return false;
438446
}
439447
}
440448

441449
# If the bug is private and the user is not the reporter, then
442450
# they must also have higher access than private_bug_threshold
443-
if( bug_get_field( $p_bug_id, 'view_state' ) == VS_PRIVATE && !bug_is_user_reporter( $p_bug_id, $p_user_id ) ) {
444-
$t_access_level = access_get_project_level( $t_project_id, $p_user_id );
451+
if( !$t_bug_is_user_reporter && bug_get_field( $p_bug_id, 'view_state' ) == VS_PRIVATE ) {
445452
$t_private_bug_threshold = config_get( 'private_bug_threshold', null, $p_user_id, $t_project_id );
446453
return access_compare_level( $t_access_level, $t_private_bug_threshold )
447-
&& access_compare_level( $t_access_level, $p_access_level );
454+
&& access_compare_level( $t_access_level, $p_access_level );
448455
}
449456

450-
return access_has_project_level( $p_access_level, $t_project_id, $p_user_id );
457+
return access_compare_level( $p_access_level, $t_access_level );
451458
}
452459

453460
/**

0 commit comments

Comments
 (0)
Please sign in to comment.