Smaller objects for the free-threading build using smaller integer types for refcount
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No file or test is named. Start with the free-threading build's definitions and uses of ob_ref_local and ob_ref_shared, then trace refcount overflow, atomic updates, saturation, and immortality behavior. Done means the smaller layout works without changing reference-counting correctness or the stated overflow behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, python
- Domain
- performance
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100