Skip to content

Only check pending objects in is_session_modified - #417

Open
mi-netizen007 wants to merge 1 commit into
sqlalchemy-continuum:mainfrom
mi-netizen007:fix/session-modified-scan
Open

mi-netizen007 wants to merge 1 commit into
sqlalchemy-continuum:mainfrom
mi-netizen007:fix/session-modified-scan

Conversation

@mi-netizen007

@mi-netizen007 mi-netizen007 commented Oct 25, 2026 •

Copy link
Copy Markdown

Description

Fixes #406.

is_session_modified() is the gate UnitOfWork.process_before_flush uses to decide whether a flush needs a transaction. It iterated versioned_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.dirty and session.deleted, which are the only objects the old loop could return True for. I left versioned_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):

                     is_modified calls        wall         version rows
 rows/flushes      main      branch     main     branch       (both)
           50     1,275          50   0.031s     0.023s          100
          100    17,550         150   0.179s     0.049s          300
          200   102,600         350   0.915s     0.093s          700
          400   482,700         750   3.886s     0.188s         1500

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 added tests/utils/test_is_session_modified.py. test_does_not_check_clean_objects fails 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:

  • A documentation / typographical error fix
    • Good to go, no issue or tests are needed
  • A short code fix
    • Please include the issue number, and create an issue if none exists, which
      must include a complete example of the issue.
    • Please include: Fixes: #<issue number> in the commit message
    • Please include tests. A test that fails without the fix is strongly preferred.
  • A new feature implementation
    • Please include the issue number, and create an issue if none exists, which
      must include a complete example of how the feature would look.
    • Please include tests and documentation updates.

Summary by CodeRabbit

  • Performance
    • Improved repeated flush performance for sessions containing many objects by checking only new, changed, or deleted objects.

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
@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

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: 361bee4a-3837-4c31-9ab9-ee6b4aae0c53
Commits

Reviewing files that changed from the base of the PR and between 3d3c86b and 0332925.

Files selected for processing: 4
  • CHANGES.rst
  • docs/changelog.md
  • sqlalchemy_continuum/utils.py
  • tests/utils/test_is_session_modified.py

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

Walkthrough

is_session_modified() now checks only new, dirty, and deleted session objects instead of scanning all versioned objects. Tests cover clean and modified sessions. The changelog describes the change and its reported effect on repeated flushes in large sessions.

Changes: Session change detection

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: Normal

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: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning 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… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 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 v…
Linked Issues check Passed Issue #406 requires 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_mod…
Out of Scope Changes check Passed The whole-PR diff against the merge base changes is_session_modified() in sqlalchemy_continuum/utils.py. That change implements #406 directly. The added test file `tests/utils/test_is_session_modifi…
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 #406 requires 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 iterates chain(session.new, session.dirty, session.deleted) and passes each object to is_modified_or_deleted(). The reviewed diff against the merge base shows this as the only source change. chain is 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 adds tests/utils/test_is_session_modified.py, which covers clean, new, modified, and deleted sessions. test_does_not_check_clean_objects monkeypatches utils.is_modified and 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 #406 was 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 #406 directly. The added test file tests/utils/test_is_session_modified.py supports 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 #406 and documents the same change. That is a documentation change tied to the objective. No unrelated source module, public API, or versioned_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.

❤️ Share

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

@greptile-apps

greptile-apps Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

Speeds up is_session_modified() by checking only new, dirty, and deleted objects instead of every object held by the session.

  • Flush checks skip clean objects in large sessions.

Reviews (1) · Last reviewed commit: "Only check pending objects in is_session..." · Reviewed by Greptile

This branch has not been deployed

No deployments
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.

Every flush change-detects the whole session, making many-flush transactions quadratic

1 participant