perf: find the versions directory without inspect.stack() - #54
Merged
Conversation
`get_down_revision(revision)` locates the versions directory by walking the call stack, and it did so with `inspect.stack()`, which also reads the source context of every frame. It runs once per migration, so on a long chain it dominated building the revision map: in the Mergify engine's test suite, collecting the tree spent most of its time here, because the collection stack is deep and 400 migration loads each paid for it. Walk the frame objects with `f_back` instead and read only `f_code.co_filename`, which is all the lookup uses. Collecting the engine's 15,680 tests goes from 20.8-31.8s to 6.2-9.0s. A new test executes a real migration file from a `versions/` directory, since the existing one mocks the lookup out. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Change-Id: I893407ec8055a6f18b083fef182070a13d531705
Contributor
Merge Protections🟢 All 6 merge protections satisfied — ready to merge. Show 6 satisfied protections🟢 🤖 Continuous Integration
🟢 👀 Review Requirements
🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
🟢 🔎 Reviews
🟢 📕 PR description
🟢 🚦 Auto-queueWhen all merge protections are satisfied, this pull request will be queued automatically. |
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
No unresolved blocking issues were identified.
Review effort: Lite
Findings: None
What changed in this PR
Improves migration version-directory discovery performance by replacing inspect.stack() with direct frame traversal and adds real migration coverage.
Changes:
- Walks caller frames via
f_backandco_filename. - Cleans up frame references.
- Adds an integration-style migration test.
| File | Description |
|---|---|
tests/test_chain.py |
Tests discovery from an executed migration file. |
alembic_git_revisions/_chain.py |
Optimizes caller-frame traversal. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
jd
marked this pull request as ready for review
September 23, 2026 17:21
kozlek
approved these changes
Sep 23, 2026
Contributor
Merge Queue Status
This pull request spent 10 seconds in the queue, including 2 seconds running CI. Required conditions to merge
|
mergify
Bot
deleted the
devs/jd/perf/discover-without-inspect-stack/find-versions-dir-without-inspect-stack--893407ec
branch
September 23, 2026 17:56
This branch was successfully deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
get_down_revision(revision)locates the versions directory by walkingthe call stack, and it did so with
inspect.stack(), which also readsthe source context of every frame. It runs once per migration, so on a
long chain it dominated building the revision map: in the Mergify
engine's test suite, collecting the tree spent most of its time here,
because the collection stack is deep and 400 migration loads each paid
for it.
Walk the frame objects with
f_backinstead and read onlyf_code.co_filename, which is all the lookup uses. Collecting theengine's 15,680 tests goes from 20.8-31.8s to 6.2-9.0s. A new test
executes a real migration file from a
versions/directory, since theexisting one mocks the lookup out.
🤖 Generated with Claude Code
Co-Authored-By: Claude Opus 5.5 (1M context) noreply@anthropic.com