Repository navigation
gh-162507: Speed up floating and complex array
#162509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Speed up writes to floating-point and complex :class:`array.array` objects. | ||
| Patch by Maurycy Pawłowski-Wieroński. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -584,8 +584,8 @@ e_getitem(arrayobject *ap, Py_ssize_t i) | |||||||||||||||||
| static int | ||||||||||||||||||
| e_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | ||||||||||||||||||
| { | ||||||||||||||||||
| double x; | ||||||||||||||||||
| if (!PyArg_Parse(v, "d;array item must be float", &x)) { | ||||||||||||||||||
| double x = PyFloat_AsDouble(v); | ||||||||||||||||||
| if (x == -1.0 && PyErr_Occurred()) { | ||||||||||||||||||
| return -1; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -607,9 +607,10 @@ f_getitem(arrayobject *ap, Py_ssize_t i) | |||||||||||||||||
| static int | ||||||||||||||||||
| f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | ||||||||||||||||||
| { | ||||||||||||||||||
| double x; | ||||||||||||||||||
| if (!PyArg_Parse(v, "d;array item must be float", &x)) | ||||||||||||||||||
| double x = PyFloat_AsDouble(v); | ||||||||||||||||||
| if (x == -1.0 && PyErr_Occurred()) { | ||||||||||||||||||
| return -1; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| CHECK_ARRAY_BOUNDS(ap, i); | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -629,9 +630,10 @@ d_getitem(arrayobject *ap, Py_ssize_t i) | |||||||||||||||||
| static int | ||||||||||||||||||
| d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | ||||||||||||||||||
| { | ||||||||||||||||||
| double x; | ||||||||||||||||||
| if (!PyArg_Parse(v, "d;array item must be float", &x)) | ||||||||||||||||||
| double x = PyFloat_AsDouble(v); | ||||||||||||||||||
| if (x == -1.0 && PyErr_Occurred()) { | ||||||||||||||||||
| return -1; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| CHECK_ARRAY_BOUNDS(ap, i); | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -652,9 +654,8 @@ cf_getitem(arrayobject *ap, Py_ssize_t i) | |||||||||||||||||
| static int | ||||||||||||||||||
| cf_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | ||||||||||||||||||
| { | ||||||||||||||||||
| Py_complex x; | ||||||||||||||||||
|
|
||||||||||||||||||
| if (!PyArg_Parse(v, "D;array item must be complex", &x)) { | ||||||||||||||||||
| Py_complex x = PyComplex_AsCComplex(v); | ||||||||||||||||||
| if (PyErr_Occurred()) { | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Detailsfrom 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;
}
|
||||||||||||||||||
| return -1; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -688,10 +689,9 @@ cd_getitem(arrayobject *ap, Py_ssize_t i) | |||||||||||||||||
| static int | ||||||||||||||||||
| cd_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | ||||||||||||||||||
| { | ||||||||||||||||||
| Py_complex x; | ||||||||||||||||||
| Py_complex x = PyComplex_AsCComplex(v); | ||||||||||||||||||
| double f[2]; | ||||||||||||||||||
|
|
||||||||||||||||||
| if (!PyArg_Parse(v, "D;array item must be complex", &x)) { | ||||||||||||||||||
| if (PyErr_Occurred()) { | ||||||||||||||||||
| return -1; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PEP7 nit:
(ditto other cases)