Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.4k
gh-141335: Deque as a Growable Ring Buffer#141337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Conversation
| Unix. They are also useful for tracking transactions and other pools of data | ||
| where only the most recent activity is of interest. | ||
| where only the most recent activity is of interest. Passing a *maxlen* | ||
| greater than:data:`sys.maxsize` raises:exc:`ValueError`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
We could also leave the behaviour the same as before (raiseOverflowError), or fix it to be in line with what eg slicing for lists does (which handles huge numbers gracefully.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Pull Request Overview
This PR reimplements thecollections.deque data structure from a doubly-linked list of blocks to a growable ring buffer (circular array), authored by Matthias Goergens. The primary goals are to improve performance for indexing operations and add slicing support.
Key Changes
- Replaced the block-based doubly-linked list implementation with a power-of-2 sized ring buffer using a single dynamically allocated array
- Added O(1) indexing support (previously O(n) in the middle) and O(k) slicing support for deques
- Updated documentation to reflect the new capabilities and performance characteristics
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 14 comments.
Show a summary per file
| File | Description |
|---|---|
| Modules/_collectionsmodule.c | Complete rewrite of deque internals from block-based to ring buffer implementation, including all operations (append, pop, rotate, etc.) |
| Modules/clinic/_collectionsmodule.c.h | Updated generated clinic code for deque_rotate to accept PyObject* instead of Py_ssize_t |
| Lib/test/test_deque.py | Added extensive hypothesis-based property tests and new test cases for slicing functionality |
| Lib/collections/init.py | Added import for reverse iterator type needed for pickle support |
| Doc/whatsnew/3.14.rst | Documented new slicing support and maxlen validation change |
| Doc/library/collections.rst | Updated documentation to reflect O(1) indexing, slicing support, and maxlen validation behavior |
💡Add Copilot custom instructions for smarter, more guided reviews.Learn how to get started.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
| /* collections module implementation of a deque() datatype | ||
| Written and maintained by Raymond D. Hettinger <python@rcn.com> | ||
| Originally written by Raymond D. Hettinger <python@rcn.com> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@rhettinger I'm happy to handle this however you feel like, including leaving the whole comment as is.
c735b03 to8aeb1d7CompareCo-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
matthiasgoergens commentedNov 10, 2025
Hmm, looks like I broke some of the tests. Will fix. |
Uh oh!
There was an error while loading.Please reload this page.
Inspired by Rust's VecDeque, we have rewritten Python's
collections.dequeto 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.
collections.dequeto a growable ring buffer for fast random access #141335