PyO3 / PyO3/pyo3

GlobalAlloc that uses PyMem_Raw* methods

Open
#6,268 21 comments 1 reaction 0 assignees View on GitHub

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.

https://github.com/bufbuild/protobuf-py/pull/46/changes#diff-d860a3c1b220287bc41b2a02c7830aab1867404d09509c63d4c7cf07bab2f7a5

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.