Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
matthiasgoergens wants to merge5 commits intopython:main
base:main
Choose a base branch
Loading
frommatthiasgoergens:growable-ring-buffer

Conversation

@matthiasgoergens
Copy link
Contributor

@matthiasgoergensmatthiasgoergens commentedNov 10, 2025
edited by bedevere-appbot
Loading

Inspired by Rust's VecDeque, we have rewritten Python'scollections.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.

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`.
Copy link
ContributorAuthor

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.)

Copy link

CopilotAI left a 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
FileDescription
Modules/_collectionsmodule.cComplete rewrite of deque internals from block-based to ring buffer implementation, including all operations (append, pop, rotate, etc.)
Modules/clinic/_collectionsmodule.c.hUpdated generated clinic code for deque_rotate to accept PyObject* instead of Py_ssize_t
Lib/test/test_deque.pyAdded extensive hypothesis-based property tests and new test cases for slicing functionality
Lib/collections/init.pyAdded import for reverse iterator type needed for pickle support
Doc/whatsnew/3.14.rstDocumented new slicing support and maxlen validation change
Doc/library/collections.rstUpdated 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.


/* 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>
Copy link
ContributorAuthor

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.

@matthiasgoergens
Copy link
ContributorAuthor

Hmm, looks like I broke some of the tests. Will fix.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

Copilot code reviewCopilotCopilot left review comments

@rhettingerrhettingerAwaiting requested review from rhettingerrhettinger is a code owner

@AA-TurnerAA-TurnerAwaiting requested review from AA-Turner

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

@matthiasgoergens

[8]ページ先頭

©2009-2025 Movatter.jp