From a9f4e2ccf0116789f6ed07b1f6f5b5ebf84f85ee Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Mon, 7 Jul 2025 14:58:16 +0100 Subject: [PATCH] [mypyc] Use per-type freelists for nested functions Often at most one instance is allocated at any given time, so this improves performance quite significantly in common cases. --- mypyc/irbuild/callable_class.py | 1 + mypyc/irbuild/env_class.py | 1 + 2 files changed, 2 insertions(+) diff --git a/mypyc/irbuild/callable_class.py b/mypyc/irbuild/callable_class.py index c7c3c7677cda7..bbd1b909afb65 100644 --- a/mypyc/irbuild/callable_class.py +++ b/mypyc/irbuild/callable_class.py @@ -56,6 +56,7 @@ class for the nested function. # environment to point at the previously defined environment # class. callable_class_ir = ClassIR(name, builder.module_name, is_generated=True, is_final_class=True) + callable_class_ir.reuse_freed_instance = True # The functools @wraps decorator attempts to call setattr on # nested functions, so we create a dict for these nested diff --git a/mypyc/irbuild/env_class.py b/mypyc/irbuild/env_class.py index 9e72f7efcf940..51c854a4a2b2d 100644 --- a/mypyc/irbuild/env_class.py +++ b/mypyc/irbuild/env_class.py @@ -48,6 +48,7 @@ class is generated, the function environment has not yet been is_generated=True, is_final_class=True, ) + env_class.reuse_freed_instance = True env_class.attributes[SELF_NAME] = RInstance(env_class) if builder.fn_info.is_nested: # If the function is nested, its environment class must contain an environment