python / python/cpython

Proposal to add gc_list_for_each macro

Open
#133,058 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Feature or enhancement

Proposal:

In the implementation of the reference cycle garbage collector, gc.c file, I have noticed that we use the following for-loop pattern over and over to traverse a PyGC_Head list:

for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) {
  /* ... */
}

However, in pycore_llist.h we avoided that by using llist_for_each macro as:

// Iterate over a list.
#define llist_for_each(node, head) \
    for (node = (head)->next; node != (head); node = node->next)

I propose adding a new macro as gc_list_for_each to the gc_list_xxx family to serve the same purpose:

#define gc_list_for_each(gc, list) \
    for (gc = GC_NEXT((list)); gc != (list); gc = GC_NEXT(gc))
Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

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 repeated PyGC_Head traversal patterns in gc.c and the existing llist_for_each macro in pycore_llist.h. Add the proposed gc_list_for_each macro to the gc_list_xxx family, then verify that the reference-cycle garbage collector still builds and its existing behavior is unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
backend
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.