Verified Commit 72303793 authored by godotislate's avatar godotislate
Browse files

task: #3610503 Optimize StorageCopyTrait::replaceStorageContents()

By: catch
By: smustgrave
By: godotislate
(cherry picked from commit bad194b8)
parent f2eae5f2
Loading
Loading
Loading
Loading
+8 −1
Changes for core/lib/Drupal/Core/Config/MemoryStorage.php: 8 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -56,7 +56,14 @@ public function read($name) {
   * {@inheritdoc}
   */
  public function readMultiple(array $names) {
    return array_intersect_key($this->config[$this->collection], array_flip($names));
    $items = [];
    foreach ($names as $name) {
      $value = $this->read($name);
      if (!empty($value)) {
        $items[$name] = $value;
      }
    }
    return $items;
  }

  /**
+11 −5
Changes for core/lib/Drupal/Core/Config/StorageCopyTrait.php: 11 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -34,13 +34,18 @@ protected static function replaceStorageContents(StorageInterface $source, Stora
      foreach (array_diff($target_collection->listAll(), $names) as $name) {
        $target_collection->delete($name);
      }

      // Load the config in chunks of 500 items to conserve peak memory.
      foreach (array_chunk($names, 500) as $chunked_names) {
        // Then we loop over the config which needs to be there.
      foreach ($names as $name) {
        $data = $source_collection->read($name);
        if ($data !== FALSE) {
          if ($target_collection->read($name) !== $data) {
        $source_data = $source_collection->readMultiple($chunked_names);
        $target_data = $target_collection->readMultiple($chunked_names);

        foreach ($chunked_names as $name) {
          if (isset($source_data[$name])) {
            if (!isset($target_data[$name]) || ($target_data[$name] !== $source_data[$name])) {
              // Update the target collection if the data is different.
            $target_collection->write($name, $data);
              $target_collection->write($name, $source_data[$name]);
            }
          }
          else {
@@ -51,6 +56,7 @@ protected static function replaceStorageContents(StorageInterface $source, Stora
          }
        }
      }
    }

    // Make sure that the target is set to the same collection as the source.
    $target = $target->createCollection($source->getCollectionName());