Unverified Commit d33c15d9 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3538006 by catch: Optimize EntityFieldManager::buildBundleFieldDefinitions()

parent 3dc5d0e7
Loading
Loading
Loading
Loading
Loading
+23 −9
Original line number Diff line number Diff line
@@ -67,6 +67,13 @@ class EntityFieldManager implements EntityFieldManagerInterface, PreWarmableInte
   */
  protected $activeFieldStorageDefinitions;

  /**
   * Static cache of base field overrides per entity type and bundle.
   *
   * @var array
   */
  protected $baseFieldOverrides;

  /**
   * An array of lightweight maps of fields, keyed by entity type.
   *
@@ -392,17 +399,22 @@ protected function buildBundleFieldDefinitions($entity_type_id, $bundle, array $
    // overrides of base fields.
    $bundle_field_definitions = $class::bundleFieldDefinitions($entity_type, $bundle, $base_field_definitions);

    // Load base field overrides from configuration. These take precedence over
    // base field overrides returned above.
    $base_field_override_ids = array_map(function ($field_name) use ($entity_type_id, $bundle) {
      return $entity_type_id . '.' . $bundle . '.' . $field_name;
    }, array_keys($base_field_definitions));
    $base_field_overrides = $this->entityTypeManager->getStorage('base_field_override')->loadMultiple($base_field_override_ids);
    foreach ($base_field_overrides as $base_field_override) {
    if (!isset($this->baseFieldOverrides)) {
      $this->baseFieldOverrides = [];
      $base_field_overrides = $this->entityTypeManager->getStorage('base_field_override')->loadMultiple();
      foreach ($base_field_overrides as $override) {
        $this->baseFieldOverrides[$override->getTargetEntityTypeId()][$override->getTargetBundle()][] = $override;
      }
    }

    foreach ($this->baseFieldOverrides[$entity_type_id][$bundle] ?? [] as $base_field_override) {
      /** @var \Drupal\Core\Field\Entity\BaseFieldOverride $base_field_override */
      // Base field definitions can be removed.
      if (isset($base_field_definitions[$base_field_override->getName()])) {
        $field_name = $base_field_override->getName();
        $bundle_field_definitions[$field_name] = $base_field_override;
      }
    }

    $provider = $entity_type->getProvider();
    foreach ($bundle_field_definitions as $definition) {
@@ -628,6 +640,7 @@ public function clearCachedFieldDefinitions() {
    $this->fieldMapByFieldType = [];
    $this->entityDisplayRepository->clearDisplayModeInfo();
    $this->extraFields = NULL;
    $this->baseFieldOverrides = NULL;
    Cache::invalidateTags(['entity_field_info']);
    // The typed data manager statically caches prototype objects with injected
    // definitions, clear those as well.
@@ -644,6 +657,7 @@ public function useCaches($use_caches = FALSE) {
      $this->baseFieldDefinitions = [];
      $this->fieldStorageDefinitions = [];
      $this->activeFieldStorageDefinitions = [];
      $this->baseFieldOverrides = NULL;
    }
  }

+4 −4
Original line number Diff line number Diff line
@@ -128,10 +128,10 @@ protected function doTestNodePageAdministrator(): void {
    }, 'administratorNodePage');

    $expected = [
      'QueryCount' => 435,
      'CacheGetCount' => 484,
      'QueryCount' => 418,
      'CacheGetCount' => 466,
      'CacheGetCountByBin' => [
        'config' => 174,
        'config' => 156,
        'bootstrap' => 16,
        'discovery' => 107,
        'data' => 67,
@@ -141,7 +141,7 @@ protected function doTestNodePageAdministrator(): void {
        'render' => 39,
        'menu' => 24,
      ],
      'CacheSetCount' => 467,
      'CacheSetCount' => 449,
      'CacheDeleteCount' => 0,
      'CacheTagInvalidationCount' => 0,
      'CacheTagLookupQueryCount' => 51,
+3 −3
Original line number Diff line number Diff line
@@ -52,9 +52,9 @@ protected function testFrontPageColdCache(): void {
    $this->assertSession()->pageTextContains('Umami');

    $expected = [
      'QueryCount' => 350,
      'CacheGetCount' => 451,
      'CacheSetCount' => 445,
      'QueryCount' => 336,
      'CacheGetCount' => 436,
      'CacheSetCount' => 430,
      'CacheDeleteCount' => 0,
      'CacheTagLookupQueryCount' => 49,
      'CacheTagInvalidationCount' => 0,
+4 −4
Original line number Diff line number Diff line
@@ -55,8 +55,8 @@ protected function testNodePageColdCache(): void {
    $this->assertSession()->pageTextContains('quiche');

    $expected = [
      'QueryCount' => 361,
      'CacheSetCount' => 422,
      'QueryCount' => 345,
      'CacheSetCount' => 405,
      'CacheDeleteCount' => 0,
      'CacheTagLookupQueryCount' => 45,
      'CacheTagInvalidationCount' => 0,
@@ -117,9 +117,9 @@ protected function testNodePageCoolCache(): void {
    $this->assertSession()->pageTextContains('quiche');

    $expected = [
      'QueryCount' => 111,
      'QueryCount' => 110,
      'CacheGetCount' => 214,
      'CacheSetCount' => 66,
      'CacheSetCount' => 64,
      'CacheDeleteCount' => 0,
      'CacheTagInvalidationCount' => 0,
      'CacheTagLookupQueryCount' => 25,
+1 −1
Original line number Diff line number Diff line
@@ -619,7 +619,7 @@ protected function setUpEntityWithFieldDefinition($custom_invoke_all = FALSE, $f
    $this->setUpEntityTypeDefinitions(['test_entity_type' => $this->entityType, 'base_field_override' => $override_entity_type]);

    $storage = $this->prophesize(EntityStorageInterface::class);
    $storage->loadMultiple(Argument::type('array'))->willReturn([]);
    $storage->loadMultiple()->willReturn([]);

    // By default, make the storage entity class lookup return the
    // EntityTypeManagerTestEntity class.