Skip to content

perf: aggregate thread engagement counts in PostgreSQL - #1540

Open
hayaIshida wants to merge 1 commit into
dev-null-sh:productionfrom
hayaIshida:perf/sql-thread-engagement-counts
Open

hayaIshida wants to merge 1 commit into
dev-null-sh:productionfrom
hayaIshida:perf/sql-thread-engagement-counts

Conversation

@hayaIshida

@hayaIshida hayaIshida commented Nov 11, 2026 •

Copy link
Copy Markdown

Summary

Thread rescoring currently transfers every distinct participant ID to PHP, builds per-thread maps, removes the author's entries, and counts them. Busy threads pay unnecessary transfer, allocation, and PHP processing costs while the thread roots are locked. Aggregate those same participant sets in PostgreSQL and fetch the counts instead, preserving the existing scoring behavior.

In a local synthetic benchmark with 50,000 distinct stargazers, the stargazer counting step fell from 75.3 ms to 37.4 ms, and its additional PHP peak memory fell from 33.8 MB to 27 KB.

Changes

  • Replace participant maps with one aggregate count per engaged thread and category. Keep the existing distinct (thread_id, profile_id) queries, including their indexed root/reply unions, and group their results in PostgreSQL.
  • Build the aggregate through Commit::query() and return counts with Eloquent pluck(). The query retains one COUNT(*) expression; it does not use DB::query() or toBase().
  • Preserve the author exclusions, anonymous-participant handling, moderation/deletion filters, organization semantics, score formula, root lock order, transaction timing, and chunked updates. Authorless threads still count their non-anonymous participants.
  • Add regression coverage for authorless threads, organization authors, and an author whose contributions should be excluded from their own thread but counted in another thread.
  • Narrow the star-count query-log test to the direct star-count queries it checks, so the new engagement aggregates are not mistaken for extra star-count queries. Its exact expected SQL remains unchanged.

Validation

Verified locally against base f8b3dae0eed3623849bdde2ef80afb5a45e0b526 and head 80b8f1c543dfa5eb2a86f36875e0d3eb11d5231c:

  • composer test:lint: passed; 53 existing warnings in unchanged frontend files, zero errors.
  • composer test:types: passed.
  • composer test:type-coverage: passed, 100.0% type coverage.
  • bun run test:unit: 314 passed, zero failures.
  • Full PHP suite: 5,953 passed, 31,880 assertions, 100.0% line coverage; no test filters or impacted-test skipping.
php -d memory_limit=-1 vendor/bin/pest --parallel --processes=4 --ci \
  --coverage --exactly=100.0 --passthru-php="'-d' 'memory_limit=-1'"

PHP tests used isolated PostgreSQL test databases with pgvector 0.8.7. The local runtime used PHP 8.5, PostgreSQL 17, Node 26, and Bun 1.4.3. Coverage thresholds and exclusions were not changed.

Benchmark details

Distinct stargazers Stored star rows Before After Before extra PHP peak After extra PHP peak
1,000 2,000 1.595 ms 1.497 ms 667,472 bytes 27,088 bytes
10,000 20,000 12.105 ms 7.258 ms 7,018,832 bytes 27,088 bytes
50,000 100,000 75.309 ms 37.427 ms 33,794,408 bytes 27,088 bytes

The benchmark invokes the original and updated counting helpers against the same stargazer query and synthetic data in session-local PostgreSQL tables with the application's indexes. Each participant stars both the root and a reply, exercising duplicate elimination; the root author is included and must be excluded from the result. Both versions produced identical counts in every case.

Timings are medians of five warm executions after one warmup, including SQL execution, result transfer, and PHP counting. PHP memory is the additional peak above usage immediately before the counting call. Local database tables and benchmark artifacts are excluded from the implementation diff.

Independent review

PASS from a read-only independent senior-reviewer agent for base f8b3dae0eed3623849bdde2ef80afb5a45e0b526 and head 80b8f1c543dfa5eb2a86f36875e0d3eb11d5231c, after receiving the final verification results above. No actionable correctness, regression, architecture, security, or test-quality findings remain. Visual and device review are inapplicable to this backend-only diff.

Limitations

  • The benchmark measures the stargazer counting step, not a complete request, the other engagement categories, or total rescoring latency. PHP memory measurements exclude database working memory.
  • PostgreSQL still recounts the engagement sets. This reduces transferred rows and PHP allocation/traversal; it does not eliminate the cumulative cost of repeated recounts as engagement grows.
  • No UI or mobile interactions change, so visual and device verification are inapplicable. Hosted CI results are separate from local verification.

Summary by CodeRabbit

  • Bug Fixes
    • Engagement-based commit scores now exclude a thread author’s own engagement, including when the author is an organization. Engagements without an associated profile are also excluded, improving the accuracy of thread activity counts.
    • Commit star-count checks now distinguish star queries from unrelated count queries.

@coderabbitai

coderabbitai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

UpdateCommitHotScores now uses database-side per-thread engagement counts that exclude anonymous profiles and thread authors. Tests cover author exclusion, authorless threads, organization authors, and the expected commit-specific star count query.

Changes: Thread engagement scoring

Layer / File(s) Summary
Database counts and score inputs
app/Actions/UpdateCommitHotScores.php
The action counts distinct profiles per thread in the database, excludes anonymous profiles and each thread’s author, then passes the counts to ThreadEngagement.
Author exclusion regression coverage
tests/Unit/Actions/UpdateCommitHotScoresTest.php
Tests cover member engagement, authorless threads, and organization authors. They verify that a thread author’s engagement is excluded from their own thread.

Commit star query test

Layer / File(s) Summary
Count query assertion
tests/Feature/Controllers/CommitStarControllerTest.php
The query-log filter now matches aggregate-count queries against the stars table.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Possibly related issues

  • dev-null-sh/dev-null.sh#938 — This change adds regression coverage for organization authors and excludes their engagement from their own thread.

Possibly related PRs

Suggested reviewers: mnapoli, nunomaduro

Merge Risk: Minimal · up to 80b8f

This change moves thread engagement counting into PostgreSQL and adds tests for author exclusion, authorless threads and organization authors. No actionable merge-blocking risk remains from the supplied review evidence.

Pre-merge checks: Passed 5

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: moving thread engagement count aggregation to PostgreSQL for performance.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 3 files.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5e96dbf0-e621-4e32-b476-de1915ac1e45
Commits

Reviewing files that changed from the base of the PR and between f8b3dae and 80b8f1c.

Files selected for processing: 3

  • app/Actions/UpdateCommitHotScores.php
  • tests/Feature/Controllers/CommitStarControllerTest.php
  • tests/Unit/Actions/UpdateCommitHotScoresTest.php

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

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.

1 participant