Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Use tagged pointers on the stack in the default build. #127705

Closed
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement
@markshannon

Description

@markshannon

Currently all references to objects in frameobjects use_PyStackRef instead ofPyObject *.
This is necessary for the free-threaded build to support deferred references.

For the default build_PyStackRef is just an alias forPyObject *.
We should change_PyStackRef to use proper tagged pointers in the default build for two important reasons:

  • It will reduce the maintenance burden of using tagged pointers if they were the same in both builds
  • It offers a lot of optimization potential. The overhead of reference counting operations is large, and tagged pointers will allow us to reduce that overhead considerably.

My initial implementation is0.8% slower, although I'd like to get that closer to 0 before merging anything. There is some speedup in the GC due to streamlined immortality checks, and some slowdown due to increased overhead of turning newPyObject * references into_PyStackRefs.

This small slowdown will allow us a large speedup (maybe more than 5%) as we can do the following:

  • Reduce the overhead of refcount operations by using tagged references for the majority ofLOAD_ instructions in the interpreter.
  • Completely eliminate many decref operations by tracking which references are tagged in the JIT.

The tagging scheme:

TagMeaning
00Normal pointers
01Pointers with embedded reference count
10Unused
11Pointer to immortal object1 (including NULL)

This tagging scheme is chosen as it provides the best performance for the most common operations:

  • PyStackRef_DUP: Can check to see if the object's reference count needs updating with a single check and no memory read:ptr & 1
  • PyStackRef_CLOSE: As for PyStackRef_DUP, only a single bit check is needed
  • PyStackRef_XCLOSE: SinceNULL is treated as immortal and tagged, this is the same as PyStackRef_CLOSE.

Maintaining the invariant that tag11 is used for all immortal objects is a bit expensive, but can be mitigated by pushing the conversion fromPyObject * to_PyStackRef down to a point where it is known whether an object is newly created or not.
For newly created objectsPyStackRef_FromPyObjectStealMortal can be used which performs no immortality check.


  1. Actually, any object that was immortal when the reference was created. References to objects that are made immortal after the reference is created would have the low bits set to00, or01. This is OK as immortal refcounts have a huge margin of error and the number of possible references to one of these immortal objects is very small.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp