Author vstinner
Recipients rhettinger, serhiy.storchaka, vstinner
Date 2017-02-05.10:09:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Attached patch changes index(), insert() and rotate() functions of the collections.deque type to use FASTCALL calling convention. I chose to only modify these functions since they use METH_VARARGS which requires to create a temporary tuple, whereas other functions use METH_NOARGS or METH_O which is already fast ;-)

I know that Raymond, maintainer of the collections module, is not a big fan of Argument Clinic ;-) So I wrote the minimum change and chose to not use Argument Clinic yet. By the way, the index() method has the signature "D.index(value, [start, [stop]])" which is not supported by Argument Clinic yet: see issue #29299. For these reasons, I propose to wait to convert collections.deque to Argument Clinic, it can be done later.
Ok, now the cool part: it makes these methods faster ;-)

* d.rotate(): 1.10x faster
* d.rotate(1): 1.24x faster
* d.insert(): 1.18x faster
* d.index(): 1.24x faster

$ ./python -m perf timeit -s 'import collections; d=collections.deque()' 'd.rotate()' --compare-to=../default-ref/python 

Median +- std dev: [ref] 70.5 ns +- 0.9 ns -> [patch] 64.2 ns +- 0.3 ns: 1.10x faster (-9%)

$ ./python -m perf timeit -s 'import collections; d=collections.deque()' 'd.rotate(1)' --compare-to=../default-ref/python

Median +- std dev: [ref] 107 ns +- 1 ns -> [patch] 86.2 ns +- 1.1 ns: 1.24x faster (-20%)

$ ./python -m perf timeit -s 'import collections' 'd=collections.deque(); d.insert(0, None); d.insert(1, None); d.insert(2, None); d.insert(3, None); d.insert(4, None)' --compare-to=../default-ref/python -p3

Median +- std dev: [ref] 699 ns +- 6 ns -> [patch] 591 ns +- 5 ns: 1.18x faster (-15%)

$ ./python -m perf timeit -s 'import collections; d=collections.deque((None,))' 'd.index(None)' --compare-to=../default-ref/python 

Median +- std dev: [ref] 115 ns +- 1 ns -> [patch] 92.5 ns +- 0.8 ns: 1.24x faster (-19%)
Date User Action Args
2017-02-05 10:09:27vstinnersetrecipients: + vstinner, rhettinger, serhiy.storchaka
2017-02-05 10:09:27vstinnersetmessageid: <>
2017-02-05 10:09:27vstinnerlinkissue29452 messages
2017-02-05 10:09:26vstinnercreate