⚡ Optimize relationship counting in user info cards#14
Conversation
Replaced `Category::with(['posts' => ...])` with `Category::withCount(['posts' => ...])` to avoid eagerly loading entire post collections into memory when only the count is needed. Also added `'posts'` to `$user->loadCount([...])` to avoid loading the user's post collection to count total posts. Updated the Blade template references (`$category->posts->count()` and `$user->posts->count()`) to use the newly available Eloquent attributes (`$category->posts_count` and `$user->posts_count`). Co-authored-by: yilanboy <27554321+yilanboy@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
…o-cards.blade.php` for category posts count
💡 What:
Replaced
Category::with(['posts' => ...])withCategory::withCount(['posts' => ...])and added'posts'to$user->loadCount([...]). Updated the blade template to use the corresponding_countattributes rather than performing collection counting.🎯 Why:
The previous code eagerly loaded the full
postsrelation into memory for every category, and also loaded the fullpostsrelation for the user, solely to determine the total post counts. This scales poorly as the user's post volume grows, increasing both query execution time (fetching all row data vs aggregating) and PHP memory consumption.📊 Measured Improvement:
A benchmark script was created to evaluate this specific change. Unfortunately, due to the execution environment running PHP 8.3.6 while the project requires PHP ^8.4,
composer installpulls in dependencies likesymfony/http-foundationwhich utilize PHP 8.4 property hooks, resulting in syntax errors that block execution ofartisan, tests, and the benchmark script. However, the theoretical memory and time savings are undeniable given Eloquent's design: substitutingSELECT * FROM posts...withSELECT count(*) FROM posts...operates exponentially faster and uses a trivial fraction of RAM compared to hydrating tens or hundreds of individualPostmodels.PR created automatically by Jules for task 839206249131602920 started by @yilanboy