Skip to content

add PyMemRawAllocator - #6279

Open
chirizxc wants to merge 38 commits into
PyO3:mainfrom
chirizxc:pymem-raw-alloc
Open

add PyMemRawAllocator#6279
chirizxc wants to merge 38 commits into
PyO3:mainfrom
chirizxc:pymem-raw-alloc

Conversation

@chirizxc

@chirizxc chirizxc commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Comment thread Cargo.toml Outdated
Comment thread src/pymem_alloc.rs Outdated
Comment on lines +24 to +26
// CPython documents this alignment as `ALIGNOF_MAX_ALIGN_T`
// (8 on Windows; autoconf-derived on Unix, not guaranteed >8 on every target/libc).
const MAX_ALIGN: usize = 8;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think this worth double-checking

@anuraaga's implementation included the following:

/// Alignment the raw allocator provides on its own (that of `max_align_t`).
const RAW_ALIGN: usize = if size_of::<usize>() >= 8 { 16 } else { 8 };

@chirizxc chirizxc Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

418a0a9bacce8fe2ccc5fabd1be1ef077dcff7df last commit for 3.15 branch
802a2c8b7a91d700c8bf342a27cd096ac444af41 last commit for 3.14 branch

3.15

Windows:

Unix

MacOS

Fallback

// Make sure we have maximum alignment, even if the current compiler
// does not support max_align_t. Note that:
// - Autoconf reports alignment of unknown types to 0.
// - 'long double' has maximum alignment on *most* platforms,
//   looks like the best we can do for pre-C11 compilers.
// - The value is tested, see test_alignof_max_align_t
#if !defined(ALIGNOF_MAX_ALIGN_T) || ALIGNOF_MAX_ALIGN_T == 0
#   undef ALIGNOF_MAX_ALIGN_T
#   define ALIGNOF_MAX_ALIGN_T _Alignof(long double)
#endif

3.14

Windows:

Unix

MacOS

Fallback

// Make sure we have maximum alignment, even if the current compiler
// does not support max_align_t. Note that:
// - Autoconf reports alignment of unknown types to 0.
// - 'long double' has maximum alignment on *most* platforms,
//   looks like the best we can do for pre-C11 compilers.
// - The value is tested, see test_alignof_max_align_t
#if !defined(ALIGNOF_MAX_ALIGN_T) || ALIGNOF_MAX_ALIGN_T == 0
#   undef ALIGNOF_MAX_ALIGN_T
#   define ALIGNOF_MAX_ALIGN_T _Alignof(long double)
#endif

@chirizxc
chirizxc marked this pull request as draft July 31, 2026 16:17
@mejrs

mejrs commented Jul 31, 2026

Copy link
Copy Markdown
Member

I don't think this needs to be gated behind a feature. Typically we only use features if we can gate some compile time cost behind it, like a dependency or proc macros.

@chirizxc
chirizxc marked this pull request as ready for review July 31, 2026 21:53
Comment on lines -4 to -19
extern_libpython! {
#[cfg_attr(PyPy, link_name = "PyPyMem_RawMalloc")]
pub fn PyMem_RawMalloc(size: size_t) -> *mut c_void;
#[cfg_attr(PyPy, link_name = "PyPyMem_RawCalloc")]
pub fn PyMem_RawCalloc(nelem: size_t, elsize: size_t) -> *mut c_void;
#[cfg_attr(PyPy, link_name = "PyPyMem_RawRealloc")]
pub fn PyMem_RawRealloc(ptr: *mut c_void, new_size: size_t) -> *mut c_void;
#[cfg_attr(PyPy, link_name = "PyPyMem_RawFree")]
pub fn PyMem_RawFree(ptr: *mut c_void);

// skipped _PyMem_GetCurrentAllocatorName
// skipped _PyMem_RawStrdup
// skipped _PyMem_Strdup
// skipped _PyMem_RawWcsdup
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment thread pyo3-ffi/src/pymem.rs
// skipped PyMem_Del
// skipped PyMem_DEL

#[cfg_attr(PyPy, link_name = "PyPyMem_RawMalloc")]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@chirizxc chirizxc closed this Aug 1, 2026
@chirizxc chirizxc reopened this Aug 1, 2026
@codspeed-hq

codspeed-hq Bot commented Aug 1, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 10.24%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 1 regressed benchmark
✅ 139 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation bench_fast 6.1 µs 6.8 µs -10.24%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing chirizxc:pymem-raw-alloc (5068b35) with main (92e47e1)

Open in CodSpeed

@chirizxc chirizxc closed this Aug 1, 2026
@chirizxc chirizxc reopened this Aug 1, 2026
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.

GlobalAlloc that uses PyMem_Raw* methods

2 participants