-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-144995: Optimize memoryview == memoryview #144996
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
Changes from 1 commit
2294542
0dd7911
61e37e4
102f26d
2316fab
800e85f
a754be4
1d72ed2
134f0de
3272520
83ad213
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 @@ | ||
| Optimize :class:`memoryview` comparison: a :class:`memoryview` is equal to | ||
| itself, there is no need to compare values. Patch by Victor Stinner. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3101,6 +3101,25 @@ cmp_rec(const char *p, const char *q, | |
| return 1; | ||
| } | ||
|
|
||
| static int | ||
| is_float_format(const char *format) | ||
|
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. Does this cover the complex types?
Member
Author
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. This memory format is |
||
| { | ||
| if (format == NULL) { | ||
| return 0; | ||
| } | ||
| if (strcmp("d", format) == 0) { | ||
| return 1; | ||
| } | ||
| if (strcmp("f", format) == 0) { | ||
| return 1; | ||
| } | ||
| if (strcmp("e", format) == 0) { | ||
| return 1; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
|
|
||
| static PyObject * | ||
| memory_richcompare(PyObject *v, PyObject *w, int op) | ||
| { | ||
|
|
@@ -3122,6 +3141,14 @@ memory_richcompare(PyObject *v, PyObject *w, int op) | |
| } | ||
| vv = VIEW_ADDR(v); | ||
|
|
||
| // A memoryview is equal to itself: there is no need to compare individual | ||
| // values. This is not true for float values since they can be NaN, and NaN | ||
| // is not equal to itself. | ||
| if (v == w && !is_float_format(vv->format)) { | ||
| equal = 1; | ||
| goto result; | ||
| } | ||
|
|
||
| if (PyMemoryView_Check(w)) { | ||
| if (BASE_INACCESSIBLE(w)) { | ||
| equal = (v == w); | ||
|
|
||
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.
Can "?" be tested? Can format starting with "@" be tested? Can the null format be tested?
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.
I don't know how to test these formats.
array.arraydoesn't support "P" and "?" formats and it doesn't support "@" byte order. Do you have an idea how to test these cases?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.
memoryview.cast()supports them.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.
Surprisingly:
even if
But this may be platform depending, so I would not test values different than 0 and 1. Or 1 is also not safe?
It may be undefined behavior to interpret random values except 0 as
void*(even if it works on x86). Maybe there is a way to create an array of pointers inctypes? Or it is not worth to bother?