Repository navigation
ENH: speed up the fixed-width string casts and unicode scalars - #32550
Merged
Merged
Conversation
Two per-element overheads on the legacy `S`/`U` dtypes: The `STRING/UNICODE/VOID_to_<T>` cast loops build a full array scalar per element and hand it to the destination's setitem. For S and U sources that is wasteful: the plain bytes/str returned by getitem behaves identically in every setitem and is much cheaper to create. Keep `PyArray_Scalar` for VOID sources (where it is what makes `void.astype(bool)` agree with `bool(void)`, numpygh-9847) and for VOID destinations (`VOID_setitem` reads the source through the buffer protocol, which `np.str_` supports but `str` does not). `PyArray_Scalar` itself, which also backs indexing and iteration, unconditionally allocated a scratch buffer and copied the element into it before handing it to `PyUnicode_FromKindAndData`. The copy is only needed to byte-swap, so skip it when the data is native-endian and suitably aligned, and build the argument tuple with `PyTuple_FromArray` instead of `Py_BuildValue`. Measured on 2e5 elements: `U -> int64` 2.9x faster, `U -> float64` 2.1x, `S -> int64` 1.4x, `list(U_arr)` 1.47x. Closes numpygh-11014 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
ngoldbaum
self-requested a review
September 9, 2026 17:48
Member
|
Nice this looks good to me! Can we get a performance release note fragment? |
Co-Authored-By: Claude Fable 5.1 <[email protected]>
antareepsarkar
pushed a commit
to antareepsarkar/numpy
that referenced
this pull request
Oct 5, 2026
…#32550) Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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.
PR summary
Fixes #11014 and improves string conversions in general:
STRING/UNICODE/VOID_to_<T>inarraytypes.c.srccallsPyArray_Scalarfor every element and hands the result to the destination'ssetitem. ForS/Usources the plainbytes/strfrom getitem behaves identically in everysetitemand is cheaperPyArray_Scalarcopies when it does not have to. If no swap is needed, we now skip the copy.Py_BuildValue, withPyTuple_FromArrayinstead ofPy_BuildValuenp.array(U_arr, dtype=int)U_arr.astype(np.float64)S_arr.astype(np.int64)list(U_arr)list(U_arr), byte-swappedU_arr.tolist()benchmark script
AI Disclosure
Generated with Claude Code
](#11232)