Author serhiy.storchaka
Recipients methane, serhiy.storchaka
Date 2020-11-22.12:31:52
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1606048312.33.0.0547387473291.issue42435@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2020-11-22 12:31:52serhiy.storchakasetrecipients: + serhiy.storchaka, methane
2020-11-22 12:31:52serhiy.storchakasetmessageid: <1606048312.33.0.0547387473291.issue42435@roundup.psfhosted.org>
2020-11-22 12:31:52serhiy.storchakalinkissue42435 messages
2020-11-22 12:31:52serhiy.storchakacreate