New submission from Serhiy Storchaka <storchaka+cpyt...@gmail.com>:

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.

----------
components: Interpreter Core
messages: 381612
nosy: methane, serhiy.storchaka
priority: normal
severity: normal
status: open
title: Spped up comparison of bytes and bytearray object with objects of 
different types
type: performance
versions: Python 3.10

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue42435>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to