Skip to content

Commit

Permalink
Improve performance of array comparison & sorting (#612)
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd committed Oct 18, 2023
1 parent 80258e5 commit cdd434a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/core/etl/src/Flow/ArrayComparison/ArrayComparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ private function valueEquals($a, $b) : bool
return false;
}

foreach (\array_keys($b) as $key) {
if (!\array_key_exists($key, $a) || !\array_key_exists($key, $b)) {
/** @psalm-suppress MixedAssignment */
foreach ($b as $key => $value) {
if (!\array_key_exists($key, $a)) {
return false;
}

if (!$this->valueEquals($a[$key], $b[$key])) {
if (!$this->valueEquals($a[$key], $value)) {
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/etl/src/Flow/ArrayComparison/ArraySortByKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public function __invoke(array $array) : array
);

if (\array_is_list($array)) {
\usort($array, fn ($a, $b) : int => $a <=> $b);
\sort($array);
} else {
\uksort($array, fn ($a, $b) : int => $a <=> $b);
\ksort($array);
}

return $array;
Expand Down
4 changes: 2 additions & 2 deletions src/core/etl/src/Flow/ETL/Row/Entry/ArrayEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function is(string|Reference $name) : bool

public function isEqual(Entry $entry) : bool
{
return $this->is($entry->name()) && $entry instanceof self && (new ArrayComparison())->equals($this->value(), $entry->value());
return $this->is($entry->name()) && $entry instanceof self && (new ArrayComparison())->equals($this->value, $entry->value());
}

public function map(callable $mapper) : Entry
Expand All @@ -83,7 +83,7 @@ public function rename(string $name) : Entry

public function toString() : string
{
return \json_encode($this->value(), JSON_THROW_ON_ERROR);
return \json_encode($this->value, \JSON_THROW_ON_ERROR);
}

public function value() : array
Expand Down

0 comments on commit cdd434a

Please sign in to comment.