GlobalAlloc that uses PyMem_Raw* methods
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.2k
- Forks
- 1k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 66
Description
It could be convenient if there is a utility module that provides a rust GlobalAlloc backed by PyMem_Raw - this is particularly important on free-threaded Python where Rust would still use the system allocator which usually performs much worse with contention (article provides some context). While the extension can compile in mimalloc, that's a pretty large library for what's already in the interpreter, multiplied by all extensions. And regardless of that, users can get tracemalloc integration for free.
The idea is to basically just have this alloc.rs available in PyO3 somewhere. I don't think it should be registered by default, notably it is questionable for libraries that allocate after detaching, normally it will be fine but tracemalloc would itself cause acquiring GIL on arbitrary possibly async Rust threads. But it's probably still applicable relatively broadly and some trickiesh code that may be helpful.
I notice https://github.com/PyO3/pyo3/issues/81 brought the idea up a long time ago, back then free-threaded may not have been well known as a motivation for this, so maybe it can be reconsidered.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the linked alloc.rs implementation and the PyMem_Raw* allocator APIs, then read the discussion around issue #81 and this issue's free-threaded concerns. Done means deciding whether PyO3 should provide a reusable Rust GlobalAlloc backed by PyMem_Raw, where it belongs, and how its lifetime, tracing, and detached-thread behavior should be documented and tested.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, rust
- Domain
- backend-api-design, performance
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100