Skip to content

gh-144725: Relax over-strict alignment test on arrays - #157530

Open
cmaloney wants to merge 1 commit into
python:mainfrom
cmaloney:relax_array_alignment
Open

cmaloney wants to merge 1 commit into
python:mainfrom
cmaloney:relax_array_alignment

Conversation

@cmaloney

@cmaloney cmaloney commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

The alignment test introduced in gh-140557 guards against empty-allocation optimisations handing out oddly-aligned pointers, as had been observed for the empty bytearray used by pickle protocol 5. It compared every array buffer against the maximum alignment of any array element type, which over-specifies the requirement: an allocation only needs to be aligned for the type stored in it. This failed on 32-bit platforms using mimalloc for single-byte element types such as 'B', where the tiny allocation region only guarantees 4-byte alignment rather than the 8-byte alignment of double.

Check instead that each array's buffer, empty or allocated, is aligned to at least the alignment of its element type. The alignment is derived from struct rather than from itemsize, since the two differ for types such as Zf and Zd.


I took @jakelishman's PR and added the fixes from review by @colesbury in GH-145026

The alignment test introduced in pythongh-140557 guards against empty-allocation
optimisations handing out oddly-aligned pointers, as had been observed for
the empty `bytearray` used by pickle protocol 5. It compared every array
buffer against the maximum alignment of any array element type, which
over-specifies the requirement: an allocation only needs to be aligned for
the type stored in it. This failed on 32-bit platforms using mimalloc for
single-byte element types such as 'B', where the tiny allocation region
only guarantees 4-byte alignment rather than the 8-byte alignment of
`double`.

Check instead that each array's buffer, empty or allocated, is aligned to
at least the alignment of its element type. The alignment is derived from
`struct` rather than from `itemsize`, since the two differ for types such
as `Zf` and `Zd`.

Co-authored-by: Jake Lishman <jake@binhbar.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review skip news tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant