Repository navigation
Conversation
|
|
||
| if (!PyArg_Parse(v, "D;array item must be complex", &x)) { | ||
| Py_complex x = PyComplex_AsCComplex(v); | ||
| if (PyErr_Occurred()) { |
There was a problem hiding this comment.
Maybe
| if (PyErr_Occurred()) { | |
| if (x.real == -1.0 && PyErr_Occurred()) { |
There was a problem hiding this comment.
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;
}
| double x; | ||
| if (!PyArg_Parse(v, "d;array item must be float", &x)) { | ||
| double x = PyFloat_AsDouble(v); | ||
| if (x == -1.0 && PyErr_Occurred()) { |
There was a problem hiding this comment.
PEP7 nit:
| if (x == -1.0 && PyErr_Occurred()) { | |
| if (x == -1.0 && PyErr_Occurred()) { |
(ditto other cases)
|
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. |
|
@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:
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 |
Call
PyFloat_AsDouble()directly in thee,fanddsetters andPyComplex_AsCComplex()in theZfandZdsetters, so we skip aPyArg_Parse()call for each item during array construction and assignment.macOS PGO+LTO:
array('e', floats)array('f', floats)array('d', ints)array('d', floats)array('Zf', complexes)array('Zd', complexes)array('i', ints)(control)a[0] = valuescimark_fftbench
ref #162478
Closes #162507
arrayitem #162507