Static caching of CodeComponentDataProvider per request

Created on 8 September 2025, 8 days ago

Overview

I noticed this while using the AI capabilities. CodeComponentDataProvider::getXbDataBreadcrumbsV0 took 553ms becaue it was invoked 132 times. The breadcrumb builder can be expensive. The preview rendering of whatever was happening behind the scenes ran this a lot

Proposed resolution

Use \SplObjectStorage to cache data per-request.

  public function __construct(
    private readonly ConfigFactoryInterface $configFactory,
    private readonly RequestStack $requestStack,
    private readonly RouteMatchInterface $routeMatch,
    private readonly TitleResolverInterface $titleResolver,
    private readonly ChainBreadcrumbBuilderInterface $breadcrumbManager,
    private readonly ContainerInterface $container,
  ) {
    $this->data = new \SplObjectStorage();
  }

  /**
   * Returns the BaseUrl for V0 of drupalSettings.xbData.
   *
   * @return array[]
   */
  public function getXbDataBaseUrlV0(): array {
    $request = $this->requestStack->getCurrentRequest();
    \assert($request instanceof Request);

    if (!isset($this->data[$request])) {
      $this->data[$request] = [
        self::V0 => [
          // ⚠️ Not the same as `drupalSettings.path.baseUrl` nor Symfony's
          // definition of a base URL.
          // JavaScript tools like @drupal-api-client/json-api-client usually need
          // a full absolute URL.
          // @see \Symfony\Component\HttpFoundation\Request::getBaseUrl()
          // @see \Drupal\system\Hook\SystemHooks::jsSettingsAlter()
          'baseUrl' => $request->getSchemeAndHttpHost() . $request->getBaseUrl(),
        ],
      ];
    }
    return $this->data[$request];
  }

User interface changes

Feature request
Status

Active

Version

1.0

Component

… to be triaged

Created by

🇺🇸United States mglaman WI, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024