Skip to content

Commit 6600073

Browse files
authored
[mypyc] Use per-type free "lists" for nested functions (#19390)
Often at most one instance is allocated at any given time, so this improves performance quite significantly in common cases. Uses the freelist feature introduced in #19316. This speeds up self check by about 0.5% (measured 100 samples). Speeds up this microbenchmark that gets close to the maximal benefit by about 90%: ``` def f(x: int) -> int: def inc(y: int) -> int: return y + 1 return inc(x) def bench(n: int) -> None: for x in range(n): f(x) from time import time bench(1000) t0 = time() bench(50 * 1000 * 1000) print(time() - t0) ```
1 parent ada0d2a commit 6600073

File tree

2 files changed

+2
-0
lines changed

2 files changed

+2
-0
lines changed

mypyc/irbuild/callable_class.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class for the nested function.
5656
# environment to point at the previously defined environment
5757
# class.
5858
callable_class_ir = ClassIR(name, builder.module_name, is_generated=True, is_final_class=True)
59+
callable_class_ir.reuse_freed_instance = True
5960

6061
# The functools @wraps decorator attempts to call setattr on
6162
# nested functions, so we create a dict for these nested

mypyc/irbuild/env_class.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class is generated, the function environment has not yet been
4848
is_generated=True,
4949
is_final_class=True,
5050
)
51+
env_class.reuse_freed_instance = True
5152
env_class.attributes[SELF_NAME] = RInstance(env_class)
5253
if builder.fn_info.is_nested:
5354
# If the function is nested, its environment class must contain an environment

0 commit comments

Comments
 (0)