Skip to content

Memoize backpack_users_have_email() so N avatars no longer run N schema queries - #6089

Merged
pxpm merged 3 commits into
mainfrom
memoize-users-have-email
Nov 1, 2026
Merged

pxpm merged 3 commits into
mainfrom
memoize-users-have-email

Conversation

@tabacitu

Copy link
Copy Markdown
Member

Symptom

backpack_users_have_email() runs Schema::hasColumn() on the users table every time it is called. backpack_avatar_url() calls it for every avatar, so any page that renders N user avatars (a users list with avatar columns, a chip column, a search palette) runs N identical information_schema.columns queries.

Fix

Memoize the result in a static variable, keyed by user table + email column, so a swapped user model or a custom backpack.base.email_column still resolves correctly. The schema is checked once per process instead of once per call.

Behaviour is otherwise unchanged: same return value, same config keys, same user model lookup.

Also fixed the docblock (@return bool, not string).

Tests

Added tests/Feature/HelpersTest.php:

  • repeated calls after the first run zero queries (fails on main, passes with this change)
  • changing backpack.base.email_column gets its own cache entry

Full suite passes locally on PHP 8.3.

🤖 Generated with Claude Code

…ma queries

backpack_avatar_url() calls backpack_users_have_email() for every avatar it
renders, and that helper ran Schema::hasColumn() on the users table each time.
A list of N users therefore ran N identical information_schema queries.

Cache the result in a static array keyed by table + email column, so a swapped
user model or custom email_column still resolves correctly. Behaviour is
otherwise unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@tabacitu

Copy link
Copy Markdown
Member Author

@pxpm discovered this while working on smth else - take it over please.

@tabacitu tabacitu moved this to Todo in Triage Oct 19, 2026
@pxpm
pxpm merged commit d0683cf into main Nov 1, 2026
17 checks passed
@pxpm
pxpm deleted the memoize-users-have-email branch November 1, 2026 17:35
@github-project-automation github-project-automation Bot moved this from Todo to Done in Triage Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants