Skip to content

[mypyc] Avoid polluting generator environment layouts - #21990

Merged
JukkaL merged 1 commit into
masterfrom
mypyc-generator-env-layout-fix
Sep 15, 2026
Merged

JukkaL merged 1 commit into
masterfrom
mypyc-generator-env-layout-fix

Conversation

@JukkaL

@JukkaL JukkaL commented Sep 15, 2026

Copy link
Copy Markdown
Collaborator

When setting up a non-merged generator, we passed the generator helper symbol table to load_outer_env. This made the closure environment class inherit the helper arguments and generator state as attributes, adding six unused object slots to every affected environment.

Load the outer environment directly from the generator frame during setup, and make load_outer_env copy only attributes that are actually owned by the environment being loaded. Add a regression test that verifies helper-only attributes stay out of the environment and that its self-reference keeps the correct type.

This made a few microbenchmarks that allocate lots of generators noticeably (though not massively) faster.

When setting up a non-merged generator, we passed the generator helper
symbol table to load_outer_env. This made the closure environment class
inherit the helper arguments and generator state as attributes, adding six
unused object slots to every affected environment. It could also replace the
declared type of the environment self-reference with the generator frame
type.

Load the outer environment directly from the generator frame during setup,
and make load_outer_env copy only attributes that are actually owned by the
environment being loaded. Add a regression test that verifies helper-only
attributes stay out of the environment and that its self-reference keeps the
correct type.
@JukkaL
JukkaL merged commit ad0dff5 into master Sep 15, 2026
18 checks passed
@JukkaL
JukkaL deleted the mypyc-generator-env-layout-fix branch September 15, 2026 13:21
@JukkaL

JukkaL commented Sep 15, 2026

Copy link
Copy Markdown
Collaborator Author

BTW, from now on I won't mention if I used coding agent assist explicitly, unless perhaps a PR was 100% created using a coding agent, with minimal direct involvement from me. These days I'm using coding agents in most PRs (often multiple agents/models).

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