Repository navigation
gh-162066: Convert LOAD_FAST to LOAD_FAST_BORROW in exception handlers - #162068
Open
eendebakpt wants to merge 1 commit into
Open
eendebakpt wants to merge 1 commit into
eendebakpt wants to merge 1 commit into
Conversation
Once the SETUP_* pseudo-instructions have been converted, exception handler blocks are only reachable through the per-instruction exception edges. optimize_load_fast() only followed jumps and fallthroughs, so the code in exception handlers was never considered for the LOAD_FAST to LOAD_FAST_BORROW conversion. Push the exception handler blocks onto the work list as well. END_ASYNC_FOR has a jump target that is only used for monitoring, so it must not be treated as a jump, like in calculate_stackdepth(). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
eendebakpt
requested review from
Eclips4,
iritkatriel and
markshannon
as code owners
November 15, 2026 22:04
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.
The compiler replaces
LOAD_FASTby the cheaperLOAD_FAST_BORROWwherever it can show that the local variable outlives the loaded value. The pass that does this never looks at exception handlers, so code in a handler, or code that can only be reached through one, keeps itsLOAD_FASTinstructions. This affectsexceptandfinallybodies, the cleanup code ofwithstatements, code after atrywhose body returns, and all code that follows anasync forloop.This PR makes the pass visit exception handlers as well. The analysis of each block is unchanged.
Bytecode statistics for all code objects from
Lib/:LOAD_FASTLOAD_FAST_LOAD_FASTSTORE_FAST_LOAD_FASTLOAD_FAST_BORROWLOAD_FAST_BORROW_LOAD_FAST_BORROWNote that the number of
STORE_FAST_LOAD_FASTis very low now, I think the opcode should be removed (see #160921).Statistics script
On the default build the effect is one reference count pair per load, so small. On the free-threaded build a strong load of an object can lead to contention.
The benchmark calls one method of a parser object from several threads. The method accepts a size as
"4096"or with a suffix as in"100m":The code after
except ValueError: passis only reachable through the handler, so on main it loadsshiftswithLOAD_FAST. Free-threaded build, time per thread in ms:Benchmark script
@markshannon