Skip to content
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

[mypyc] Avoid cyclic reference in nested functions #16268

Merged
merged 6 commits into from
Oct 19, 2023
Merged

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Oct 15, 2023

Mypyc used to always put nested functions into the environment object, which results
in cyclic references, since the function object contains a reference to the environment.

Now we only do this if the body of a nested function refers to a nested function (e.g.
due to a recursive call). This means that in the majority of cases we can avoid the
cyclic reference.

This speeds up self check by an impressive 7%. I'm not sure exactly why the impact
is so big, but spending less time in the cyclic garbage collector is probably a big part.

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Just one suggestion for a test.

return any(
has_nested_func_self_reference(builder, nested)
for nested in builder.encapsulating_funcs.get(fitem, [])
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it may be worth testing mutually recursive functions, something involving:

def f():
    def g():
        h()
    def h():
        g()
    return g, h

or are we already testing this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we already have at least the test case testNestedFunctions2 in mypyc/test-data/run-functions.test (mutual_recursion).

@JukkaL JukkaL merged commit e1f6d6b into master Oct 19, 2023
13 checks passed
@JukkaL JukkaL deleted the mypyc-nested-fn branch October 19, 2023 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants