Commit 8df6544e authored by catch's avatar catch
Browse files

Issue #3266568 by mxr576, sourabhjain, quietone: Refactor array_merge() usage...

Issue #3266568 by mxr576, sourabhjain, quietone: Refactor array_merge() usage in loops as possible for performance
parent 74f7e5ac
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -72,7 +72,7 @@ public function getSortedProviders() {
      krsort($this->providerOrders);

      // Merge nested providers from $this->providers into $this->sortedProviders.
      $this->sortedProviders = array_merge([], ...$this->providerOrders);
      $this->sortedProviders = array_merge(...$this->providerOrders);
    }

    return $this->sortedProviders;
+1 −1
Original line number Diff line number Diff line
@@ -107,7 +107,7 @@ protected function getSortedBuilders() {
      // Sort the builders according to priority.
      krsort($this->builders);
      // Merge nested builders from $this->builders into $this->sortedBuilders.
      $this->sortedBuilders = array_merge([], ...$this->builders);
      $this->sortedBuilders = array_merge(...$this->builders);
    }
    return $this->sortedBuilders;
  }
+7 −6
Original line number Diff line number Diff line
@@ -264,7 +264,7 @@ public function findConfigEntityDependencies($type, array $names, ConfigDependen
    foreach ($names as $name) {
      $dependencies[] = $dependency_manager->getDependentEntities($type, $name);
    }
    return array_merge([], ...$dependencies);
    return array_merge(...$dependencies);
  }

  /**
@@ -292,9 +292,9 @@ public function findConfigEntityDependenciesAsEntities($type, array $names, Conf
      $storage = $this->entityTypeManager->getStorage($entity_type_id);
      // Remove the keys since there are potential ID clashes from different
      // configuration entity types.
      $entities_to_return = array_merge($entities_to_return, array_values($storage->loadMultiple($entities_to_load)));
      $entities_to_return[] = array_values($storage->loadMultiple($entities_to_load));
    }
    return $entities_to_return;
    return array_merge(...$entities_to_return);
  }

  /**
@@ -487,13 +487,14 @@ public function findMissingContentDependencies() {
    $missing_dependencies = [];
    foreach ($this->activeStorage->readMultiple($this->activeStorage->listAll()) as $config_data) {
      if (isset($config_data['dependencies']['content'])) {
        $content_dependencies = array_merge($content_dependencies, $config_data['dependencies']['content']);
        $content_dependencies[] = $config_data['dependencies']['content'];
      }
      if (isset($config_data['dependencies']['enforced']['content'])) {
        $content_dependencies = array_merge($content_dependencies, $config_data['dependencies']['enforced']['content']);
        $content_dependencies[] = $config_data['dependencies']['enforced']['content'];
      }
    }
    foreach (array_unique($content_dependencies) as $content_dependency) {
    $unique_content_dependencies = array_unique(array_merge(...$content_dependencies));
    foreach ($unique_content_dependencies as $content_dependency) {
      // Format of the dependency is entity_type:bundle:uuid.
      [$entity_type, $bundle, $uuid] = explode(':', $content_dependency, 3);
      if (!$this->entityRepository->loadEntityByUuid($entity_type, $uuid)) {
+21 −15
Original line number Diff line number Diff line
@@ -129,7 +129,8 @@ protected function loadRecords() {
    $prefix_length = strlen($prefix);

    // Search the conditions for restrictions on configuration object names.
    $names = FALSE;
    $filter_by_names = [];
    $has_added_restrictions = FALSE;
    $id_condition = NULL;
    $id_key = $this->entityType->getKey('id');
    if ($this->condition->getConjunction() == 'AND') {
@@ -140,21 +141,22 @@ protected function loadRecords() {
        if (is_string($condition['field']) && ($operator == 'IN' || $operator == '=')) {
          // Special case ID lookups.
          if ($condition['field'] == $id_key) {
            $has_added_restrictions = TRUE;
            $ids = (array) $condition['value'];
            $names = array_map(function ($id) use ($prefix) {
            $filter_by_names[] = array_map(static function ($id) use ($prefix) {
              return $prefix . $id;
            }, $ids);
          }
          elseif (in_array($condition['field'], $lookup_keys)) {
            $has_added_restrictions = TRUE;
            // If we don't find anything then there are no matches. No point in
            // listing anything.
            $names = [];
            $keys = (array) $condition['value'];
            $keys = array_map(function ($value) use ($condition) {
            $keys = array_map(static function ($value) use ($condition) {
              return $condition['field'] . ':' . $value;
            }, $keys);
            foreach ($this->getConfigKeyStore()->getMultiple($keys) as $list) {
              $names = array_merge($names, $list);
              $filter_by_names[] = $list;
            }
          }
        }
@@ -166,7 +168,7 @@ protected function loadRecords() {
        // We stop at the first restricting condition on name. In the case where
        // there are additional restricting conditions, results will be
        // eliminated when the conditions are checked on the loaded records.
        if ($names !== FALSE) {
        if ($has_added_restrictions !== FALSE) {
          // If the condition has been responsible for narrowing the list of
          // configuration to check there is no point in checking it further.
          unset($conditions[$condition_key]);
@@ -174,52 +176,56 @@ protected function loadRecords() {
        }
      }
    }

    // If no restrictions on IDs were found, we need to parse all records.
    if ($names === FALSE) {
      $names = $this->configFactory->listAll($prefix);
    if ($has_added_restrictions === FALSE) {
      $filter_by_names = $this->configFactory->listAll($prefix);
    }
    else {
      $filter_by_names = array_merge(...$filter_by_names);
    }
    // In case we have an ID condition, try to narrow down the list of config
    // objects to load.
    if ($id_condition && !empty($names)) {
    if ($id_condition && !empty($filter_by_names)) {
      $value = $id_condition['value'];
      $filter = NULL;
      switch ($id_condition['operator']) {
        case '<>':
          $filter = function ($name) use ($value, $prefix_length) {
          $filter = static function ($name) use ($value, $prefix_length) {
            $id = substr($name, $prefix_length);
            return $id !== $value;
          };
          break;

        case 'STARTS_WITH':
          $filter = function ($name) use ($value, $prefix_length) {
          $filter = static function ($name) use ($value, $prefix_length) {
            $id = substr($name, $prefix_length);
            return strpos($id, $value) === 0;
          };
          break;

        case 'CONTAINS':
          $filter = function ($name) use ($value, $prefix_length) {
          $filter = static function ($name) use ($value, $prefix_length) {
            $id = substr($name, $prefix_length);
            return strpos($id, $value) !== FALSE;
          };
          break;

        case 'ENDS_WITH':
          $filter = function ($name) use ($value, $prefix_length) {
          $filter = static function ($name) use ($value, $prefix_length) {
            $id = substr($name, $prefix_length);
            return strrpos($id, $value) === strlen($id) - strlen($value);
          };
          break;
      }
      if ($filter) {
        $names = array_filter($names, $filter);
        $filter_by_names = array_filter($filter_by_names, $filter);
      }
    }

    // Load the corresponding records.
    $records = [];
    foreach ($this->configFactory->loadMultiple($names) as $config) {
    foreach ($this->configFactory->loadMultiple($filter_by_names) as $config) {
      $records[substr($config->getName(), $prefix_length)] = $config->get();
    }
    return $records;
+2 −2
Original line number Diff line number Diff line
@@ -88,9 +88,9 @@ public static function flattenConfigObjects(array $config_objects) {
        }
        return $config_name;
      }, $config_names);
      $flat_config_objects = array_merge($flat_config_objects, $config_names);
      $flat_config_objects[] = $config_names;
    }
    return $flat_config_objects;
    return array_merge(...$flat_config_objects);
  }

}
Loading