Skip to content

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

Open
ljfp wants to merge 1 commit intopython:mainfrom
ljfp:fix-issue-133672
Open

gh-133672: Allow LOAD_FAST to be optimized to LOAD_FAST_BORROW#148999
ljfp wants to merge 1 commit intopython:mainfrom
ljfp:fix-issue-133672

Conversation

@ljfp
Copy link
Copy Markdown
Contributor

@ljfp ljfp commented Apr 25, 2026

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 ljfp force-pushed the fix-issue-133672 branch from 2bcbf29 to ebd2c01 Compare April 25, 2026 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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