From b42914cdad71a067e2572df430b8435d9ee2d38f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Tue, 24 Nov 2026 00:01:02 +0100 Subject: [PATCH 1/6] memcmp --- Objects/memoryobject.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c index 11a0a7ab04f08bc..02ef32fc6ed27ac 100644 --- a/Objects/memoryobject.c +++ b/Objects/memoryobject.c @@ -3444,6 +3444,11 @@ cmp_base(const char *p, const char *q, const Py_ssize_t *shape, Py_ssize_t i; int equal; + if ((*fmt == 'B' || *fmt == 'b' || *fmt == 'c') && + pstrides[0] == 1 && qstrides[0] == 1 && + psuboffsets == NULL && qsuboffsets == NULL && shape[0] > 0) { + return memcmp(p, q, shape[0]) == 0; + } for (i = 0; i < shape[0]; p+=pstrides[0], q+=qstrides[0], i++) { const char *xp = ADJUST_PTR(p, psuboffsets, 0); const char *xq = ADJUST_PTR(q, qsuboffsets, 0); From da0b5a9306cfa46c7621c284ebc4f717e666e52e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Tue, 24 Nov 2026 00:01:09 +0100 Subject: [PATCH 2/6] test --- Lib/test/test_memoryview.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Lib/test/test_memoryview.py b/Lib/test/test_memoryview.py index 283f0077d7ae932..43cb90462c85343 100644 --- a/Lib/test/test_memoryview.py +++ b/Lib/test/test_memoryview.py @@ -561,6 +561,15 @@ def _check_contents(self, tp, obj, contents): class BytesMemoryviewTest(unittest.TestCase, BaseMemoryviewTests, BaseBytesMemoryTests): + def test_compare_byte_views(self): + for fmt in ('B', 'b', 'c'): + with self.subTest(format=fmt): + packed = memoryview(b'ab').cast(fmt) + spaced = memoryview(b'axb').cast(fmt)[::2] + self.assertEqual(packed, spaced) + self.assertEqual(spaced, packed) + self.assertNotEqual(packed, memoryview(b'ax').cast(fmt)) + def test_comparison_release_during_buffer_callback(self): # Releasing the view from inside the other operand's __buffer__ # succeeds, but the buffer stays pinned until the comparison ends: From bb5e152fd272dc109aa5e24d129206c7845e3ce1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Tue, 24 Nov 2026 00:25:27 +0100 Subject: [PATCH 3/6] news --- .../2026-11-24-00-25-22.gh-issue-162513.6-uUKS.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-24-00-25-22.gh-issue-162513.6-uUKS.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-24-00-25-22.gh-issue-162513.6-uUKS.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-24-00-25-22.gh-issue-162513.6-uUKS.rst new file mode 100644 index 000000000000000..3996f4923461a8e --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-24-00-25-22.gh-issue-162513.6-uUKS.rst @@ -0,0 +1,2 @@ +Speed up :class:`memoryview` equality comparisons for contiguous buffers +with matching byte formats. From 1ac254522dfcca989f17dfce005c9bc95e603a6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Tue, 24 Nov 2026 00:27:35 +0100 Subject: [PATCH 4/6] claim credit --- .../2026-11-24-00-25-22.gh-issue-162513.6-uUKS.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-24-00-25-22.gh-issue-162513.6-uUKS.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-24-00-25-22.gh-issue-162513.6-uUKS.rst index 3996f4923461a8e..70bcba5be991147 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-24-00-25-22.gh-issue-162513.6-uUKS.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-24-00-25-22.gh-issue-162513.6-uUKS.rst @@ -1,2 +1,2 @@ -Speed up :class:`memoryview` equality comparisons for contiguous buffers -with matching byte formats. +Speed up :class:`memoryview` equality comparisons for contiguous buffers with +matching byte formats. Patch by Maurycy Pawłowski-Wieroński. From 5b2b544beb4e0c90ef1423310a243e2f28181d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Tue, 24 Nov 2026 00:32:13 +0100 Subject: [PATCH 5/6] pep 7 --- Objects/memoryobject.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c index 02ef32fc6ed27ac..e074e6cf72bd7d7 100644 --- a/Objects/memoryobject.c +++ b/Objects/memoryobject.c @@ -3446,9 +3446,11 @@ cmp_base(const char *p, const char *q, const Py_ssize_t *shape, if ((*fmt == 'B' || *fmt == 'b' || *fmt == 'c') && pstrides[0] == 1 && qstrides[0] == 1 && - psuboffsets == NULL && qsuboffsets == NULL && shape[0] > 0) { + psuboffsets == NULL && qsuboffsets == NULL && shape[0] > 0) + { return memcmp(p, q, shape[0]) == 0; } + for (i = 0; i < shape[0]; p+=pstrides[0], q+=qstrides[0], i++) { const char *xp = ADJUST_PTR(p, psuboffsets, 0); const char *xq = ADJUST_PTR(q, qsuboffsets, 0); From affb52d0380a6c04898c1889070a803385b7836f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Tue, 24 Nov 2026 00:35:16 +0100 Subject: [PATCH 6/6] worth testing --- Lib/test/test_memoryview.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_memoryview.py b/Lib/test/test_memoryview.py index 43cb90462c85343..0c66533c62b0b01 100644 --- a/Lib/test/test_memoryview.py +++ b/Lib/test/test_memoryview.py @@ -568,6 +568,7 @@ def test_compare_byte_views(self): spaced = memoryview(b'axb').cast(fmt)[::2] self.assertEqual(packed, spaced) self.assertEqual(spaced, packed) + self.assertEqual(packed, memoryview(bytearray(b'ab')).cast(fmt)) self.assertNotEqual(packed, memoryview(b'ax').cast(fmt)) def test_comparison_release_during_buffer_callback(self):