Skip to content

Defer functions defined in nested classes in free-threaded builds #127274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mpage opened this issue Nov 25, 2024 · 1 comment
Closed

Defer functions defined in nested classes in free-threaded builds #127274

mpage opened this issue Nov 25, 2024 · 1 comment
Labels
topic-free-threading type-feature A feature request or enhancement

Comments

@mpage
Copy link
Contributor

mpage commented Nov 25, 2024

Feature or enhancement

Proposal:

We currently only defer functions that do not have the CO_NESTED flag set:

if ((code_obj->co_flags & CO_NESTED) == 0) {
// Use deferred reference counting for top-level functions, but not
// nested functions because they are more likely to capture variables,
// which makes prompt deallocation more important.
_PyObject_SetDeferredRefcount((PyObject *)op);
}

This also excludes functions defined on nested classes. In the example below, the Foo.__init__ function will not use deferred reference counting because the __init__ method's code object has the CO_NESTED flag set.

def func():
    class Foo:
        def __init__(self):
            pass

We would like to relax the restriction on CO_NESTED to allow functions that are defined on nested classes to use deferred reference counting.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

@mpage mpage added type-feature A feature request or enhancement topic-free-threading labels Nov 25, 2024
mpage added a commit to mpage/cpython that referenced this issue Dec 18, 2024
mpage added a commit to mpage/cpython that referenced this issue Dec 19, 2024
mpage added a commit that referenced this issue Dec 19, 2024
Methods (functions defined in class scope) are likely to be cleaned
up by the GC anyway.

Add a new code flag, `CO_METHOD`, that is set for functions defined
in a class scope. Use that when deciding to defer functions.
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Dec 23, 2024
Methods (functions defined in class scope) are likely to be cleaned
up by the GC anyway.

Add a new code flag, `CO_METHOD`, that is set for functions defined
in a class scope. Use that when deciding to defer functions.
@erlend-aasland
Copy link
Contributor

I suspect this can be closed, @mpage.

@mpage mpage closed this as completed Jan 5, 2025
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Jan 8, 2025
Methods (functions defined in class scope) are likely to be cleaned
up by the GC anyway.

Add a new code flag, `CO_METHOD`, that is set for functions defined
in a class scope. Use that when deciding to defer functions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-free-threading type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants