Skip to content

gh-162507: Speed up floating and complex array - #162509

Draft
maurycy wants to merge 3 commits into
python:mainfrom
maurycy:array-direct-conversion
Draft

maurycy wants to merge 3 commits into
python:mainfrom
maurycy:array-direct-conversion

Conversation

@maurycy

@maurycy maurycy commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Call PyFloat_AsDouble() directly in the e, f and d setters and PyComplex_AsCComplex() in the Zf and Zd setters, so we skip a PyArg_Parse() call for each item during array construction and assignment.

macOS PGO+LTO:

Operation Baseline Patched Baseline / patched
array('e', floats) 7.36 ± 0.70 µs 3.63 ± 0.04 µs 2.03x
array('f', floats) 7.36 ± 0.10 µs 3.87 ± 0.11 µs 1.90x
array('d', ints) 7.24 ± 0.21 µs 4.42 ± 0.20 µs 1.64x
array('d', floats) 6.13 ± 0.05 µs 3.32 ± 0.01 µs 1.84x
array('Zf', complexes) 9.18 ± 0.17 µs 6.30 ± 0.03 µs 1.46x
array('Zd', complexes) 6.94 ± 0.11 µs 4.11 ± 0.10 µs 1.69x
array('i', ints) (control) 6.71 ± 0.08 µs 6.72 ± 0.18 µs 1.00x
a[0] = value 15.41 ± 0.42 ns 11.60 ± 0.23 ns 1.33x
scimark_fft 114.72 ± 2.18 ms 103.74 ± 0.40 ms 1.11x
bench
from array import array

import pyperf


ints = list(range(1, 1001))
floats = list(map(float, ints))
complexes = [complex(x, -x) for x in ints]
a = array("d", [0])
value = 3.5

runner = pyperf.Runner()
for stmt in (
    "array('e', floats)",
    "array('f', floats)",
    "array('d', ints)",
    "array('d', floats)",
    "array('Zf', complexes)",
    "array('Zd', complexes)",
    "array('i', ints)",
    "a[0] = value",
):
    runner.timeit(stmt, stmt, globals=globals())

ref #162478

Closes #162507

@skirpichev
skirpichev self-requested a review November 23, 2026 22:51
Comment thread Modules/arraymodule.c

if (!PyArg_Parse(v, "D;array item must be complex", &x)) {
Py_complex x = PyComplex_AsCComplex(v);
if (PyErr_Occurred()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe

Suggested change
if (PyErr_Occurred()) {
if (x.real == -1.0 && PyErr_Occurred()) {

@skirpichev skirpichev Nov 23, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I suspect this was copied from Python/getargs.c. We also check errors this way in the AC.

Not sure if there is a measurable difference, though. PyErr_Occurred() is cheap.

Edit:
Probably it's not worth for AC, but for array you can see measurable difference (default configure options, gcc 14.2):

Benchmark ref patch
array('Zd', <10>) 1.91 us 1.63 us: 1.17x faster
array('Zd', <100>) 9.32 us 7.65 us: 1.22x faster
Geometric mean (ref) 1.19x faster
Details
from array import array
import pyperf
runner = pyperf.Runner()
for n in [10, 100]:
    xs = [i + 1j for i in range(n)]
    s = f"array('Zd', <{n}>)"
    tp = "Zd"
    runner.bench_func(s, array, tp, xs)
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index a484dba703c..5d1deb1e4e7 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -691,7 +691,7 @@ cd_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
 {
     Py_complex x = PyComplex_AsCComplex(v);
     double f[2];
-    if (PyErr_Occurred()) {
+    if (x.real == -1.0 && PyErr_Occurred()) {
         return -1;
     }
 

Comment thread Modules/arraymodule.c
double x;
if (!PyArg_Parse(v, "d;array item must be float", &x)) {
double x = PyFloat_AsDouble(v);
if (x == -1.0 && PyErr_Occurred()) {

@skirpichev skirpichev Nov 23, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

PEP7 nit:

Suggested change
if (x == -1.0 && PyErr_Occurred()) {
if (x == -1.0 && PyErr_Occurred()) {

(ditto other cases)

@skirpichev

Copy link
Copy Markdown
Member

In principle, same optimization will work for integer typecodes as well. Sometimes, you will have to check for overflows after PyLong_AsLong*() calls, but not for 'l'/'L' cases.

@maurycy
maurycy marked this pull request as draft November 24, 2026 11:44
@maurycy

maurycy commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

@eendebakpt @skirpichev Thank you for your careful reviews! I've moved this back to the draft. I'm travelling but I think about already extending this PR to the other setters. My initial idea was to do it as a set of narrow PRs, but I think there are regressions that go beyond noise:

Benchmark main current pr version after review all setters
array('e', floats) 6.96 µs 3.61 µs 3.64 µs 3.60 µs
array('f', floats) 7.33 µs 3.84 µs 3.85 µs 3.84 µs
array('d', ints) 7.30 µs 4.32 µs 4.34 µs 4.32 µs
array('d', floats) 6.33 µs 3.32 µs 3.76 µs 3.33 µs
array('Zf', complexes) 9.21 µs 6.99 µs 5.33 µs 5.37 µs
array('Zd', complexes) 6.83 µs 4.09 µs 3.14 µs 3.16 µs
array('i', ints) 6.80 µs 7.38 µs 7.43 µs 3.81 µs
a[0] = 3.5 (double array) 15.14 ns 11.50 ns 12.15 ns 11.48 ns
scimark_fft 117.81 ms 106.37 ms 104.95 ms 106.44 ms
Benchmark main current pr version after review all setters
array('b', small_ints) 7.03 µs 7.78 µs 7.79 µs 4.08 µs
array('B', small_ints) 6.79 µs 7.29 µs 7.34 µs 3.80 µs
array('h', ints) 6.77 µs 7.58 µs 7.64 µs 3.80 µs
array('H', ints) 7.04 µs 7.64 µs 7.62 µs 4.05 µs
array('I', ints) 3.59 µs 3.59 µs 3.60 µs 3.59 µs
array('l', ints) 6.76 µs 7.30 µs 7.30 µs 3.56 µs
array('L', ints) 3.48 µs 3.48 µs 3.48 µs 3.48 µs
array('q', ints) 6.54 µs 7.32 µs 8.25 µs 3.37 µs
array('Q', ints) 3.49 µs 3.49 µs 3.51 µs 3.49 µs
array('w', chars) 2.71 µs 2.72 µs 2.73 µs 2.73 µs
array('i', ints), 50% random -1 9.58 µs 11.36 µs 11.33 µs 6.20 µs
array('Zf', complexes), 50% random real == -1 9.12 µs 6.99 µs 8.72 µs 8.86 µs
array('Zd', complexes), 50% random real == -1 6.83 µs 4.08 µs 5.77 µs 5.75 µs
a[0] = 123 (int array) 15.12 ns 15.87 ns 16.03 ns 11.61 ns
a[0] = 3.5+1j (complex double array) 15.37 ns 12.36 ns 11.50 ns 11.47 ns

That's PGO+LTO macOS, Apple Clang 21.0.0.

If true, the regression is likely caused by the fact that the PR reduces the number of PyArg_Parse() calls during PGO training, so (I suspect) it's not inlined. I want to get a clearer picture (also on Linux) before marking this as ready for review again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid parsing a format string for each float or complex array item

3 participants