Skip to content
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

Increase performance by using CacheableVoterInterface #227

Closed
kevinpapst opened this issue Apr 17, 2024 · 2 comments · Fixed by #228
Closed

Increase performance by using CacheableVoterInterface #227

kevinpapst opened this issue Apr 17, 2024 · 2 comments · Fixed by #228

Comments

@kevinpapst
Copy link
Contributor

kevinpapst commented Apr 17, 2024

Description

Symfony 5.4 introduced cacheable Voters, which speed up apps, which use a lot of is_granted calls.
You can define which attributes the Voter supports, and thus it will not be called for other attributes.

In one of my apps, I opened the profiler for a listing page, which triggers around 1000 is_granted calls.
Each of them calls Scheb\TwoFactorBundle\Security\Authorization\Voter\TwoFactorInProgressVoter in addition to the specific app Voter, see this example screenshot:
Bildschirmfoto 2024-04-17 um 21 35 00

By changing the voter to this

class TwoFactorInProgressVoter implements CacheableVoterInterface
{

    public function supportsAttribute(string $attribute): bool
    {
        return $attribute === self::IS_AUTHENTICATED_2FA_IN_PROGRESS || $attribute === AuthenticatedVoter::PUBLIC_ACCESS;
    }

    public function supportsType(string $subjectType): bool
    {
        return $subjectType === 'null';
    }

    ... 
}

I was able to remove the Voter from almost all of the 1k is_granted decisions:

Bildschirmfoto 2024-04-17 um 21 45 31

Which reduced the time from this:

Bildschirmfoto 2024-04-17 um 21 47 11

to this:

Bildschirmfoto 2024-04-17 um 21 47 35

A pretty low hanging fruit, which I'd like to address in a PR if that's okay!?
Or is there anything I am missing?

@scheb
Copy link
Owner

scheb commented Apr 18, 2024

Sounds reasonable, give it a shot 👍

@kevinpapst
Copy link
Contributor Author

Thanks @scheb - added PR #228

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants