Skip to content

Avoid double checking tp_as_number, tp_as_sequence and tp_as_mapping #149180

Open

Description

@markshannon

Feature or enhancement

The PyTypeObject fields tp_as_number, tp_as_sequence and tp_as_mapping can be set to NULL
which means that any code that wants to access a field in those structs needs to first check for NULL.
e.g. checking if an object is a sequence:
if (Py_TYPE(s)->tp_as_mapping && Py_TYPE(s)->tp_as_mapping->mp_length) {

this is ugly, a bit error prone and slower than necessary.
Instead, during PyType_Ready, any of these fields that are NULL should be made to pointer to a struct full of NULLs.
This would remove the need for the extra check:
if (Py_TYPE(s)->tp_as_mapping->mp_length) {
since we would be guaranteed that Py_TYPE(s)->tp_as_mapping is never NULL.

Immutable types can all share common constant structs, so this should use very little extra memory.

Linked PRs

Activity

  1. added
    type-featureA feature request or enhancement
    performancePerformance or resource usage
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    on Apr 30, 2026
  2. markshannon commented on May 6, 2026

    @markshannon
    MemberAuthor

    If done properly, using structs of nulls will be faster than using null pointers, as the hot path will less branchy, even though the cold paths of constructing and modifying classes may be slower.

    But it needs to be done with care, making sure that the correct test is being performed in all cases.

    Many tests of the form tp->tp_as_mapping == NULL can be removed, but some will need to be converted to tp->tp_as_mapping == &MAPPING_NULL_STRUCT and a few will need to be left as tp->tp_as_mapping == NULL .
    Also, take care to include implicit null checks of the form !tp->tp_as_mapping and those tests inverses.

  3. sergey-miryanov commented on May 6, 2026

    @sergey-miryanov
    Contributor

    I'll take this on in a week or two, if no one minds.

  4. NekoAsakura commented on May 6, 2026

    @NekoAsakura
    Member

    I'll take this on in a week or two, if no one minds.

    Hi @sergey-miryanov, would you mind if I take this one? I was nearly done when #149317 was opened. :)

  5. sergey-miryanov commented on May 6, 2026

    @sergey-miryanov
    Contributor

    Yeah, go ahead!

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