Skip to content

Extend and improve LOAD_COMMON_CONSTANT #148871

Description

@markshannon

Currently LOAD_COMMON_CONSTANT only loads 7 not-so-common constants and does so fairly inefficiently.

It could be improved in two ways:

  • Add some more constants, especially None, but also "", True, False and -1
  • Make the constants that it does load both statically allocated and immortal to avoid the pointer chasing and incref operation. Only any and all would need changing.

After some consideration, I think it best to leave the common constants table per interpreter, and not statically allocate them.

  • The changes to any and all may be breaking.
  • We also need to handle AssertionError and NotImplementedError will be tricky to make static

They should all be immortal though, and stored as _PyStackRefs not PyObject *s.

Linked PRs

Activity

  1. added a commit that references this issue on May 2, 2026
  2. markshannon commented on May 2, 2026

    @markshannon
    MemberAuthor

    Now #148971 is merged, we need to make LOAD_COMMON_CONSTANT fast.

    Instead of storing an array of PyObject *s the array should contain _PyStackRefs and all objects should be made immortal.

    So, instead of

    interp->common_consts[CONSTANT_BUILTIN_ALL] = all;
    

    we should have

    _Py_SetImmortal(all);
    interp->common_consts[CONSTANT_BUILTIN_ALL] = _PyStackRef_FromPyObjectBorrow(all);
    

    most objects in the common constants set are already immortal.

    Then the code for LOAD_COMMON_CONSTANT becomes

            value = tstate->interp->common_consts[oparg];
    

    Without any need to handle refcounts.

  3. added a commit that references this issue on May 11, 2026
  4. added a commit that references this issue on May 21, 2026
  5. added a commit that references this issue on May 21, 2026
  6. added a commit that references this issue on May 28, 2026
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)performancePerformance or resource usagetype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions