Repository navigation
Faster user permission lookups - #20149
Merged
Merged
Conversation
doesUserHavePermission() scanned the user's whole permission list for every check. Keep a map alongside the cached list so lookups are constant-time. Co-authored-by: Amp <amp@ampcode.com> Amp-Thread-ID: https://ampcode.com/threads/T-ae777030-d792-49ae-8853-87e83195c88f
brandonkelly
force-pushed
the
perf/user-permission-lookups
branch
from
October 28, 2026 15:12
a0c4a60 to
90a86b5
Compare
Keeps doesUserHavePermission() working for subclasses that override getPermissionsByUserId(). Amp-Thread-ID: https://ampcode.com/threads/T-ae777030-d792-49ae-8853-87e83195c88f Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-ae777030-d792-49ae-8853-87e83195c88f Co-authored-by: Amp <amp@ampcode.com>
brandonkelly
added a commit
that referenced
this pull request
Oct 29, 2026
Ports the 5.12 changes to 6.x: - User permission lookups use a hash map built from getPermissionsByUserId(), cleared whenever user or group permissions are saved (#20149) - Applying volume config saved before the Propagation Method setting existed keeps existing volumes' propagation method, rather than switching them to "all" and queuing a resave, and new volumes follow the migration's site-count rule 6.x's Str::convertToUtf8() already falls back to Windows-1252, and the yii2-phpstan dev dependency only applies to 5.x's PHPStan setup, so StringHelper and composer.lock keep 6.x's versions. Co-authored-by: Amp <amp@ampcode.com> Amp-Thread-ID: https://ampcode.com/threads/T-5834f157-5515-4cdf-8d3d-71e4c0bfc685
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Users' cached permission lists now have a map alongside them for lookups. The map is rebuilt whenever the list is cached (
getPermissionsByUserId(),saveUserPermissions()) and cleared byreset().Evidence
Per-lookup cost (
doesUserHavePermission(), mean of 20,000 lookups, half hits):Semi-complex site (PostgreSQL, production mode, GraphQL caching off; median of 11–15 fresh processes):
canView/Save/Delete/Duplicate/CreateDrafts× 100 entries)entries(limit: 20)At this scale, permission lookups aren't the bottleneck; the differences are within noise. The gains appear with thousands of permissions. In a mocked test with 1,000 sections and 200 volumes (8,600 permissions), the authenticated schema build went from 118 ms to 20 ms.
Regression test:
UserPermissionsTest::testDoesUserHavePermissionnow also checks that lookups are case-insensitive and thatreset()drops cached permissions. It fails ifreset()doesn't clear the new map.Related issues
{ ping }request (~390 ms) goes to building the schema definition (Schema::getTypeMap()→FieldArgument::createMap), not to permission checks. WithlazyGqlTypesenabled it drops to ~140 ms.