FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-133672: Allow LOAD_FAST to be optimized to LOAD_FAST_BORROW by ljfp · Pull Request #148999 · python/cpython · GitHub

/ cpython Public

gh-133672: Allow LOAD_FAST to be optimized to LOAD_FAST_BORROW - #148999

Open
ljfp wants to merge 2 commits into
python:mainfrom
ljfp:fix-issue-133672
Open

gh-133672: Allow LOAD_FAST to be optimized to LOAD_FAST_BORROW#148999
ljfp wants to merge 2 commits into
python:mainfrom
ljfp:fix-issue-133672

Conversation

ljfp commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

The LOAD_FAST_BORROW instruction loads a borrowed reference onto the operand stack, which is a performance optimization that avoids unnecessary reference counting operations.

Previously, we were only applying this optimization when the reference was consumed within the same basic block. If the value was still on the stack at the end of a basic block (indicated by the REF_UNCONSUMED flag), we wouldn't perform the optimization.

However, there are cases where it's safe to use LOAD_FAST_BORROW even when the value is still on the stack at the end of a basic block. The optimization is safe as long as every successor path preserves the supporting reference in the frame until the borrowed reference is consumed, including paths through exception handlers.

This fix allows us to optimize more cases, which seems to be particularly important for the virtual iterators implementation (PR #132555) where the iterable for a loop is often live at basic block end.

Fixes gh-133672.
Supersedes gh-133721.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
ljfp force-pushed the fix-issue-133672 branch from bb9166b to 08238d2 Compare May 6, 2026 21:59

ljfp commented May 6, 2026
edited
Loading

Copy link
Copy Markdown
Contributor Author

Hey @Fidget-Spinner , following up on your earlier comment: #133721 (comment)

It took me way longer than expected (almost a year!), but I’ve now opened this replacement PR which I hope fixes the issues found in the previous one.

Compared with the earlier PR, this version has a cleaned-up branch history and includes several correctness fixes found while chasing CI failures. In particular, the borrow-safety analysis now checks paths through exception handlers, avoids optimizing copied stack references into LOAD_FAST_BORROW, and handles FOR_ITER fallthrough paths so borrowed values are not kept alive after their backing local can be overwritten.

This new PR is passing tests now, and I think it’s ready for review.

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

github-actions Bot added the stale Stale PR or inactive for long period of time. label Aug 19, 2026
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

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LOAD_FAST_BORROW not being used even when safe to do so, if value is live at BB end.

1 participant


Back | FazBrowse Home | New Git URL