-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
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
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 |
|---|---|---|
|
|
@@ -575,6 +575,27 @@ def test_array_assign(self): | |
| m[:] = new_a | ||
| self.assertEqual(a, new_a) | ||
|
|
||
| def test_compare_equal(self): | ||
| # 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. | ||
| for int_format in 'bBhHiIlLqQ': | ||
| with self.subTest(format=int_format): | ||
| a = array.array(int_format, [1, 2, 3]) | ||
| m = memoryview(a) | ||
| self.assertTrue(m == m) | ||
|
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. Test also |
||
|
|
||
| for float_format in 'fd': | ||
| with self.subTest(format=int_format): | ||
| a = array.array(float_format, [1.0, 2.0, float('nan')]) | ||
| m = memoryview(a) | ||
| # nan is not equal to nan | ||
| self.assertFalse(m == m) | ||
|
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. Test also |
||
|
|
||
| a = array.array(float_format, [1.0, 2.0, 3.0]) | ||
| m = memoryview(a) | ||
| self.assertTrue(m == m) | ||
|
|
||
|
|
||
| class BytesMemorySliceTest(unittest.TestCase, | ||
| BaseMemorySliceTests, BaseBytesMemoryTests): | ||
|
|
||
| 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 |
|---|---|---|
|
|
@@ -3122,6 +3122,29 @@ memory_richcompare(PyObject *v, PyObject *w, int op) | |
| } | ||
| vv = VIEW_ADDR(v); | ||
|
|
||
| // For formats supported by the struct module 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. So only use this optimization on format known to | ||
| // not use floats. | ||
| if (v == w) { | ||
| int can_compare_ptr; | ||
| const char *format = vv->format; | ||
| if (format != NULL) { | ||
| // Include only formats known by struct, exclude formats "d" (double), | ||
| // "f" (float), "e" (16-bit float) and "P" (void*) | ||
| can_compare_ptr = (strchr("bBchHiIlLnNqQ?", format[0]) != NULL | ||
|
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. Note that format can starts with "@". |
||
| && format[1] == 0); | ||
| } | ||
| else { | ||
| can_compare_ptr = 1; | ||
| } | ||
| if (can_compare_ptr) { | ||
| 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?