Serhiy Storchaka added the comment:

There is the extra special case code for bytes and bytearray iterators and 
reversed list iterator. The patch just extends the optimizations to other 
builtin iterators.

In the example with itertools.islice() the overhead of pickling redundant data 
is 20%. If there are a lot of iterators, the overhead can be up to 90%.

>>> import pickle, pickletools, itertools
>>> len(pickletools.optimize(pickle.dumps([itertools.islice('abcdefgh', 4) for 
>>> i in range(1000)], 4)))
Unpatched: 23059
Patched:   12059

Of course this is degenerated case. But if the data contains a lot of initial 
iterators of the same sequence or of short sequences, or exhausted iterators, 
the benefit can be not small.

Yet one benefit from the patch is that it speeds up copying and deepcopying 
initial and exhausted iterators.

$ ./python -m timeit -s "from itertools import islice; from copy import copy; 
it = islice('abcdefgh', 4)" -- "copy(it)"
Unpatched: 7.37 usec per loop
Patched:   6.4 usec per loop

$ ./python -m timeit -s "from itertools import islice; from copy import 
deepcopy; it = islice('abcdefgh', 4)" -- "deepcopy(it)"
Unpatched: 41.7 usec per loop
Patched:   32.6 usec per loop

$ ./python -m timeit -s "from copy import copy; it = iter('abcdefgh')" -- 
"copy(it)"
Unpatched: 10.4 usec per loop
Patched:   9.67 usec per loop

$ ./python -m timeit -s "from copy import deepcopy; it = iter('abcdefgh')" -- 
"deepcopy(it)"
Unpatched: 21.1 usec per loop
Patched:   18.3 usec per loop

$ ./python -m timeit -s "from copy import copy; it = iter(list('abcdefgh'))" -- 
"copy(it)"
Unpatched: 10.3 usec per loop
Patched:   9.54 usec per loop

$ ./python -m timeit -s "from copy import deepcopy; it = 
iter(list('abcdefgh'))" -- "deepcopy(it)"
Unpatched: 39.7 usec per loop
Patched:   36.8 usec per loop

----------

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

Reply via email to