Skip to content

perf(templates): avoid loading group members and batch list relations - #944

Merged
T3ST3ST3R0N merged 2 commits into
PasarGuard:devfrom
dr-hoseyn:codex/perf-user-template-loading
Oct 10, 2026
Merged

T3ST3ST3R0N merged 2 commits into
PasarGuard:devfrom
dr-hoseyn:codex/perf-user-template-loading

Conversation

@dr-hoseyn

@dr-hoseyn dr-hoseyn commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Creating or updating a user template currently loads every user and inbound in its selected groups, although only the group association is needed. Listing templates also loads groups once per template. Disable those unnecessary group relationship loads on create/update and use selectinload(UserTemplate.groups) for lists, preserving responses, filters, and pagination.

Measured improvement

Local before/after comparison against dev at 26047f6b, using the actual CRUD implementations on Python 3.14 / SQLite in memory:

Measurement Before After Reduction
Create template with a 10,000-user group: median elapsed time 266.61 ms 6.63 ms 97.51%
Same operation: peak traced Python allocations 17.935 MiB 0.0607 MiB 99.66%
User objects loaded during creation 10,000 0 100%
SELECT statements for creation 5 3 40%
SELECT statements for a list of 100 templates 101 2 98.02%

Timing uses five alternating measurements per implementation after one warmup each, with a fresh session per call and response validation included. Memory was measured separately with tracemalloc; it is not process RSS. These are local synthetic CRUD measurements, not end-to-end API latency or a production guarantee. Larger lists use SQLAlchemy's select-in batches.

Type of change

  • Refactor / cleanup (performance)

Checklist

  • Tested locally.
  • Existing tests and additional local regression checks passed; no test or benchmark files included.
  • No documentation, translation, API, model, or migration changes required.
  • No secrets or unrelated changes included; only app/db/crud/user_template.py changes.

Testing

  • pytest tests/api/test_user_template.py -q --tb=short: 16 passed, with both DATABASE_URL and SQLALCHEMY_DATABASE_URL pointing to the same isolated temporary SQLite database.
  • Local before/after regression checks: equivalent serialized create/update/list responses; group reassignment; empty/omitted update group IDs; ID filters; pagination; empty results; zero user loads during optimized updates.
  • ruff check --no-fix app/db/crud/user_template.py, ruff format --check app/db/crud/user_template.py, and git diff --check: passed.
  • Repository-wide ruff check --no-fix . reports seven pre-existing issues in unchanged files; the modified file is clean.

Screenshots

Not applicable.

Notes for reviewers

The existing group validation and authorization paths remain intact. PR #785 adds template ownership and does not implement this loading optimization; PR #772 optimizes group summaries rather than template CRUD. This PR targets dev, as next referenced in CONTRIBUTING no longer exists.

Summary by CodeRabbit

  • Performance
    • Improved loading of user templates and their associated groups by avoiding unnecessary related data, while keeping the information available with templates.

@coderabbitai

coderabbitai Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: aacb52ad-9f0e-488c-a518-c6597f6f3f1e

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: b8e43f21-2931-4971-b09d-0348c7aeb0fc

📥 Commits

Reviewing files that changed from the base of the PR and between 26047f6 and 82c6954.

📒 Files selected for processing (1)
  • app/db/crud/user_template.py

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


Walkthrough

User template creation and modification fetch associated groups without loading users or inbounds. User template retrieval eagerly loads groups and no longer runs a separate attribute-loading loop.

Changes

User template group loading

Layer / File(s) Summary
Fetch groups for template mutations
app/db/crud/user_template.py
create_user_template and modify_user_template fetch associated groups with user and inbound loading disabled.
Eagerly load groups in template retrieval
app/db/crud/user_template.py
get_user_templates eagerly loads groups and returns query results without the per-template attribute-loading loop.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Suggested reviewers: m03ed

Merge Risk: ⚪ Minimal · up to 82c69

Template responses retain their group IDs, and no merge-blocking issue was identified.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
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 changes: avoiding group-member loading and batching list relationship loading.
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. Analyzed 3 functions across 1 files.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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

A rabbit checks the template rows,
Where eager-loaded group data flows.
No users or inbounds tag along,
The loading loop is gone before long.
Soft paws hop through the query night,
Group links arrive in tidy flight.

Comment @coderabbitai help to get the list of available commands.

@dr-hoseyn

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@T3ST3ST3R0N
T3ST3ST3R0N merged commit c600c6a into PasarGuard:dev Oct 10, 2026
9 checks passed
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.

2 participants