python / python/cpython

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

Open
#133,672 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.15 interpreter-core performance type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

This function

def f(x, y, c):
    return 1 + (x if c else y)

compiles to

  1           RESUME                   0

  2           LOAD_SMALL_INT           1
              LOAD_FAST_BORROW         2 (c)
              TO_BOOL
              POP_JUMP_IF_FALSE        9 (to L1)
              NOT_TAKEN
              LOAD_FAST_BORROW         0 (x)
              BINARY_OP                0 (+)
              RETURN_VALUE
      L1:     LOAD_FAST                1 (y)
              BINARY_OP                0 (+)
              RETURN_VALUE

Note that the load of y uses LOAD_FAST even though LOAD_FAST_BORROW is safe.

This becomes important with virtual iterators as the iterable for the loop is live at BB end.

Linked PRs
  • gh-133721
  • gh-148999

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported disassembly for f(x, y, c) and investigate the compiler handling of values live at a basic-block end. Compare the conditional branches and virtual-iterator case, then verify that y uses LOAD_FAST_BORROW when safe and add or update coverage for the reported behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.