Improve installer performance by ~20% by rebuilding the router after the entire installation is complete rather than after each module

Created on 7 October 2019, almost 5 years ago
Updated 24 January 2023, over 1 year ago

Drupal rebuilds the router on each module installation. This is useful when adding modules once Drupal has been installed.

However, it is causing a lot of CPU cycles when Drupal is installing and the routes may be rebuilt over and over. Here's a screenshot from a Blackfire profile shaal created while we were debugging the Umami demo's installation time. The router is rebuilt 55 times before Drupal has finished being installed.

Here is the current code:

      if (!\Drupal::service('router.route_provider.lazy_builder')->hasRebuilt()) {
        // Rebuild routes after installing module. This is done here on top of
        // \Drupal\Core\Routing\RouteBuilder::destruct to not run into errors on
        // fastCGI which executes ::destruct() after the module installation
        // page was sent already.
        \Drupal::service('router.builder')->rebuild();
      }

Adding a check to drupal_installation_attempted () greatly improves the installation time. Here is the comparison when we check if Drupal is installed first

      if (!drupal_installation_attempted() && !\Drupal::service('router.route_provider.lazy_builder')->hasRebuilt()) {
        // Rebuild routes after installing module. This is done here on top of
        // \Drupal\Core\Routing\RouteBuilder::destruct to not run into errors on
        // fastCGI which executes ::destruct() after the module installation
        // page was sent already.
        \Drupal::service('router.builder')->rebuild();
      }
πŸ“Œ Task
Status

Fixed

Version

9.1

Component
InstallΒ  β†’

Last updated 2 days ago

No maintainer
Created by

πŸ‡ΊπŸ‡ΈUnited States mglaman WI, USA

Live updates comments and jobs are added and updated live.
  • Performance

    It affects performance. It is often combined with the Needs profiling tag.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024