[issue24874] Improve pickling efficiency of itertools.cycle

Raymond Hettinger report at bugs.python.org
Sat Aug 15 23:23:07 CEST 2015


New submission from Raymond Hettinger:

When a cycle object has fully consumed its input iterable, __reduce__ method uses the returns a space-inefficient result when space-efficient alternative is available.

# Current way of restoring a cycle object with excess info in setstate:
>>> c = cycle(iter('de'))
>>> c.__setstate__((['a', 'b', 'c', 'd', 'e'], 1))
>>> ''.join(next(c) for i in range(20)) # next 20 values
'deabcdeabcdeabcdeabc'

# The same result can be achieved in less info in setstate: 
>>> c = cycle(iter('de'))
>>> c.__setstate__((['a', 'b', 'c'], 0))
>>> ''.join(next(c) for i in range(20)) # next 20 values

----------
assignee: rhettinger
components: Extension Modules
messages: 248657
nosy: rhettinger
priority: normal
severity: normal
status: open
title: Improve pickling efficiency of itertools.cycle
type: resource usage
versions: Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24874>
_______________________________________


More information about the Python-bugs-list mailing list