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 — Add a C API function to detect temporaries · Issue #133164 · python/cpython

Add a C API function to detect temporaries #133164

New issue
Copy link
New issue
Copy link
Closed
Closed
Add a C API function to detect temporaries#133164
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-featureA feature request or enhancementA feature request or enhancement

Description

@ngoldbaum
ngoldbaum
opened on Apr 29, 2025
Issue body actions

Feature or enhancement

Proposal:

NumPy has an optimization to detect temporaries created via the NumPy C API (e.g. in NumPy internals) and elide them. This can lead to a significant performance improvement for some operations.

In numpy/numpy#28681, @colesbury proposed adding some code to handle the change to use stackrefs internally in CPython, which broke the NumPy temporary elision heuristics in 3.14.

We later added that code more or less verbatim to the NumPy main branch:

https://github.com/numpy/numpy/blob/d692fbccd98cb880812b32936e5f94fcfe55053f/numpy/_core/src/multiarray/temp_elide.c#L119-L152

This unblocks testing NumPy on the 3.14 beta but we should really have at least an unstable C API function we can call here rather than relying on CPython internals.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

#133140 (comment)

Linked PRs

  • gh-133164: Add PyUnstable_Object_IsUniqueReferencedTemporary C API #133170

Activity

  1. ngoldbaum
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Apr 29, 2025
  2. colesbury commented on Apr 29, 2025

    @colesbury
    colesbury
    on Apr 29, 2025
    Contributor
    More actions

    To add some more context, in NumPy, if you write:

    x = np.array([ ... ])
    foo(x)

    In 3.14, x will have a reference count of 1 within the call to foo() (from the local variable x), whereas before 3.14, it would be at least 2. The reason is that interpreter now safely borrows references when it can, such as in function calls, if it knows that it already holds a strong reference. It's an optimization in the generated bytecode that replaces some LOAD_FAST bytecodes with the new LOAD_FAST_BORROW bytecode.

    This means that in some contexts, it's not safe to assume that Py_REFCNT(x) == 1 means that you can modify x in-place. They weren't 100% true before because of the C API (see the explanation in numpy/numpy@e6c397b), but the 3.14 change definitely breaks some code.

    We want to provide some C API that NumPy and other packages can uses to safely replace their Py_REFCNT(x) == 1 checks. Ideally, it should also avoid the need for backtrace() too.

    The workaround for NumPy, which I think we should encapsulate in a new C API function, is to check that:

    1. The reference count is 1
    2. There exists a single temporary on the interpreter's stack that is a strong (not borrowed) reference to the relevant object

    We might want to call this something like: PyUnstable_Object_IsUniqueTemporary(ob)

    also cc @pablogsal

  3. colesbury
    added
    topic-C-API
    on Apr 29, 2025
  4. colesbury
    added a commit that references this issue on Apr 29, 2025

    pythongh-133164: Add `PyUnstable_Object_IsUniqueTemporary` C API

    32b6cd0
  5. picnixz
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Apr 30, 2025
  6. colesbury
    added a commit that references this issue on Apr 30, 2025

    Merge branch 'main' into pythongh-133164-unique-temporary

    07f3b23
  7. ZeroIntensity commented on May 1, 2025

    @ZeroIntensity
    ZeroIntensity
    on May 1, 2025
    Member
    More actions

    Should this (and probably #133140) be a 3.14 blocker? That beta freeze is creeping up on us quickly.

  8. colesbury
    added 2 commits that reference this issue on May 2, 2025

    Merge branch 'main' into pythongh-133164-unique-temporary

    f30ccf4

    gh-133164: Add `PyUnstable_Object_IsUniqueReferencedTemporary` C API (g…

    f237953
  9. ZeroIntensity
    closed this as completedon May 6, 2025
  10. Pranjal095
    added a commit that references this issue on Jul 12, 2025

    pythongh-133164: Add `PyUnstable_Object_IsUniqueReferencedTemporary` …

    ba5b945
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.