GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-133672: Allow LOAD_FAST to be optimized to LOAD_FAST_BORROW by ljfp · Pull Request #148999 · python/cpython

Conversation

@ljfp

@ljfp 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

contre95, maud3l, mauriciodeoliveirareis, and perepalacin reacted with thumbs up emoji perepalacin reacted with rocket emoji
All reactions
  • 👍 4 reactions
  • 🚀 1 reaction
@ljfp
ljfp requested review from iritkatriel and markshannon as code owners April 25, 2026 21:31
@bedevere-app bedevere-app Bot added the awaiting review label Apr 25, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Apr 25, 2026
LOAD_FAST_BORROW not being used even when safe to do so, if value is live at BB end. #133672
Open
@ljfp
ljfp force-pushed the fix-issue-133672 branch 2 times, most recently from ebd2c01 to bb9166b Compare April 26, 2026 13:53
@ljfp
pythongh-133672: Allow LOAD_FAST to be optimized to LOAD_FAST_BORROW
08238d2
Co-authored-by: Copilot <[email protected]>
@ljfp
ljfp force-pushed the fix-issue-133672 branch from bb9166b to 08238d2 Compare May 6, 2026 21:59
@ljfp

ljfp commented May 6, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ljfp
Merge branch 'main' into fix-issue-133672
28d06a6
@github-actions

github-actions Bot commented Aug 19, 2026

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Aug 19, 2026
@eendebakpt

eendebakpt commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

@ljfp Sorry for the delay in reviewing. The PR needs a rebase onto main. Can you also determine how many opcodes are converted from LOAD_FAST to the borrowed version with this approach? I have a branch main...eendebakpt:cpython:load_fast_borrow_handlers that also adds more borrowed loads, it improves from 94% to roughly 97% in converted opcodes (tested in the Lib/ from cpython).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner
@iritkatriel iritkatriel Awaiting requested review from iritkatriel iritkatriel is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review

Projects

None yet

Milestone

No milestone

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.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@ljfp @eendebakpt
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.