gh-144995: Optimize memoryview == memoryview#144996
gh-144995: Optimize memoryview == memoryview#144996vstinner wants to merge 4 commits intopython:mainfrom
Conversation
|
Results of the benchmark from the issue: memoryview comparison complexity is no longer O(n) but O(1): values are no longer compared. |
Objects/memoryobject.c
Outdated
| } | ||
|
|
||
| static int | ||
| is_float_format(const char *format) |
There was a problem hiding this comment.
Does this cover the complex types?
import numpy as np
a = np.array([1+2j, 3+4j, float('nan')], dtype=np.complex128)
mv = memoryview(a)
mv == mv # False
There was a problem hiding this comment.
This memory format is Zd. Oh, my change doesn't work for this memoryview. I should replace the blocklist with an allowlist. I'm not a memoryview/buffer expert. I didn't know that 3rd party projects can have their own format.
|
@eendebakpt: I updated the PR to allow formats known to be safe for pointer comparison (integer types), instead of blocking formats known to use floats. I excluded the format |
I think adding the |
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Objects/memoryobject.c
Outdated
| const char *format = vv->format; | ||
| if (format != NULL) { | ||
| // Exclude formats "d" (double), "f" (float), "e" (16-bit float) | ||
| // and "P" (void*) |
| if (format != NULL) { | ||
| // Exclude formats "d" (double), "f" (float), "e" (16-bit float) | ||
| // and "P" (void*) | ||
| can_compare_ptr = (strchr("bBchHiIlLnNqQ?", format[0]) != NULL |
There was a problem hiding this comment.
Note that format can starts with "@".
| a = array.array(float_format, [1.0, 2.0, float('nan')]) | ||
| m = memoryview(a) | ||
| # nan is not equal to nan | ||
| self.assertFalse(m == m) |
| with self.subTest(format=int_format): | ||
| a = array.array(int_format, [1, 2, 3]) | ||
| m = memoryview(a) | ||
| self.assertTrue(m == m) |
| # 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': |
There was a problem hiding this comment.
Can "?" be tested? Can format starting with "@" be tested? Can the null format be tested?
Uh oh!
There was an error while loading. Please reload this page.