Skip to content

Commit 6e6bff1

Browse files
Deque as a Growable Ring Buffer
Inspired by Rust's VecDeque, we have rewritten Python's collections.deque to use a growable ring buffer implementation. This change improves memory efficiency and performance for various operations, including indexing and slicing. Specifically, indexing is now O(1), and slicing is supported at all in O(k) time, where k is the number of elements in the slice.
1 parent 9b0179f commit 6e6bff1

File tree

6 files changed

+1243
-928
lines changed

6 files changed

+1243
-928
lines changed

Doc/library/collections.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,8 @@ or subtracting from an empty counter.
479479
corresponding number of items are discarded from the opposite end. Bounded
480480
length deques provide functionality similar to the ``tail`` filter in
481481
Unix. They are also useful for tracking transactions and other pools of data
482-
where only the most recent activity is of interest.
482+
where only the most recent activity is of interest. Passing a *maxlen*
483+
greater than :data:`sys.maxsize` raises :exc:`ValueError`.
483484

484485

485486
Deque objects support the following methods:
@@ -591,9 +592,11 @@ or subtracting from an empty counter.
591592

592593
In addition to the above, deques support iteration, pickling, ``len(d)``,
593594
``reversed(d)``, ``copy.copy(d)``, ``copy.deepcopy(d)``, membership testing with
594-
the :keyword:`in` operator, and subscript references such as ``d[0]`` to access
595-
the first element. Indexed access is *O*\ (1) at both ends but slows to *O*\ (*n*) in
596-
the middle. For fast random access, use lists instead.
595+
the :keyword:`in` operator, subscript references such as ``d[0]`` to access
596+
the first element, and slicing notation like ``d[i:j:k]`` which returns a new
597+
deque of the same type (including subclasses) while preserving ``maxlen``.
598+
Indexed access is *O*\ (1). Slicing is *O*\ (k) where *k* is the number of
599+
elements in the slice.
597600

598601
Starting in version 3.5, deques support ``__add__()``, ``__mul__()``,
599602
and ``__imul__()``.

Doc/whatsnew/3.14.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,15 @@ bdb
21932193
to the :class:`~bdb.Bdb` class's new *backend* parameter.
21942194
(Contributed by Tian Gao in :gh:`124533`.)
21952195

2196+
collections
2197+
-----------
2198+
2199+
* Slicing a :class:`collections.deque <collections.deque>` is now supported. Our deque also support
2200+
constant time indexing.
2201+
2202+
* Passing a *maxlen* larger than :data:`sys.maxsize` to :class:`collections.deque`
2203+
now raises :exc:`ValueError` instead of :exc:`OverflowError`.
2204+
21962205

21972206
difflib
21982207
-------

Lib/collections/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454
except ImportError:
5555
pass
5656

57+
try:
58+
# Ditto for reverse iterators (used by pickle reducers)
59+
from _collections import _deque_reverse_iterator # noqa: F401
60+
except ImportError:
61+
pass
62+
5763
try:
5864
from _collections import defaultdict
5965
except ImportError:

0 commit comments

Comments
 (0)