Skip to content

Commit

Permalink
perf: improve performance of Chunk and Entropy operations
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 27, 2024
1 parent 68e7154 commit 3e571a0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 57 deletions.
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,6 @@ parameters:
count: 1
path: src/Operation/Duplicate.php

-
message: "#^Parameter \\#1 \\$callback of method loophp\\\\collection\\\\Contract\\\\Operation\\\\Reduceable\\<int,float\\>\\:\\:reduce\\(\\) expects callable\\(float\\|int, float, int, iterable\\<int, float\\>\\)\\: \\(float\\|int\\), Closure\\(float, float, int, loophp\\\\collection\\\\Collection\\)\\: float given\\.$#"
count: 1
path: src/Operation/Entropy.php

-
message: "#^Parameter \\#1 of closure expects callable\\(mixed, mixed, iterable\\)\\: mixed, Closure\\(mixed, int, loophp\\\\collection\\\\Collection\\)\\: float given\\.$#"
count: 1
path: src/Operation/Entropy.php

-
message: "#^Template type V of method loophp\\\\collection\\\\Operation\\\\Find\\:\\:__invoke\\(\\) is not referenced in a parameter\\.$#"
count: 1
Expand Down
18 changes: 0 additions & 18 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,6 @@
<code><![CDATA[$callbacks]]></code>
</InvalidArgument>
</file>
<file src="src/Operation/Entropy.php">
<InvalidArgument>
<code><![CDATA[static fn (float $acc, float $p, int $_, Collection $c): float => 0 === $key ? $acc : $acc - $p * log($p, 2) / ((1 === $count = $c->count()) ? 1 : log($count, 2))]]></code>
<code><![CDATA[static fn (mixed $_, int $key, Collection $collection): float => $collection
->limit($key + 1)
->frequency()
->map(
/**
* @param T $_
*/
static fn (mixed $_, int $freq): float => $freq / ($key + 1)
)
->reduce(
static fn (float $acc, float $p, int $_, Collection $c): float => 0 === $key ? $acc : $acc - $p * log($p, 2) / ((1 === $count = $c->count()) ? 1 : log($count, 2)),
0
)]]></code>
</InvalidArgument>
</file>
<file src="src/Operation/Every.php">
<InvalidArgument>
<code><![CDATA[$predicates]]></code>
Expand Down
10 changes: 4 additions & 6 deletions src/Operation/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Closure;
use Generator;
use Iterator;

use function count;

Expand Down Expand Up @@ -34,13 +33,12 @@ public function __invoke(): Closure
* @return Generator<int, list<T>>
*/
static function (iterable $iterable) use ($sizes): Generator {
/** @var Iterator<int, int> $sizesIterator */
$sizesIterator = (new Cycle())()($sizes);

$sizesCount = count($sizes);
$i = 0;
$values = [];

foreach ($iterable as $value) {
$size = $sizesIterator->current();
$size = $sizes[$i % $sizesCount];

if (0 >= $size) {
return;
Expand All @@ -52,7 +50,7 @@ static function (iterable $iterable) use ($sizes): Generator {
continue;
}

$sizesIterator->next();
++$i;

yield $values;

Expand Down
43 changes: 20 additions & 23 deletions src/Operation/Entropy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Closure;
use Generator;
use loophp\collection\Collection;
use loophp\collection\Contract\Collection as CollectionInterface;

/**
* @immutable
Expand All @@ -24,32 +23,30 @@ public function __invoke(): Closure
{
/** @var Closure(iterable<TKey, T>): Generator<int, int<0,1>|float> $pipe */
$pipe = (new Pipe())()(
(
/**
* @param iterable<TKey, T> $iterable
*
* @return CollectionInterface<int, T>
*/
static fn (iterable $iterable): CollectionInterface => Collection::fromIterable($iterable)->normalize()->squash()
),
(new Map())()(
/**
* @param T $_
* @param Collection<TKey, T> $collection
* @param iterable<TKey, T> $iterable
*/
static fn (mixed $_, int $key, Collection $collection): float => $collection
->limit($key + 1)
->frequency()
->map(
/**
* @param T $_
*/
static fn (mixed $_, int $freq): float => $freq / ($key + 1)
)
->reduce(
static fn (float $acc, float $p, int $_, Collection $c): float => 0 === $key ? $acc : $acc - $p * log($p, 2) / ((1 === $count = $c->count()) ? 1 : log($count, 2)),
0
)
static function (mixed $_, int $key, iterable $iterable): float {
$collection = Collection::fromIterable($iterable);

return $collection
->normalize()
->squash()
->limit($key + 1)
->frequency()
->map(
/**
* @param T $_
*/
static fn (mixed $_, int $freq): float => $freq / ($key + 1)
)
->reduce(
static fn (float $acc, float $p, int $k): float => 0 === $key ? $acc : $acc - $p * log($p, 2) / ((1 === $k) ? 1 : log($k, 2)),
0
);
}
),
(new Map())()(
/**
Expand Down

0 comments on commit 3e571a0

Please sign in to comment.