Skip to content

Conversation

@JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Sep 2, 2025

It's quite common to have a class where we almost always have at most a single allocated instance (per thread). Now these instances can be allocated more quickly, by reusing the memory that was used for the most recently freed instance (a separate memory block is reused for each thread on free-threaded builds).

It's used like this (only the value 1 is supported for now):

from mypy_extensions import mypyc_attr

@mypyc_attr(free_list=1)
class Foo:
    ...

This makes a microbenchmark that only allocates and immediately frees simple objects repeatedly around 3.8x faster.

It's probably worth extending this to support larger free lists in the future.

We can later look into enabling this automatically for certain native classes based on profile information.

JukkaL and others added 2 commits September 2, 2025 10:55
It's quite common to have a class where we almost always have at most
a single allocated instance (per thread). Now these instances can be
allocated more quickly, by reusing the memory that was used for the
most recently freed instance (a separate memory block is reused for
each thread on free-threaded builds).

It's used like this (only the value 1 is supported for now):
```
from mypy_extensions import mypyc_attr

@mypyc_attr(free_list=1)
class Foo:
    ...
```

This makes a microbenchmark that only allocates and immediately
frees simple objects repeatedly around 3.8x faster.

It's probably worth extending this to support larger free lists in the
future.

We can later look into enabling this automatically for certain native
classes based on profile information.
@JukkaL JukkaL merged commit e35e05f into master Sep 2, 2025
13 checks passed
@JukkaL JukkaL deleted the mypyc-free-list-attr branch September 2, 2025 12:53
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.

3 participants