Skip to content

Smaller objects for the free-threading build using smaller integer types for refcount #153202

Description

@markshannon

The literature on reference counting for JVM and other GCs show that very few reference counts get to more than 7. This matches up with our stats that should the ratio of refcount ops to object allocations is about 6/7 to 1.

With that in mind, it would make sense to make the C ints used to represent refcounts smaller.
Instead of:

    uint32_t ob_ref_local;      // local reference count
    Py_ssize_t ob_ref_shared;   // shared (atomic) reference count

we can use:

    uint8_t ob_ref_local;      // local reference count
    uint32_t ob_ref_shared;   // shared (atomic) reference count

using 5 bytes instead of 12.

If ob_ref_local would overflow, we can atomically move some of that count into ob_ref_shared.

if (++op->ob_ref_local == 0) {
    atomic_add(&op->ob_ref_shared, 128);
    op->ob_ref_local = 128;
}

ob_ref_shared can use the same approach to saturation and immortality that the default build currently does.

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.16new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetopic-free-threading

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions