Repository navigation
Commit 58b94d6
PERF: avoid array scalars in the string-like casts
The legacy `STRING/UNICODE/VOID_to_<T>` cast loops build a full array
scalar for every element and hand it to the destination's setitem. For
S and U sources that scalar is needlessly expensive: `PyArray_Scalar` for
NPY_UNICODE allocates a scratch buffer, builds a `str`, wraps it in an
argument tuple and then runs `str.__new__` on the `np.str_` subtype,
which copies the data a second time. The plain bytes/str returned by
getitem behaves identically in every setitem and is much cheaper.
Use getitem for S and U sources, keeping `PyArray_Scalar` for VOID
sources (where `void.item()` and `bool(void)` disagree, numpygh-9847, the
reason the scalar was introduced in numpygh-9856) and for VOID destinations
(`VOID_setitem` reads the source through the buffer protocol, which
`np.str_` supports but `str` does not).
Measured on 2e5 elements: `U -> int64` 3.1x faster, `U -> float64` 2.5x,
`U -> bool` 6.5x, `U -> S` 3.8x, `S -> int64` 1.4x, `S -> float64` 1.35x.
The reproducer in numpygh-11014 is resolved: `np.array(string_array, dtype=int)`
goes from 2.2x slower than `np.array(string_array.tolist(), dtype=int)` to
slightly faster.
Closes numpygh-11014
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>1 parent 3274e70 commit 58b94d6
1 file changed
Lines changed: 16 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1515 | 1515 | | |
1516 | 1516 | | |
1517 | 1517 | | |
| 1518 | + | |
| 1519 | + | |
| 1520 | + | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
| 1526 | + | |
| 1527 | + | |
| 1528 | + | |
| 1529 | + | |
1518 | 1530 | | |
1519 | 1531 | | |
1520 | 1532 | | |
| |||
1537 | 1549 | | |
1538 | 1550 | | |
1539 | 1551 | | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
1540 | 1555 | | |
1541 | 1556 | | |
1542 | 1557 | | |
| |||
1552 | 1567 | | |
1553 | 1568 | | |
1554 | 1569 | | |
1555 | | - | |
| 1570 | + | |
1556 | 1571 | | |
1557 | 1572 | | |
1558 | 1573 | | |
| |||
0 commit comments