Permalink
Browse files

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
  • Loading branch information...
1 parent d6619f7 commit d42e80c01277eec37c7436a23ec72073c9961c49 @dregad dregad committed Feb 26, 2013
Showing with 22 additions and 15 deletions.
  1. +22 −15 core/access_api.php
View
37 core/access_api.php
@@ -417,37 +417,44 @@ function access_has_bug_level( $p_access_level, $p_bug_id, $p_user_id = null ) {
}
$t_project_id = bug_get_field( $p_bug_id, 'project_id' );
+ $t_bug_is_user_reporter = bug_is_user_reporter( $p_bug_id, $p_user_id );
+ $t_access_level = access_get_project_level( $t_project_id, $p_user_id );
# check limit_Reporter (Issue #4769)
# reporters can view just issues they reported
$t_limit_reporters = config_get( 'limit_reporters', null, $p_user_id, $t_project_id );
- $t_report_bug_threshold = config_get( 'report_bug_threshold', null, $p_user_id, $t_project_id );
- if( !is_array( $t_report_bug_threshold ) ) {
- $t_report_bug_threshold = array( $t_report_bug_threshold );
- }
- if( $t_limit_reporters && !bug_is_user_reporter( $p_bug_id, $p_user_id ) ) {
- $t_has_access = false;
- foreach( $t_report_bug_threshold as $t_threshold ) {
- if( access_has_project_level( $t_threshold + 1, $t_project_id, $p_user_id ) ) {
- $t_has_access = true;
- break;
+ if( $t_limit_reporters && !$t_bug_is_user_reporter ) {
+ # Here we only need to check that the current user has an access level
+ # higher than the lowest needed to report issues (report_bug_threshold).
+ # To improve performance, esp. when processing for several projects, we
+ # build a static array holding that threshold for each project
+ static $s_thresholds = array();
+ if( !isset( $s_thresholds[$t_project_id] ) ) {
+ $t_report_bug_threshold = config_get( 'report_bug_threshold', null, $p_user_id, $t_project_id );
+ if( !is_array( $t_report_bug_threshold ) ) {
+ $s_thresholds[$t_project_id] = $t_report_bug_threshold;
+ } else if ( empty( $t_report_bug_threshold ) ) {
+ $s_thresholds[$t_project_id] = NOBODY;
+ } else {
+ sort( $t_report_bug_threshold );
+ $s_thresholds[$t_project_id] = $t_report_bug_threshold[0];
}
}
- if( !$t_has_access ) {
+
+ if( !access_compare_level( $s_thresholds[$t_project_id], $t_access_level ) ) {
return false;
}
}
# If the bug is private and the user is not the reporter, then
# they must also have higher access than private_bug_threshold
- if( bug_get_field( $p_bug_id, 'view_state' ) == VS_PRIVATE && !bug_is_user_reporter( $p_bug_id, $p_user_id ) ) {
- $t_access_level = access_get_project_level( $t_project_id, $p_user_id );
+ if( !$t_bug_is_user_reporter && bug_get_field( $p_bug_id, 'view_state' ) == VS_PRIVATE ) {
$t_private_bug_threshold = config_get( 'private_bug_threshold', null, $p_user_id, $t_project_id );
return access_compare_level( $t_access_level, $t_private_bug_threshold )
- && access_compare_level( $t_access_level, $p_access_level );
+ && access_compare_level( $t_access_level, $p_access_level );
}
- return access_has_project_level( $p_access_level, $t_project_id, $p_user_id );
+ return access_compare_level( $p_access_level, $t_access_level );
}
/**

0 comments on commit d42e80c

Please sign in to comment.