Message381612
The proposed PR speeds up comparison of bytes and bytearray object with objects of different types, especially if use the -b option.
Before:
$ ./python -m timeit -s "x = b''" "x == None"
10000000 loops, best of 5: 29.5 nsec per loop
$ ./python -b -m timeit -s "x = b''" "x == None"
2000000 loops, best of 5: 139 nsec per loop
$ ./python -m timeit -s "x = bytearray()" "x == None"
1000000 loops, best of 5: 282 nsec per loop
$ ./python -b -m timeit -s "x = bytearray()" "x == None"
1000000 loops, best of 5: 282 nsec per loop
After:
$ ./python -m timeit -s "x = b''" "x == None"
10000000 loops, best of 5: 29.7 nsec per loop
$ ./python -b -m timeit -s "x = b''" "x == None"
10000000 loops, best of 5: 29.9 nsec per loop
$ ./python -m timeit -s "x = bytearray()" "x == None"
10000000 loops, best of 5: 32.1 nsec per loop
$ ./python -b -m timeit -s "x = bytearray()" "x == None"
10000000 loops, best of 5: 32.2 nsec per loop
There were two causes of the slowdown:
1. When checked for bytes warning, the code used slow PyObject_IsInstance() which checks the __class__ attribute and looks up several other attributes. Using fast PyUnicode_Check() and PyLong_Check() is enough, because the only case when these methods give different result if you compare with an instance of special class with the __class__ property which return str or int. It is very uncommon case.
2. For bytearray, it tried to get buffers of arguments, and if they did not support the buffer protocol, a TypeError was raised and immediately suppressed. Using fast check PyObject_CheckBuffer() allows to get rid of raising an exception.
Also, PyUnicode_Check() and PyLong_Check() are more convenient, because they do not need error handling. |
|
Date |
User |
Action |
Args |
2020-11-22 12:31:52 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, methane |
2020-11-22 12:31:52 | serhiy.storchaka | set | messageid: <1606048312.33.0.0547387473291.issue42435@roundup.psfhosted.org> |
2020-11-22 12:31:52 | serhiy.storchaka | link | issue42435 messages |
2020-11-22 12:31:52 | serhiy.storchaka | create | |
|