Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-34320

Improve performance when showing category list by not filtering category names

XMLWordPrintable

    • MOODLE_22_STABLE, MOODLE_23_STABLE

      The performance is quite bad on any page where the category list is shown (e.g. on a course/category.php?id=xx), if the following conditions are true:

      • there are a large number of course categories (1248, in my case)
      • there is a filter enabled, for instance the glossary filter, set to apply to "Content and Headings", which makes $CFG->filterall be set to 1

      With the above configuration, displaying the page course/category.php?id=xx takes > 3 seconds, and does > 1300 DB queries!

      A valid use case for filtering the course category names is for the multilanguage filter. However, I propose that there should be an option to disable that for course categories. Or alternatively do some special caching. I don't think that 1300 DB queries per page view is acceptable.

      I know that a lot of larger Moodle installations have many course categories; this is an easy performance fix.

      Making the changes shown in the code below (for Moodle 2.3.1) cut the time to create the page to 44% of what it was before, and uses 80 instead of 1311 db queries.

      diff --git a/course/lib.php b/course/lib.php
      index 374b309..2c9f1ca 100644
      --- a/course/lib.php
      +++ b/course/lib.php
      @@ -2081,7 +2081,7 @@ function make_categories_list(&$list, &$parents, $requiredcapability = '',
               }
       
               $context = get_context_instance(CONTEXT_COURSECAT, $category->id);
      -        $categoryname = format_string($category->name, true, array('context' => $context));
      +        $categoryname = format_string($category->name, true, array('context' => $context), true);
       
               // Update $path.
               if ($path) {
      diff --git a/lib/weblib.php b/lib/weblib.php
      index 84362fe..6138d6b 100644
      --- a/lib/weblib.php
      +++ b/lib/weblib.php
      @@ -1255,7 +1255,7 @@ function reset_text_filters_cache() {
        * @param array $options options array/object or courseid
        * @return string
        */
      -function format_string($string, $striplinks = true, $options = NULL) {
      +function format_string($string, $striplinks = true, $options = NULL, $skipfilters=false) {
           global $CFG, $COURSE, $PAGE;
       
           //We'll use a in-memory cache here to speed up repeated strings
      @@ -1301,7 +1301,7 @@ function format_string($string, $striplinks = true, $options = NULL) {
           // Regular expression moved to its own method for easier unit testing
           $string = replace_ampersands_not_followed_by_entity($string);
       
      -    if (!empty($CFG->filterall)) {
      +    if (!$skipfilters && !empty($CFG->filterall)) {
               $filtermanager = filter_manager::instance();
               $filtermanager->setup_page_for_filters($PAGE, $options['context']); // Setup global stuff filters may have.
               $string = $filtermanager->filter_string($string, $options['context']);

            Unassigned Unassigned
            brianking Brian King
            Votes:
            1 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved:

                Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.