Skip to content

Commit 637a3ca

Browse files
committed
perf: optimize filter count()
Using the iterator_to_array entirely materializes the filtered result, consuming unnecessary extra memory. Using a straight-forward foreach loop will avoid the use of an iterator, avoiding duplicate storage and a slightly faster execution. Signed-off-by: Sacha Telgenhof <me@sachatelgenhof.com>
1 parent 7fda9e6 commit 637a3ca

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

src/Yasumi/Filters/AbstractFilter.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@ abstract class AbstractFilter extends \FilterIterator implements \Countable
3030
*/
3131
public function count(): int
3232
{
33-
$names = array_map(
34-
static fn ($holiday) => $holiday instanceof SubstituteHoliday ?
35-
$holiday->getSubstitutedHoliday()->getKey() : $holiday->getKey(),
36-
iterator_to_array($this)
37-
);
33+
$names = [];
3834

39-
return count(array_unique($names));
35+
foreach ($this as $holiday) {
36+
$key = $holiday instanceof SubstituteHoliday
37+
? $holiday->getSubstitutedHoliday()->getKey()
38+
: $holiday->getKey();
39+
40+
$names[$key] = true;
41+
}
42+
43+
return count($names);
4044
}
4145
}

0 commit comments

Comments
 (0)