Skip to content

perf: Skip the reload of the loaded but empty relations without force - #70

Merged
joamag merged 2 commits into
masterfrom
perf/relation-loaded-empty
Oct 31, 2026
Merged

joamag merged 2 commits into
masterfrom
perf/relation-loaded-empty

Conversation

@joamag

@joamag joamag commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Closes #69

Summary

  • relation(name, force=False) checks if the relation is loaded with has_value (the name set in the entity) instead of the truthiness of its value, so a relation that is loaded but empty (an empty list of a to-many relation or an unset to-one relation, None) is not loaded again on every call, its value being read only when it's returned
  • the relations that were never loaded (still the class level description, not set in the entity) are loaded as before, and force=True (the default) always loads
  • no caller in colony-plugins, the 26 callers of Omni all mean "load in case it's not loaded"
  • tests for the loading with and without force, of a loaded, an empty, an unset and a never loaded relation, and with an explicit entity manager (MockRelationEntity and MockEntityManager.relation in the mocks)

Performance

Measured in Omni (the benchmark of hivesolutions/omni#336, MariaDB 11.8, the version of omni-ldj, with the demo data plus 2000 users), where SystemUser.get_from_session runs several times on every request:

  • a read of the user in session goes from 1 to 3 queries (0.69 to 2.64 ms) to none (3 to 6 µs) for the users with no employee, store or own ACL entries
  • authenticated API calls: +25% requests per second for such a user (1.20 to 0.60 SELECTs per request)

Regression checks

  • Every colony-plugins test passes (1318)
  • The complete Omni suite passes with the mvc_utils of this branch (2074 tests, SQLite), and test_get_from_session_empty of hivesolutions/omni#343 passes with it while failing with the mvc_utils of master
  • Coverage (coverage.py): changed lines 48/49 (98.0%), the line left out being the get_description of the new test case, as for every other test case

- Check if the relation is loaded with has_value instead of the truthiness of its value
- Stop querying the empty lists and unset to-one relations on every call without force
- Cover the loading of the relations with and without force in the tests
@coderabbitai

coderabbitai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Caution

Review failed

The pull request is closed.

  • 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

@joamag joamag self-assigned this Oct 31, 2026
@joamag joamag added the enhancement New feature or request label Oct 31, 2026
@joamag
joamag marked this pull request as ready for review October 31, 2026 18:31
Copilot AI balanced review requested due to automatic review settings October 31, 2026 18:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7b58189f26

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread mvc/src/mvc_utils/entity_model.py Outdated
Comment on lines 1927 to 1928
if not force and self.has_value(name):
return value

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Exclude lazy sentinels when checking whether a relation is loaded

After EntityClass.reset() (also called by detach(reset=True) and detach_l(reset=True)), relations are stored in the instance as colony.Lazy, not removed. In data/src/entity_manager/structures.py, has_value() only checks membership in __dict__, and get_value() returns that sentinel unchanged. Consequently, relation(name, force=False) now treats these unloaded relations as loaded and returns colony.Lazy without asking the entity manager to fetch them. Exclude lazy sentinel values from this early return while retaining the optimization for genuine empty lists and None.

Useful? React with 👍 / 👎.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-31T18:33:46.459939Z 7b58189 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@joamag
joamag merged commit 7b2a22d into master Oct 31, 2026
42 checks passed
@joamag
joamag deleted the perf/relation-loaded-empty branch October 31, 2026 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Skip the reload of the loaded but empty relations without force

2 participants