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-145629: Expand block inlining to enable LOAD_FAST_BORROW optimization for return ternary expressions by cocolato · Pull Request #146503 · python/cpython

Conversation

@cocolato

@cocolato cocolato commented Mar 27, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

In existing CFG optimization logic, for equivalent conditional code, ternary expressions do not get the LOAD_FAST optimization applied, unlike standard if/else statements:

# Ternary Expression
def f(a, b):
    return a if a < b else b

# if/else
def g(a, b):
    if a < b:
        return a
    else:
        return b

The return in the if/else branch is successfully optimized to LOAD_FAST_BORROW, whereas the else branch in the ternary expression can only generate a standard LOAD_FAST.

Before being passed to optimize_load_fast (the entry point for the reference checking phase), the control flow graphs of the two structures exhibit subtle differences:

For if/else, its else branch naturally belongs to a single basic block, containing both the reference being loaded and the return instruction that consumes that reference:

Block A (jump here from if failure):
    LOAD_FAST          1 (b)
    RETURN_VALUE

Because the local reference is consumed directly within this basic block, optimize_load_fast can determine that the safety constraint is satisfied and convert it into a prioritized LOAD_FAST_BORROW.

However, for a trinary expression, due to the structural characteristics of the AST, the code generator produces the following CFG, which prevents optimize_load_fast from performing the optimization:

Block A (evaluation of the else branch expression):
    LOAD_FAST          1 (b)
    || 
    \/
Block B (small exit return block):
    RETURN_VALUE

So this PR extends the inlining capabilities of basicblock_inline_small_or_no_lineno_blocks:

  • When a basic block is connected to the next block via Fallthrough and no conditional jump instructions block the path, if the target block is an extremely small exit block (e.g., containing only RETURN_VALUE), we can still safely copy it directly and inline it at the end of the underlying block.
  • Issue: LOAD_FAST is not always converted to LOAD_FAST_BORROW in a basicblock #145629

Sorry, something went wrong.

Uh oh!

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

All reactions
@cocolato
optimize CFG bb inline
610738b
@cocolato
cocolato requested review from iritkatriel and markshannon as code owners March 27, 2026 04:11
@bedevere-app bedevere-app Bot mentioned this pull request Mar 27, 2026
LOAD_FAST is not always converted to LOAD_FAST_BORROW in a basicblock #145629
Open
@bedevere-app bedevere-app Bot added the awaiting review label Mar 27, 2026
@blurb-it
📜🤖 Added by blurb_it.
d930c34
@Fidget-Spinner

Fidget-Spinner commented Mar 27, 2026

Copy link
Copy Markdown
Member

To my untrained (for the compiler) eye, this looks fine, but maybe someone else needs to review it.

cocolato reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

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

@Fidget-Spinner

Fidget-Spinner commented Mar 27, 2026

Copy link
Copy Markdown
Member

@mpage

All reactions

Sorry, something went wrong.

Uh oh!

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

@github-actions

github-actions Bot commented May 8, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 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 May 8, 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 stale Stale PR or inactive for long period of time.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

2 participants

@cocolato @Fidget-Spinner
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.