Skip to content

Security: memoize Permission decisions - #1

Open
ohmyfelix wants to merge 1 commit into
masterfrom
perf/r3-permission-memo
Open

ohmyfelix wants to merge 1 commit into
masterfrom
perf/r3-permission-memo

Conversation

@ohmyfelix

Copy link
Copy Markdown
Collaborator

What

Permission::decide() caches results per role/resource/privilege, after checkRole()/checkResource() so exceptions are unchanged. Cleared by all 7 mutators; disabled for the instance once any assertion exists or a role/resource name contains NUL; bypassed for privilege ''; capped at 4096 entries.

Why

Menus and templates ask the same isAllowed() questions many times per request; each walks role and resource inheritance.

Measured

10 roles, 30 resources, 200 rules, user with 2 roles, 100 checks (60 % repeats):

  • warm: User::isAllowed 128 -> 52 us, Permission::isAllowed 72 -> 20 us
  • cold mixed: 244 -> 170 us (user), 132 -> 69 us (perm)
  • cold with zero repeated checks: 268 -> 289 us (+8 %), about +0.14 us per miss; break-even at ~18 % repeated checks.
    Equivalence: 10 000 seeds with mutations, assertions, Resource objects, Principal and voters; identical results.

Details: https://github.com/subnette/rfc-performance/blob/perf/round-2/docs/perf/round-2.md (round 3 numbers measured on current upstream master).

Tests

  • vendor/bin/tester tests -s -C on master: OK (78 tests)
  • same on this branch: OK (78 tests)
  • phpstan: no errors on both

Trade-off to decide: slower when no check repeats.

decide() caches results per role/resource/privilege after the existence checks. The memo is cleared by every mutator, disabled once any assertion is registered or a name contains a NUL byte, bypassed for an empty privilege and capped at 4096 entries.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants