python / python/cpython

Deferred reference counts

Open
#120,024 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core performance type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

Proposal:

Approximately 80% of reference count operations occur in the interpreter. stats
The vast majority of these operations are needed only to maintain the correct reference counts for references in local variables and the evaluation stack.
We should not count these references, instead deferring them until we wish to perform incremental collection.

Doing so will give us a reasonable speedup on default builds, but the real value is for free-threading.
Free-threading requires that some references on the frame stack are deferred, but tagging those references is expensive. It is much more efficient to simply deferred all references on the frame stack.

This is not a new idea, in fact it is a very old one.

The implementation is conceptually fairly simple:

  • We don't count references in local variables and the evaluation stack
  • Any object that has a reference count of zero is, instead of being reclaimed, added to a "Zero count table"
  • When we perform collection, we update the reference count of all objects that have references on the stack, collect any objects with a zero reference count, and then reset the reference counts.

Like many "simple" ideas, the devil is in the detail.

There are two main concerns:

  • Reclamation of objects is not as prompt as before. We may use more resources, waiting for them to be reclaimed.
  • The overhead of updating references during collections may be as great or greater than the saving by deferring the reference counting.

We can keep reclamation acceptably prompt, by tracking the size of objects in the Zero Count Table, the size of objects allocated.
Once this number gets large enough, we perform a collection at the next opportunity.

We can keep the overhead of updating the reference counts low, by only deferring the top of the stack. Parts of the stack that are not accessed between collections, can be counted eagerly, reducing the amount of scanning needed to a few frames.

Previous discussion

https://github.com/faster-cpython/ideas/issues/677

Linked PRs
  • gh-121917
  • gh-121923
  • gh-122693
  • gh-122730
  • gh-122734
  • gh-122780
  • gh-122968
  • gh-125095
  • gh-125192
### Tasks
- [ ] https://github.com/python/cpython/issues/123391
- [x] Interpreter code generators need to be able to flush the stack around escaping calls.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the linked PRs and the previous discussion in faster-cpython/ideas#677, then inspect the remaining task in issue #123391. The proposal covers deferred reference counting, zero-count tracking, and collection timing; completion would require resolving the listed implementation concerns and completing the outstanding task.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
15/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.