Skip to content

📦 Use WeakMap instead of SplObjectStorage to avoid memory leak #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Knp/Menu/Matcher/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
class Matcher implements MatcherInterface
{
/**
* @var \SplObjectStorage<ItemInterface, bool>
* @var \WeakMap<ItemInterface, bool>
*/
private \SplObjectStorage $cache;
private \WeakMap $cache;

/**
* @var iterable|VoterInterface[]
Expand All @@ -26,7 +26,7 @@ class Matcher implements MatcherInterface
public function __construct($voters = [])
{
$this->voters = $voters;
$this->cache = new \SplObjectStorage();
$this->cache = new \WeakMap();
}

public function isCurrent(ItemInterface $item): bool
Expand All @@ -36,7 +36,7 @@ public function isCurrent(ItemInterface $item): bool
return $current;
}

if ($this->cache->contains($item)) {
if ($this->cache->offsetExists($item)) {
return $this->cache[$item];
}

Expand Down Expand Up @@ -71,6 +71,6 @@ public function isAncestor(ItemInterface $item, ?int $depth = null): bool

public function clear(): void
{
$this->cache = new \SplObjectStorage();
$this->cache = new \WeakMap();
}
}