Skip to content

ENH: use PyMem_RawMalloc instead of system allocator for better performance on free-threading#31503

Merged
ngoldbaum merged 4 commits into
numpy:mainfrom
kumaraditya303:malloc
May 28, 2026
Merged

ENH: use PyMem_RawMalloc instead of system allocator for better performance on free-threading#31503
ngoldbaum merged 4 commits into
numpy:mainfrom
kumaraditya303:malloc

Conversation

@kumaraditya303
Copy link
Copy Markdown
Contributor

@kumaraditya303 kumaraditya303 commented May 25, 2026

PR summary

Fixes #28454

This PR replaces usage of system allocator malloc with PyMem_RawMalloc and friends for better performance on free-threading where mimalloc is used as the underlying allocator.

AI Disclosure

I used Claude to find all the places where the replacement makes sense.

Copy link
Copy Markdown
Member

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good. I'm approving because this is mergeable as-is but I also have some small followup suggestions:

  • Can you add a release note fragment that we are using PyMem_RawMalloc for internal allocators that this will show up in tracemalloc statistics?
  • Claude points out that you missed the uses in _simd_convert.inc:
    void *ptr = malloc(size);

@charris are you OK with backporting this to the 2.5 branch with the release note? I don't think this is dangerous to backport and it'd be nice to include it with all the other free-threading scaling improvements for 2.5.

@ngoldbaum ngoldbaum added 09 - Backport-Candidate PRs tagged should be backported 39 - free-threading PRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703) 01 - Enhancement labels May 26, 2026
@ngoldbaum
Copy link
Copy Markdown
Member

I'm going to merge this to make sure it gets included in the 2.5.0rc1. Thanks @kumaraditya303!

@ngoldbaum ngoldbaum merged commit 0bfbca2 into numpy:main May 28, 2026
87 checks passed
@kumaraditya303 kumaraditya303 deleted the malloc branch May 28, 2026 19:48
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label May 28, 2026
charris pushed a commit that referenced this pull request May 28, 2026
charris added a commit that referenced this pull request May 28, 2026
ENH: use PyMem_RawMalloc instead of system allocator for better performance on free-threading (#31503)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

01 - Enhancement 39 - free-threading PRs and issues related to support for free-threading CPython (a.k.a. no-GIL, PEP 703)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MAINT,TASK: Avoid direct use of malloc

3 participants