-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Optimize fast_locals and atomic ordering #7289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6895ddb
Relax RefCount atomic ordering from SeqCst to Arc pattern
youknowone 89703b6
Reuse existing Vec via prepend_arg in execute_call
youknowone 6a9be9f
Skip downcast_ref checks in invoke when tracing is disabled
youknowone 958853f
Replace fastlocals PyMutex with UnsafeCell-based FastLocals
youknowone 516ff0f
Auto-format: cargo fmt --all
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Replace fastlocals PyMutex with UnsafeCell-based FastLocals
Eliminate per-instruction mutex lock/unlock overhead for local variable access. FastLocals uses UnsafeCell with safety guaranteed by the frame's state mutex and sequential same-thread execution. Affects 14+ lock() call sites in hot instruction paths (LoadFast, StoreFast, DeleteFast, and their paired variants).
- Loading branch information
commit 958853f51f9041ce444d9d22b1b2b655f967633a
There are no files selected for viewing
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 9197
FastLocals synchronization contract is violated by externally reachable paths;
f_locals()getter permits concurrent unsynchronized access.The doc comment claims access is "serialized by the frame's state mutex in
with_exec()", but thef_locals()getter (line 456 in builtins/frame.rs) directly callslocals()which accessesfastlocalswithout holding that guard (line 395 in frame.rs). The comment at lines 405–406 acknowledges fastlocals is intentionally accessed outsidewith_exec()to avoid blocking. Sincef_locals()is Python-exposed and callable from any thread, andFastLocalsis markedSend + Sync, concurrent calls from multiple threads will perform unsynchronized reads/writes to the sameUnsafeCell, violating Rust's aliasing guarantees.Gate fastlocals borrows behind a guard/token tied to frame execution, or document that frames must not be accessed concurrently and remove
Send + Sync(or add runtime checks that panic if accessed cross-thread).🤖 Prompt for AI Agents