Repository navigation
Only check pending objects in is_session_modified - #417
mi-netizen007 wants to merge 1 commit into
Conversation
is_session_modified() gates every flush, and it walked the whole identity map, change-detecting every versioned object whether or not it had been touched. In a transaction that keeps many objects loaded and flushes often, the check grows with the session on every flush, so the total goes quadratic. A clean persistent object has no attribute history, so only session.new, session.dirty and session.deleted can make the check true. The version rows written are unchanged. Fixes: sqlalchemy-continuum#406
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Narrow the modification check sqlalchemy_continuum/utils.py, tests/utils/test_is_session_modified.py, CHANGES.rst, docs/changelog.md |
is_session_modified() checks the session’s new, dirty, and deleted objects. Tests cover clean, added, changed, and deleted objects, and verify that clean objects are not checked. The changelog describes the check and its reported effect on repeated flushes. |
Priority:
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers: josecsotomorales
Merge Risk: 
Minimal · up to 03329
-
No actionable merge-blocking risk is established by the reviewed change.
Pre-merge checks: 
4 · 
1
-
Failed checks: 1 warning Passed checks: 4 passed-
Full details: Title check
-
Explanation
The title "Only check pending objects in is_session_modified" matches the main change. The PR narrows is_session_modified() to session.new, session.dirty, and session.deleted instead of scanning all versioned objects, which is the core intent of the change. The title is specific, concise, and six words long. It names the function and the behavior change, so a teammate scanning history can identify the primary change.
Full details: Linked Issues check
-
Explanation
Issue
#406requires that the flush gate stop change-detecting clean versioned objects across the whole identity map. It suggests checking only new, dirty, and deleted objects. At head,is_session_modified()in sqlalchemy_continuum/utils.py iterateschain(session.new, session.dirty, session.deleted)and passes each object tois_modified_or_deleted(). The reviewed diff against the merge base shows this as the only source change.chainis already imported. Clean persistent objects are no longer visited, so the cost of the gate follows the pending objects. The change does not alter the version-writing path, which matches the issue requirement that row output stay the same.versioned_objects()is left unchanged, which the issue does not require. The PR addstests/utils/test_is_session_modified.py, which covers clean, new, modified, and deleted sessions.test_does_not_check_clean_objectsmonkeypatchesutils.is_modifiedand asserts that no clean object is checked. On the base code, that test would fail because the old loop checks every versioned object. The PR-reported repro counts and version-row equivalence were not re-run in this review. They do not change the verdict, because the code change itself implements the requested narrowing. No unmet coding requirement from#406was found.
Full details: Out of Scope Changes check
-
Explanation
The whole-PR diff against the merge base changes
is_session_modified()in sqlalchemy_continuum/utils.py. That change implements#406directly. The added test filetests/utils/test_is_session_modified.pysupports that change: it covers the four session states and the non-check of clean objects. The changelog entry that replaces the Unreleased TODO, reported in the raw summary, references#406and documents the same change. That is a documentation change tied to the objective. No unrelated source module, public API, orversioned_objects()behavior change was found in the reviewed diff. The raw summary lists no changes to exported declarations. The changelog file itself was not read directly. Its content comes from the summary only, and it does not alter code behavior.
-
Full details: Docstring Coverage
-
Explanation
Docstring coverage is 22.22% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 9 functions across 2 files. (2 skipped: 2 unsupported.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
-
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.
Comment @coderabbitai help to get the list of available commands.
[Medium impact] The PR appears safe to merge; no actionable issues were found. SummarySpeeds up
Reviews (1) · Last reviewed commit: "Only check pending objects in is_session..." · Reviewed by Greptile |
Description
Fixes #406.
is_session_modified()is the gateUnitOfWork.process_before_flushuses to decide whether a flush needs a transaction. It iteratedversioned_objects(session), which is the whole identity map, so every flush change-detected every versioned object in the session, clean or not. A transaction that holds a big working set and flushes many times ends up quadratic.A clean persistent object has no attribute history, so it can never make the check true. This narrows the loop to
session.new,session.dirtyandsession.deleted, which are the only objects the old loop could returnTruefor. I leftversioned_objects()alone since it's public and documented as returning every versioned object.The repro script from #406, run against main and this branch (SQLite, SQLAlchemy 2.1.1):
I also compared the full contents of the version table between the two (updates and deletes mixed in, hashed row by row) at each size, and they're identical.
There were no direct tests for
is_session_modified, so I addedtests/utils/test_is_session_modified.py.test_does_not_check_clean_objectsfails on main. The full suite passes on sqlite, postgres, postgres-native and mysql with SQLAlchemy 2.1.1, and on sqlite with 2.0.55.Checklist
This pull request is:
must include a complete example of the issue.
Fixes: #<issue number>in the commit messagemust include a complete example of how the feature would look.
Summary by CodeRabbit