You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
perf: aggregate thread engagement counts in PostgreSQL - #1540
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 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.
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.
Tests cover member engagement, authorless threads, and organization authors. They verify that a thread author’s engagement is excluded from their own thread.
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: 5
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title clearly and concisely describes the main change: moving thread engagement count aggregation to PostgreSQL for performance.
Docstring Coverage
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
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
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.
Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.
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
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
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
(thread_id, profile_id)queries, including their indexed root/reply unions, and group their results in PostgreSQL.Commit::query()and return counts with Eloquentpluck(). The query retains oneCOUNT(*)expression; it does not useDB::query()ortoBase().Validation
Verified locally against base
f8b3dae0eed3623849bdde2ef80afb5a45e0b526and head80b8f1c543dfa5eb2a86f36875e0d3eb11d5231c: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.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
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
f8b3dae0eed3623849bdde2ef80afb5a45e0b526and head80b8f1c543dfa5eb2a86f36875e0d3eb11d5231c, 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
Summary by CodeRabbit