Skip to content

Commit

Permalink
Improve performance for Rows: dropRight, filter, partitionBy
Browse files Browse the repository at this point in the history
…& `sortBy`
  • Loading branch information
stloyd committed Oct 30, 2023
1 parent 0ed3860 commit 726f150
Showing 1 changed file with 6 additions and 22 deletions.
28 changes: 6 additions & 22 deletions src/core/etl/src/Flow/ETL/Rows.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,7 @@ public function drop(int $size) : self

public function dropRight(int $size) : self
{
$rows = $this->rows;

for ($i = 0; $i < $size; $i++) {
\array_pop($rows);
}

return new self(...$rows);
return new self(...\array_slice($this->rows, 0, -$size));
}

/**
Expand Down Expand Up @@ -177,15 +171,7 @@ public function entries() : array
*/
public function filter(callable $callable) : self
{
$results = [];

foreach ($this->rows as $row) {
if ($callable($row)) {
$results[] = $row;
}
}

return new self(...$results);
return new self(...\array_filter($this->rows, $callable));
}

public function find(callable $callable) : self
Expand Down Expand Up @@ -556,7 +542,7 @@ public function partitionBy(string|Reference $entry, string|Reference ...$entrie
* @var array<string, mixed> $partitionsData
*/
foreach ($cartesianProduct($partitions) as $partitionsData) {
$rows = $this->filter(function (Row $row) use ($partitionsData) : bool {
$rows = \array_filter($this->rows, function (Row $row) use ($partitionsData) : bool {
/**
* @var mixed $value
*/
Expand All @@ -569,8 +555,8 @@ public function partitionBy(string|Reference $entry, string|Reference ...$entrie
return true;
});

if ($rows->count()) {
$partitionedRows[] = new PartitionedRows($rows, ...Partition::fromArray($partitionsData));
if ($rows) {
$partitionedRows[] = new PartitionedRows(new self(...$rows), ...Partition::fromArray($partitionsData));
}
}

Expand Down Expand Up @@ -672,11 +658,9 @@ public function sortAscending(string|EntryReference $ref) : self
*/
public function sortBy(EntryReference ...$refs) : self
{
$sortBy = References::init(...$refs)->reverse();

$rows = $this;

foreach ($sortBy->all() as $ref) {
foreach (\array_reverse($refs) as $ref) {
$rows = $ref->sort() === SortOrder::ASC ? $rows->sortAscending($ref) : $rows->sortDescending($ref);
}

Expand Down

0 comments on commit 726f150

Please sign in to comment.