gh-144725: Relax over-strict alignment test on small arrays#145026
Open
jakelishman wants to merge 1 commit intopython:mainfrom
Open
gh-144725: Relax over-strict alignment test on small arrays#145026jakelishman wants to merge 1 commit intopython:mainfrom
jakelishman wants to merge 1 commit intopython:mainfrom
Conversation
This alignment test was introduced in pythongh-140557 with the intention of preventing empty-allocation optimisations from introducing odd-aligned pointers (as had previously been observed in such an optimisation for the empty `bytearray`, used in the Pickle v5 protocol). The test was over-specified, however, and compared to the maximum platform alignment, even though the requirements on allocators in general is only that the allocation is aligned for all data types that would in the requested allocation. This test could fail on 32-bit platforms using `mimalloc` for single-byte array elements (e.g. 'B'), as the tiny allocation region only enforced 4-byte alignment, not the 8-byte maximum platform alignment of the `double` type.
cmaloney
approved these changes
Feb 21, 2026
| align = struct.calcsize("N") | ||
| cases = [array.array(fmt) for fmt in ARRAY] | ||
| # Empty arrays | ||
| self.assertEqual( |
Contributor
There was a problem hiding this comment.
nit: I'd prefer subtests for each case rather than the array match so when get an error get exactly the case which it pairs with (esp. since ARRAY has some variance per platform in its setup)
| align = max(struct.calcsize(fmt) for fmt in ARRAY) | ||
| # gh-140557: pointer alignment of empty buffers should be at least | ||
| # to `size_t`. | ||
| align = struct.calcsize("N") |
Contributor
There was a problem hiding this comment.
Would there be any benefit to forcing larger alignment regardless?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This alignment test was introduced in gh-140557 with the intention of preventing empty-allocation optimisations from introducing odd-aligned pointers (as had previously been observed in such an optimisation for the empty
bytearray, used in the Pickle v5 protocol). The test was over-specified, however, and compared to the maximum platform alignment, even though the requirements on allocators in general is only that the allocation is aligned for all data types that would in the requested allocation.This test could fail on 32-bit platforms using
mimallocfor single-byte array elements (e.g. 'B'), as the tiny allocation region only enforced 4-byte alignment, not the 8-byte maximum platform alignment of thedoubletype.