diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index 9a8108d882e02f..1df474628d4993 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -479,7 +479,8 @@ or subtracting from an empty counter. corresponding number of items are discarded from the opposite end. Bounded length deques provide functionality similar to the ``tail`` filter in 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`. Deque objects support the following methods: @@ -591,9 +592,11 @@ or subtracting from an empty counter. In addition to the above, deques support iteration, pickling, ``len(d)``, ``reversed(d)``, ``copy.copy(d)``, ``copy.deepcopy(d)``, membership testing with -the :keyword:`in` operator, and subscript references such as ``d[0]`` to access -the first element. Indexed access is *O*\ (1) at both ends but slows to *O*\ (*n*) in -the middle. For fast random access, use lists instead. +the :keyword:`in` operator, subscript references such as ``d[0]`` to access +the first element, and slicing notation like ``d[i:j:k]`` which returns a new +deque of the same type (including subclasses) while preserving ``maxlen``. +Indexed access is *O*\ (1). Slicing is *O*\ (k) where *k* is the number of +elements in the slice. Starting in version 3.5, deques support ``__add__()``, ``__mul__()``, and ``__imul__()``. diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index 25ac4d1d524bc2..fb35c619dd20db 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -54,6 +54,12 @@ except ImportError: pass +try: + # Ditto for reverse iterators (used by pickle reducers) + from _collections import _deque_reverse_iterator # noqa: F401 +except ImportError: + pass + try: from _collections import defaultdict except ImportError: diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py index 4e1a489205a685..56e49381fc0c20 100644 --- a/Lib/test/test_deque.py +++ b/Lib/test/test_deque.py @@ -5,11 +5,161 @@ import gc import weakref import copy +import operator import pickle import random import struct +import sys + +from test.support.hypothesis_helper import hypothesis + +st = hypothesis.strategies +assume = hypothesis.assume BIG = 100000 +VALUE_STRATEGY = st.text() +DEFAULT_MAXLEN_STRATEGY = st.one_of( + st.none(), + st.integers(min_value=0, max_value=sys.maxsize), +) + + +@st.composite +def simple_deques(draw, maxlen_strategy=DEFAULT_MAXLEN_STRATEGY, *, + value_strategy=VALUE_STRATEGY, check_for_equality=True): + items = draw(st.lists(value_strategy)) + maxlen = draw(maxlen_strategy) + if maxlen is not None: + assume(0 <= maxlen <= sys.maxsize) + d = deque(items, maxlen=maxlen) + if check_for_equality: + if maxlen is None or len(items) <= maxlen: + assert items == list(d) + else: + assert list(d) == items[len(items)-maxlen:], (items, maxlen, d) + return d + + +@st.composite +def composite_deques(draw, maxlen_strategy=DEFAULT_MAXLEN_STRATEGY, *, + value_strategy=VALUE_STRATEGY, check_for_equality=True): + d = draw(simple_deques(maxlen_strategy=maxlen_strategy, + value_strategy=value_strategy, check_for_equality=check_for_equality)) + shadow = list(d) + + def append(): + value = draw(value_strategy) + d.append(value) + shadow.append(value) + if d.maxlen is not None and len(shadow) > d.maxlen: + shadow.pop(0) + if check_for_equality: + assert shadow == list(d) + + def appendleft(): + value = draw(value_strategy) + d.appendleft(value) + shadow.insert(0, value) + if d.maxlen is not None and len(shadow) > d.maxlen: + shadow.pop() + if check_for_equality: + assert shadow == list(d) + + def pop(): + try: + result = d.pop() + except IndexError: + assert not shadow + else: + assert shadow + expected = shadow.pop() + if check_for_equality: + assert result == expected + if check_for_equality: + assert shadow == list(d) + + def popleft(): + try: + result = d.popleft() + except IndexError: + assert not shadow + else: + assert shadow + expected = shadow.pop(0) + if check_for_equality: + assert result == expected + if check_for_equality: + assert shadow == list(d) + + def rotate(): + steps = draw(st.integers()) + d.rotate(steps) + if shadow: + tmp = deque(shadow) + tmp.rotate(steps) + shadow[:] = list(tmp) + if check_for_equality: + assert shadow == list(d) + + def clear(): + d.clear() + shadow.clear() + if check_for_equality: + assert shadow == list(d) + + def imul(): + nonlocal shadow, d + multiplier = draw(st.integers(-5, 8)) + d *= multiplier + if multiplier <= 0: + shadow.clear() + else: + shadow *= multiplier + if d.maxlen is not None: + shadow = shadow[-d.maxlen:] + if check_for_equality: + assert shadow == list(d) + + operations = st.lists(st.sampled_from([append, appendleft, pop, popleft, rotate, clear, imul])) + for op in draw(operations): + op() + + return d + +_NONREFLEXIVE_STRATEGY = st.one_of( + st.floats(), + st.just(support.NEVER_EQ), +) + +_SLICE_BOUND_STRATEGY = st.one_of(st.none(), st.integers(-30, 30)) +_SLICE_STEP_STRATEGY = st.one_of( + st.none(), st.integers(-5, 5).filter(lambda value: value != 0) +) + +@st.composite +def composite_deques_maxlen(draw, value_strategy=VALUE_STRATEGY, *, + check_for_equality=True): + return draw( + composite_deques( + maxlen_strategy=st.integers(min_value=0, max_value=sys.maxsize), + value_strategy=value_strategy, + check_for_equality=check_for_equality, + ) + ) + +@st.composite +def composite_deques_optional_maxlen(draw, value_strategy=VALUE_STRATEGY, *, + check_for_equality=True): + return draw( + composite_deques( + maxlen_strategy=st.one_of( + st.just(None), + st.integers(min_value=0, max_value=sys.maxsize), + ), + value_strategy=value_strategy, + check_for_equality=check_for_equality, + ) + ) def fail(): raise SyntaxError @@ -27,7 +177,21 @@ def __eq__(self, other): self.deque.clear() return self.result -class TestBasic(unittest.TestCase): +class TestDequeProperty(unittest.TestCase): + + @hypothesis.given(d=composite_deques()) + def test_strategy(self, d): + """Test to exercise the deque strategy. + + The strategy itself contains some asserts to verify correctness. + """ + + @hypothesis.given(d=composite_deques_maxlen()) + def test_strategy_maxlen(self, d): + """Test to exercise the deque strategy with maxlen. + + The strategy itself contains some asserts to verify correctness. + """ def test_basics(self): d = deque(range(-5125, -5000)) @@ -51,6 +215,9 @@ def test_basics(self): def test_maxlen(self): self.assertRaises(ValueError, deque, 'abc', -1) self.assertRaises(ValueError, deque, 'abc', -2) + huge = sys.maxsize + 1 + self.assertRaises(ValueError, deque, 'abc', huge) + self.assertRaises(ValueError, deque, maxlen=huge) it = iter(range(10)) d = deque(it, maxlen=3) self.assertEqual(list(it), []) @@ -96,6 +263,100 @@ def test_maxlen_attribute(self): d = deque('abc') d.maxlen = 10 + @hypothesis.given( + dq=composite_deques(), + start=_SLICE_BOUND_STRATEGY, + stop=_SLICE_BOUND_STRATEGY, + step=_SLICE_STEP_STRATEGY, + ) + def test_slice_like_list(self, dq, start, stop, step): + result = dq[start:stop:step] + self.assertIsInstance(result, deque) + expected = list(dq)[slice(start, stop, step)] + self.assertEqual(list(result), expected) + + @hypothesis.given( + dq=composite_deques(), + start=_SLICE_BOUND_STRATEGY, + stop=_SLICE_BOUND_STRATEGY, + step=_SLICE_STEP_STRATEGY, + ) + def test_slice_preserves_maxlen(self, dq, start, stop, step): + result = dq[start:stop:step] + self.assertIsInstance(result, deque) + self.assertEqual(result.maxlen, dq.maxlen) + + @hypothesis.given(composite_deques_optional_maxlen()) + def test_exercise_auxiliary_operations(self, d): + self.assertIsInstance(str(d), str) + self.assertIsInstance(repr(d), str) + self.assertEqual(len(d), len(list(d))) + self.assertEqual(copy.copy(d), d) + + @hypothesis.given(data=composite_deques_optional_maxlen()) + def test_supports_weakref(self, data): + dq = deque(data) + ref = weakref.ref(dq) + self.assertIs(ref(), dq) + del dq + support.gc_collect() + self.assertIsNone(ref()) + + @hypothesis.given( + data=composite_deques(), + advance=st.integers(min_value=0, max_value=30), + reverse=st.booleans(), + ) + def test_iterator_length_hint_tracks_remaining_items( + self, data, advance, reverse + ): + it = reversed(data) if reverse else iter(data) + baseline = list(reversed(data)) if reverse else list(data) + consumed = len(list(zip(range(advance), it))) + expected = max(len(baseline) - consumed, 0) + self.assertEqual(operator.length_hint(it), expected) + + @hypothesis.given( + data=composite_deques(), + advance=st.integers(min_value=0, max_value=25), + reverse=st.booleans(), + ) + def test_iterators_pickle_roundtrip(self, data, advance, reverse): + if reverse: + iterator = reversed(data) + baseline = list(reversed(data)) + else: + iterator = iter(data) + baseline = list(data) + + consumed = len(list(zip(range(advance), iterator))) + + payload = pickle.dumps(iterator) + restored = pickle.loads(payload) + expected = baseline[consumed:] + self.assertEqual(list(restored), expected) + self.assertEqual(list(iterator), expected) + + @hypothesis.given( + dq=composite_deques_optional_maxlen(check_for_equality=False) + ) + def test_equality_handles_nonreflexive_elements(self, dq): + left = dq.copy() + right = dq + self.assertEqual(left, right) + self.assertFalse(left != right) + + @hypothesis.given( + dq=composite_deques_optional_maxlen(check_for_equality=False), + turns=st.integers(-1000, 1000), + ) + def test_equality_respects_rotation(self, dq, turns): + left = dq.copy() + right = dq.copy() + left.rotate(turns) + right.rotate(turns) + self.assertEqual(left, right) + def test_count(self): for s in ('', 'abracadabra', 'simsalabim'*500+'abc'): s = list(s) @@ -746,17 +1007,23 @@ class C(object): @support.cpython_only def test_sizeof(self): - MAXFREEBLOCKS = 16 - BLOCKLEN = 64 - basesize = support.calcvobjsize('2P5n%dPP' % MAXFREEBLOCKS) - blocksize = struct.calcsize('P%dPP' % BLOCKLEN) + basesize = deque().__sizeof__() + ptrsize = struct.calcsize('P') + self.assertEqual(object.__sizeof__(deque()), basesize) + + def expected(n): + if n == 0: + return basesize + cap = 1 + while cap < n: + cap <<= 1 + return basesize + cap * ptrsize + check = self.check_sizeof - check(deque(), basesize + blocksize) - check(deque('a'), basesize + blocksize) - check(deque('a' * (BLOCKLEN - 1)), basesize + blocksize) - check(deque('a' * BLOCKLEN), basesize + 2 * blocksize) - check(deque('a' * (42 * BLOCKLEN)), basesize + 43 * blocksize) + for n in (0, 1, 2, 3, 4, 5, 8, 9, 64, 65, 512, 1025): + d = deque(range(n)) + check(d, expected(n)) class TestVariousIteratorArgs(unittest.TestCase): @@ -794,6 +1061,35 @@ def __iter__(self): class TestSubclass(unittest.TestCase): + @hypothesis.given( + dq=composite_deques_optional_maxlen(check_for_equality=False), + start=_SLICE_BOUND_STRATEGY, + stop=_SLICE_BOUND_STRATEGY, + step=_SLICE_STEP_STRATEGY, + ) + def test_slice_preserves_subclass(self, dq, start, stop, step): + d = Deque(dq, dq.maxlen) + view = d[start:stop:step] + self.assertIsInstance(view, Deque) + self.assertIsNot(view, d) + expected = list(d)[slice(start, stop, step)] + self.assertEqual(list(view), expected) + self.assertEqual(view.maxlen, d.maxlen) + + @hypothesis.given( + items=st.lists(st.integers()), + maxlen=st.integers(min_value=0, max_value=sys.maxsize)) + def test_slice_and_copy_preserve_large_maxlen(self, items, maxlen): + d = Deque(items, maxlen) + + sliced = d[:] + self.assertIsInstance(sliced, Deque) + self.assertEqual(sliced.maxlen, maxlen) + + copied = d.copy() + self.assertIsInstance(copied, Deque) + self.assertEqual(copied.maxlen, maxlen) + def test_basics(self): d = Deque(range(25)) d.__init__(range(200)) @@ -910,13 +1206,46 @@ def test_getitem(self): # For now, bypass tests that require slicing pass - def test_getslice(self): - # For now, bypass tests that require slicing - pass + @hypothesis.given( + dq=composite_deques_optional_maxlen(), + start=_SLICE_BOUND_STRATEGY, + stop=_SLICE_BOUND_STRATEGY, + step=_SLICE_STEP_STRATEGY, + ) + def test_getslice(self, dq, start, stop, step): + subslice = slice(start, stop, step) + expected = deque(list(dq)[subslice], maxlen=dq.maxlen) + actual = dq[subslice] + self.assertIsInstance(actual, deque) + self.assertEqual(actual, expected) + self.assertIsNot(actual, dq) + + @hypothesis.given( + dq=composite_deques_optional_maxlen(check_for_equality=False), + index=st.integers(-50, 50), + ) + def test_subscript(self, dq, index): + data = list(dq) + if -len(data) <= index < len(data): + self.assertEqual(dq[index], data[index]) + else: + with self.assertRaises(IndexError): + _ = dq[index] + + clone = dq[:] + self.assertIsInstance(clone, deque) + self.assertIsNot(clone, dq) + self.assertEqual( + list(clone), list(dq) + ) + self.assertEqual(clone.maxlen, dq.maxlen) + + even = dq[::2] + self.assertEqual(list(even), list(dq)[::2]) + self.assertEqual(even.maxlen, dq.maxlen) - def test_subscript(self): - # For now, bypass tests that require slicing - pass + with self.assertRaises(TypeError): + _ = dq[1.5] def test_free_after_iterating(self): # For now, bypass tests that require slicing diff --git a/Misc/NEWS.d/next/Library/2025-11-10-07-52-18.gh-issue-141335.g29Ksw.rst b/Misc/NEWS.d/next/Library/2025-11-10-07-52-18.gh-issue-141335.g29Ksw.rst new file mode 100644 index 00000000000000..2dd35c192a343a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-11-10-07-52-18.gh-issue-141335.g29Ksw.rst @@ -0,0 +1 @@ +Slicing a :class:`collections.deque ` is now supported. Our deque also support constant time indexing. diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index 3ba48d5d9d3c64..2ce935d96adc23 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -1,13 +1,16 @@ #include "Python.h" #include "pycore_call.h" // _PyObject_CallNoArgs() #include "pycore_dict.h" // _PyDict_GetItem_KnownHash() +#include "pycore_list.h" // _Py_memory_repeat() #include "pycore_long.h" // _PyLong_GetZero() #include "pycore_moduleobject.h" // _PyModule_GetState() +#include "pycore_object.h" // _Py_RefcntAdd() #include "pycore_pyatomic_ft_wrappers.h" #include "pycore_typeobject.h" // _PyType_GetModuleState() #include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() #include +#include "structmember.h" typedef struct { PyTypeObject *deque_type; @@ -35,6 +38,8 @@ get_module_state_by_cls(PyTypeObject *cls) static struct PyModuleDef _collectionsmodule; +static int valid_index(Py_ssize_t i, Py_ssize_t limit); + static inline collections_state * find_module_state_by_def(PyTypeObject *type) { @@ -65,173 +70,231 @@ class dequeobject_converter(self_converter): /*[python end generated code: output=da39a3ee5e6b4b0d input=b6ae4a3ff852be2f]*/ /* collections module implementation of a deque() datatype - Written and maintained by Raymond D. Hettinger + Originally written by Raymond D. Hettinger + Rewrite as a growable ring buffer by Matthias Goergens */ -/* The block length may be set to any number over 1. Larger numbers - * reduce the number of calls to the memory allocator, give faster - * indexing and rotation, and reduce the link to data overhead ratio. - * Making the block length a power of two speeds-up the modulo - * and division calculations in deque_item() and deque_ass_item(). - */ - -#define BLOCKLEN 64 -#define CENTER ((BLOCKLEN - 1) / 2) -#define MAXFREEBLOCKS 16 - -/* Data for deque objects is stored in a doubly-linked list of fixed - * length blocks. This assures that appends or pops never move any - * other data elements besides the one being appended or popped. - * - * Another advantage is that it completely avoids use of realloc(), - * resulting in more predictable performance. - * - * Textbook implementations of doubly-linked lists store one datum - * per link, but that gives them a 200% memory overhead (a prev and - * next link for each datum) and it costs one malloc() call per data - * element. By using fixed-length blocks, the link to data ratio is - * significantly improved and there are proportionally fewer calls - * to malloc() and free(). The data blocks of consecutive pointers - * also improve cache locality. - * - * The list of blocks is never empty, so d.leftblock and d.rightblock - * are never equal to NULL. The list is not circular. - * - * A deque d's first element is at d.leftblock[leftindex] - * and its last element is at d.rightblock[rightindex]. - * - * Unlike Python slice indices, these indices are inclusive on both - * ends. This makes the algorithms for left and right operations - * more symmetrical and it simplifies the design. - * - * The indices, d.leftindex and d.rightindex are always in the range: - * 0 <= index < BLOCKLEN - * - * And their exact relationship is: - * (d.leftindex + d.len - 1) % BLOCKLEN == d.rightindex - * - * Whenever d.leftblock == d.rightblock, then: - * d.leftindex + d.len - 1 == d.rightindex - * - * However, when d.leftblock != d.rightblock, the d.leftindex and - * d.rightindex become indices into distinct blocks and either may - * be larger than the other. - * - * Empty deques have: - * d.len == 0 - * d.leftblock == d.rightblock - * d.leftindex == CENTER + 1 - * d.rightindex == CENTER - * - * Checking for d.len == 0 is the intended way to see whether d is empty. - */ - -typedef struct BLOCK { - struct BLOCK *leftlink; - PyObject *data[BLOCKLEN]; - struct BLOCK *rightlink; -} block; - struct dequeobject { PyObject_VAR_HEAD - block *leftblock; - block *rightblock; - Py_ssize_t leftindex; /* 0 <= leftindex < BLOCKLEN */ - Py_ssize_t rightindex; /* 0 <= rightindex < BLOCKLEN */ - size_t state; /* incremented whenever the indices move */ - Py_ssize_t maxlen; /* maxlen is -1 for unbounded deques */ - Py_ssize_t numfreeblocks; - block *freeblocks[MAXFREEBLOCKS]; + /* Vector of pointers to list elements. list[0] is ob_item[0], etc. */ + PyObject **ob_item; + /* allocated needs to be a power of two */ + Py_ssize_t allocated; + Py_ssize_t first_element; + Py_ssize_t maxlen; + + size_t state; /* incremented whenever the indices move, to eg detect mutations during iteration */ PyObject *weakreflist; }; #define dequeobject_CAST(op) ((dequeobject *)(op)) -/* For debug builds, add error checking to track the endpoints - * in the chain of links. The goal is to make sure that link - * assignments only take place at endpoints so that links already - * in use do not get overwritten. - * - * CHECK_END should happen before each assignment to a block's link field. - * MARK_END should happen whenever a link field becomes a new endpoint. - * This happens when new blocks are added or whenever an existing - * block is freed leaving another existing block as the new endpoint. - */ - -#ifndef NDEBUG -#define MARK_END(link) link = NULL; -#define CHECK_END(link) assert(link == NULL); -#define CHECK_NOT_END(link) assert(link != NULL); -#else -#define MARK_END(link) -#define CHECK_END(link) -#define CHECK_NOT_END(link) -#endif - -/* A simple freelisting scheme is used to minimize calls to the memory - allocator. It accommodates common use cases where new blocks are being - added at about the same rate as old blocks are being freed. - */ - -static inline block * -newblock(dequeobject *deque) { - block *b; - if (deque->numfreeblocks) { - deque->numfreeblocks--; - return deque->freeblocks[deque->numfreeblocks]; - } - b = PyMem_Malloc(sizeof(block)); - if (b != NULL) { - return b; - } - PyErr_NoMemory(); - return NULL; -} - -static inline void -freeblock(dequeobject *deque, block *b) -{ - if (deque->numfreeblocks < MAXFREEBLOCKS) { - deque->freeblocks[deque->numfreeblocks] = b; - deque->numfreeblocks++; - } else { - PyMem_Free(b); - } -} static PyObject * deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { dequeobject *deque; - block *b; /* create dequeobject structure */ deque = (dequeobject *)type->tp_alloc(type, 0); if (deque == NULL) return NULL; - b = newblock(deque); - if (b == NULL) { - Py_DECREF(deque); - return NULL; - } - MARK_END(b->leftlink); - MARK_END(b->rightlink); - - assert(BLOCKLEN >= 2); Py_SET_SIZE(deque, 0); - deque->leftblock = b; - deque->rightblock = b; - deque->leftindex = CENTER + 1; - deque->rightindex = CENTER; - deque->state = 0; + deque->ob_item = NULL; + deque->allocated = 0; + deque->first_element = 0; deque->maxlen = -1; - deque->numfreeblocks = 0; + deque->state = 0; deque->weakreflist = NULL; return (PyObject *)deque; } +static size_t size_min(size_t a, size_t b) { + return a < b ? a : b; +} + +// We want zero to behave like +infinity, hence the -1 and unsigned overflow. +static Py_ssize_t min3_special(Py_ssize_t a, Py_ssize_t b, Py_ssize_t c) { + return size_min(a - 1, size_min(b - 1, c - 1)) + 1; +} + +static void circular_mem_move(PyObject **items, Py_ssize_t allocated, Py_ssize_t dst_start, Py_ssize_t src_start, Py_ssize_t m) { + assert(m <= allocated); + Py_ssize_t mask = allocated - 1; + + // move backwards: + if (((src_start - dst_start) & mask) < m) { + Py_ssize_t remaining = m; + while (remaining > 0) { + Py_ssize_t src_until_wrap = (- src_start) & mask; + Py_ssize_t dst_until_wrap = (- dst_start) & mask; + Py_ssize_t step = min3_special(src_until_wrap, dst_until_wrap, remaining); + memmove(&items[dst_start], &items[src_start], step * sizeof(PyObject *)); + remaining -= step; + assert(remaining >= 0); + assert(step > 0); + src_start = (src_start + step) & mask; + dst_start = (dst_start + step) & mask; + } + } + // move forwards: + else { + Py_ssize_t remaining = m; + Py_ssize_t src_end = (src_start + remaining) & mask; + Py_ssize_t dst_end = (dst_start + remaining) & mask; + while (remaining > 0) { + Py_ssize_t step = min3_special(src_end, dst_end, remaining); + memmove(&items[(dst_end-step) & mask], &items[(src_end-step) & mask], step * sizeof(PyObject *)); + remaining -= step; + assert(remaining >= 0); + assert(step > 0); + src_end = (src_end - step) & mask; + dst_end = (dst_end - step) & mask; + } + } +} + +static int +deque_ensure_capacity(dequeobject *deque, Py_ssize_t min_needed) +{ + Py_ssize_t allocated = deque->allocated; + if (allocated >= min_needed && allocated != 0) { + return 0; + } + + Py_ssize_t new_allocated = (allocated > 0) ? allocated : 1; + while (new_allocated < min_needed) { + if (new_allocated > PY_SSIZE_T_MAX / 2) { + PyErr_NoMemory(); + return -1; + } + new_allocated <<= 1; + } + + PyObject **new_items = PyMem_New(PyObject *, new_allocated); + if (new_items == NULL) { + PyErr_NoMemory(); + return -1; + } + + Py_ssize_t n = Py_SIZE(deque); + if (n > 0 && deque->ob_item != NULL && allocated > 0) { + Py_ssize_t mask = allocated - 1; + for (Py_ssize_t i = 0; i < n; i++) { + Py_ssize_t idx = (deque->first_element + i) & mask; + new_items[i] = deque->ob_item[idx]; + } + } + for (Py_ssize_t i = n; i < new_allocated; i++) { + new_items[i] = NULL; + } + + PyMem_Free(deque->ob_item); + deque->ob_item = new_items; + deque->allocated = new_allocated; + deque->first_element = 0; + return 0; +} + +static void +deque_maybe_shrink(dequeobject *deque) +{ + Py_ssize_t size = Py_SIZE(deque); + Py_ssize_t allocated = deque->allocated; + + if (size == 0) { + PyMem_Free(deque->ob_item); + deque->ob_item = NULL; + deque->allocated = 0; + deque->first_element = 0; + return; + } + + if (allocated <= 1 || size > (allocated >> 2)) { + return; + } + + Py_ssize_t target = 1; + while (target < size) { + target <<= 1; + } + if (target >= allocated) { + return; + } + + PyObject **new_items = PyMem_New(PyObject *, target); + if (new_items == NULL) { + PyErr_Clear(); + return; + } + + Py_ssize_t mask = allocated - 1; + for (Py_ssize_t i = 0; i < size; i++) { + new_items[i] = deque->ob_item[(deque->first_element + i) & mask]; + } + for (Py_ssize_t i = size; i < target; i++) { + new_items[i] = NULL; + } + + PyMem_Free(deque->ob_item); + deque->ob_item = new_items; + deque->allocated = target; + deque->first_element = 0; +} + +static int +deque_make_contiguous(dequeobject *deque) +{ + if (Py_SIZE(deque) == 0 || deque->allocated == 0 || + deque->first_element == 0) { + return 0; + } + + Py_ssize_t allocated = deque->allocated; + PyObject **new_items = PyMem_New(PyObject *, allocated); + if (new_items == NULL) { + PyErr_NoMemory(); + return -1; + } + + Py_ssize_t mask = allocated - 1; + Py_ssize_t n = Py_SIZE(deque); + for (Py_ssize_t i = 0; i < n; i++) { + Py_ssize_t idx = (deque->first_element + i) & mask; + new_items[i] = deque->ob_item[idx]; + } + for (Py_ssize_t i = n; i < allocated; i++) { + new_items[i] = NULL; + } + + PyMem_Free(deque->ob_item); + deque->ob_item = new_items; + deque->first_element = 0; + return 0; +} + +static int +deque_rotate_left_contiguous(dequeobject *deque, Py_ssize_t offset) +{ + Py_ssize_t size = Py_SIZE(deque); + if (offset <= 0 || offset >= size) { + return 0; + } + PyObject **items = deque->ob_item; + PyObject **tmp = PyMem_New(PyObject *, offset); + if (tmp == NULL) { + PyErr_NoMemory(); + return -1; + } + Py_MEMCPY(tmp, items, offset * sizeof(PyObject *)); + memmove(items, items + offset, (size - offset) * sizeof(PyObject *)); + Py_MEMCPY(items + size - offset, tmp, offset * sizeof(PyObject *)); + PyMem_Free(tmp); + return 0; +} + /*[clinic input] @critical_section _collections.deque.pop as deque_pop @@ -245,34 +308,20 @@ static PyObject * deque_pop_impl(dequeobject *deque) /*[clinic end generated code: output=2e5f7890c4251f07 input=55c5b6a8ad51d72f]*/ { - PyObject *item; - block *prevblock; - if (Py_SIZE(deque) == 0) { PyErr_SetString(PyExc_IndexError, "pop from an empty deque"); return NULL; } - item = deque->rightblock->data[deque->rightindex]; - deque->rightindex--; + + Py_ssize_t mask = deque->allocated - 1; + Py_ssize_t pos = (deque->first_element + Py_SIZE(deque) - 1) & mask; + PyObject *item = deque->ob_item[pos]; + deque->ob_item[pos] = NULL; Py_SET_SIZE(deque, Py_SIZE(deque) - 1); + deque_maybe_shrink(deque); deque->state++; - - if (deque->rightindex < 0) { - if (Py_SIZE(deque)) { - prevblock = deque->rightblock->leftlink; - assert(deque->leftblock != deque->rightblock); - freeblock(deque, deque->rightblock); - CHECK_NOT_END(prevblock); - MARK_END(prevblock->rightlink); - deque->rightblock = prevblock; - deque->rightindex = BLOCKLEN - 1; - } else { - assert(deque->leftblock == deque->rightblock); - assert(deque->leftindex == deque->rightindex+1); - /* re-center instead of freeing a block */ - deque->leftindex = CENTER + 1; - deque->rightindex = CENTER; - } + if (Py_SIZE(deque) == 0) { + deque->first_element = 0; } return item; } @@ -290,35 +339,20 @@ static PyObject * deque_popleft_impl(dequeobject *deque) /*[clinic end generated code: output=62b154897097ff68 input=1571ce88fe3053de]*/ { - PyObject *item; - block *prevblock; - if (Py_SIZE(deque) == 0) { PyErr_SetString(PyExc_IndexError, "pop from an empty deque"); return NULL; } - assert(deque->leftblock != NULL); - item = deque->leftblock->data[deque->leftindex]; - deque->leftindex++; + + Py_ssize_t mask = deque->allocated - 1; + PyObject *item = deque->ob_item[deque->first_element]; + deque->ob_item[deque->first_element] = NULL; + deque->first_element = (deque->first_element + 1) & mask; Py_SET_SIZE(deque, Py_SIZE(deque) - 1); + deque_maybe_shrink(deque); deque->state++; - - if (deque->leftindex == BLOCKLEN) { - if (Py_SIZE(deque)) { - assert(deque->leftblock != deque->rightblock); - prevblock = deque->leftblock->rightlink; - freeblock(deque, deque->leftblock); - CHECK_NOT_END(prevblock); - MARK_END(prevblock->leftlink); - deque->leftblock = prevblock; - deque->leftindex = 0; - } else { - assert(deque->leftblock == deque->rightblock); - assert(deque->leftindex == deque->rightindex+1); - /* re-center instead of freeing a block */ - deque->leftindex = CENTER + 1; - deque->rightindex = CENTER; - } + if (Py_SIZE(deque) == 0) { + deque->first_element = 0; } return item; } @@ -337,26 +371,27 @@ deque_popleft_impl(dequeobject *deque) #define NEEDS_TRIM(deque, maxlen) ((size_t)(maxlen) < (size_t)(Py_SIZE(deque))) +static int deque_grow_ensure(dequeobject *deque, Py_ssize_t min_size) +{ + if (min_size < 1) { + min_size = 1; + } + return deque_ensure_capacity(deque, min_size); +} + static inline int deque_append_lock_held(dequeobject *deque, PyObject *item, Py_ssize_t maxlen) { - if (deque->rightindex == BLOCKLEN - 1) { - block *b = newblock(deque); - if (b == NULL) - return -1; - b->leftlink = deque->rightblock; - CHECK_END(deque->rightblock->rightlink); - deque->rightblock->rightlink = b; - deque->rightblock = b; - MARK_END(b->rightlink); - deque->rightindex = -1; + if (deque_grow_ensure(deque, Py_SIZE(deque) + 1) < 0) { + return -1; } + Py_ssize_t mask = deque->allocated - 1; + Py_ssize_t pos = (deque->first_element + Py_SIZE(deque)) & mask; + deque->ob_item[pos] = Py_NewRef(item); Py_SET_SIZE(deque, Py_SIZE(deque) + 1); - deque->rightindex++; - deque->rightblock->data[deque->rightindex] = item; if (NEEDS_TRIM(deque, maxlen)) { PyObject *olditem = deque_popleft_impl(deque); - Py_DECREF(olditem); + Py_XDECREF(olditem); } else { deque->state++; } @@ -378,7 +413,7 @@ static PyObject * deque_append_impl(dequeobject *deque, PyObject *item) /*[clinic end generated code: output=9c7bcb8b599c6362 input=b0eeeb09b9f5cf18]*/ { - if (deque_append_lock_held(deque, Py_NewRef(item), deque->maxlen) < 0) + if (deque_append_lock_held(deque, item, deque->maxlen) < 0) return NULL; Py_RETURN_NONE; } @@ -387,23 +422,16 @@ static inline int deque_appendleft_lock_held(dequeobject *deque, PyObject *item, Py_ssize_t maxlen) { - if (deque->leftindex == 0) { - block *b = newblock(deque); - if (b == NULL) - return -1; - b->rightlink = deque->leftblock; - CHECK_END(deque->leftblock->leftlink); - deque->leftblock->leftlink = b; - deque->leftblock = b; - MARK_END(b->leftlink); - deque->leftindex = BLOCKLEN; + if (deque_grow_ensure(deque, Py_SIZE(deque) + 1) < 0) { + return -1; } + Py_ssize_t mask = deque->allocated - 1; + deque->first_element = (deque->first_element - 1) & mask; + deque->ob_item[deque->first_element] = Py_NewRef(item); Py_SET_SIZE(deque, Py_SIZE(deque) + 1); - deque->leftindex--; - deque->leftblock->data[deque->leftindex] = item; if (NEEDS_TRIM(deque, maxlen)) { PyObject *olditem = deque_pop_impl(deque); - Py_DECREF(olditem); + Py_XDECREF(olditem); } else { deque->state++; } @@ -425,7 +453,7 @@ static PyObject * deque_appendleft_impl(dequeobject *deque, PyObject *item) /*[clinic end generated code: output=9a192edbcd0f20db input=236c2fbceaf08e14]*/ { - if (deque_appendleft_lock_held(deque, Py_NewRef(item), deque->maxlen) < 0) + if (deque_appendleft_lock_held(deque, item, deque->maxlen) < 0) return NULL; Py_RETURN_NONE; } @@ -497,14 +525,6 @@ deque_extend_impl(dequeobject *deque, PyObject *iterable) if (maxlen == 0) return consume_iterator(it); - /* Space saving heuristic. Start filling from the left */ - if (Py_SIZE(deque) == 0) { - assert(deque->leftblock == deque->rightblock); - assert(deque->leftindex == deque->rightindex+1); - deque->leftindex = 1; - deque->rightindex = 0; - } - iternext = *Py_TYPE(it)->tp_iternext; while ((item = iternext(it)) != NULL) { if (deque_append_lock_held(deque, item, maxlen) == -1) { @@ -512,6 +532,7 @@ deque_extend_impl(dequeobject *deque, PyObject *iterable) Py_DECREF(it); return NULL; } + Py_DECREF(item); } return finalize_iterator(it); } @@ -553,14 +574,6 @@ deque_extendleft_impl(dequeobject *deque, PyObject *iterable) if (maxlen == 0) return consume_iterator(it); - /* Space saving heuristic. Start filling from the right */ - if (Py_SIZE(deque) == 0) { - assert(deque->leftblock == deque->rightblock); - assert(deque->leftindex == deque->rightindex+1); - deque->leftindex = BLOCKLEN - 1; - deque->rightindex = BLOCKLEN - 2; - } - iternext = *Py_TYPE(it)->tp_iternext; while ((item = iternext(it)) != NULL) { if (deque_appendleft_lock_held(deque, item, maxlen) == -1) { @@ -568,6 +581,7 @@ deque_extendleft_impl(dequeobject *deque, PyObject *iterable) Py_DECREF(it); return NULL; } + Py_DECREF(item); } return finalize_iterator(it); } @@ -617,7 +631,7 @@ deque_copy_impl(dequeobject *deque) * invisible to other threads. */ if (Py_SIZE(deque) == 1) { - PyObject *item = old_deque->leftblock->data[old_deque->leftindex]; + PyObject *item = old_deque->ob_item[old_deque->first_element]; rv = deque_append_impl(new_deque, item); } else { rv = deque_extend_impl(new_deque, (PyObject *)deque); @@ -633,8 +647,8 @@ deque_copy_impl(dequeobject *deque) result = PyObject_CallOneArg((PyObject *)(Py_TYPE(deque)), (PyObject *)deque); else - result = PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi", - deque, old_deque->maxlen, NULL); + result = PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "On", + deque, old_deque->maxlen); if (result != NULL && !PyObject_TypeCheck(result, state->deque_type)) { PyErr_Format(PyExc_TypeError, "%.200s() must return a deque, not %.200s", @@ -705,85 +719,27 @@ deque_concat(PyObject *self, PyObject *other) static int deque_clear(PyObject *self) { - block *b; - block *prevblock; - block *leftblock; - Py_ssize_t leftindex; - Py_ssize_t n, m; - PyObject *item; - PyObject **itemptr, **limit; dequeobject *deque = dequeobject_CAST(self); + if (deque->ob_item == NULL) + return 0; /* already cleared */ - if (Py_SIZE(deque) == 0) - return 0; - - /* During the process of clearing a deque, decrefs can cause the - deque to mutate. To avoid fatal confusion, we have to make the - deque empty before clearing the blocks and never refer to - anything via deque->ref while clearing. (This is the same - technique used for clearing lists, sets, and dicts.) - - Making the deque empty requires allocating a new empty block. In - the unlikely event that memory is full, we fall back to an - alternate method that doesn't require a new block. Repeating - pops in a while-loop is slower, possibly re-entrant (and a clever - adversary could cause it to never terminate). - */ + /* decref all elements */ + Py_ssize_t n = Py_SIZE(deque); + Py_ssize_t mask = deque->allocated - 1; + Py_ssize_t idx = deque->first_element; - b = newblock(deque); - if (b == NULL) { - PyErr_Clear(); - goto alternate_method; + for (Py_ssize_t k = 0; k < n; k++, idx++) { + Py_CLEAR(deque->ob_item[idx & mask]); /* safe even if element NULL */ } - /* Remember the old size, leftblock, and leftindex */ - n = Py_SIZE(deque); - leftblock = deque->leftblock; - leftindex = deque->leftindex; - - /* Set the deque to be empty using the newly allocated block */ - MARK_END(b->leftlink); - MARK_END(b->rightlink); + /* reset header */ + PyMem_Free(deque->ob_item); + deque->ob_item = NULL; + deque->allocated = 0; + deque->first_element = 0; Py_SET_SIZE(deque, 0); - deque->leftblock = b; - deque->rightblock = b; - deque->leftindex = CENTER + 1; - deque->rightindex = CENTER; deque->state++; - /* Now the old size, leftblock, and leftindex are disconnected from - the empty deque and we can use them to decref the pointers. - */ - m = (BLOCKLEN - leftindex > n) ? n : BLOCKLEN - leftindex; - itemptr = &leftblock->data[leftindex]; - limit = itemptr + m; - n -= m; - while (1) { - if (itemptr == limit) { - if (n == 0) - break; - CHECK_NOT_END(leftblock->rightlink); - prevblock = leftblock; - leftblock = leftblock->rightlink; - m = (n > BLOCKLEN) ? BLOCKLEN : n; - itemptr = leftblock->data; - limit = itemptr + m; - n -= m; - freeblock(deque, prevblock); - } - item = *(itemptr++); - Py_DECREF(item); - } - CHECK_END(leftblock->rightlink); - freeblock(deque, leftblock); - return 0; - - alternate_method: - while (Py_SIZE(deque)) { - item = deque_pop_impl(deque); - assert (item != NULL); - Py_DECREF(item); - } return 0; } @@ -807,78 +763,74 @@ deque_clearmethod_impl(dequeobject *deque) static PyObject * deque_inplace_repeat_lock_held(dequeobject *deque, Py_ssize_t n) { - Py_ssize_t i, m, size; - PyObject *seq; - PyObject *rv; - - size = Py_SIZE(deque); + Py_ssize_t size = Py_SIZE(deque); if (size == 0 || n == 1) { return Py_NewRef(deque); } if (n <= 0) { - (void)deque_clear((PyObject *)deque); + deque_clear((PyObject *)deque); return Py_NewRef(deque); } - if (size == 1) { - /* common case, repeating a single element */ - PyObject *item = deque->leftblock->data[deque->leftindex]; - - if (deque->maxlen >= 0 && n > deque->maxlen) - n = deque->maxlen; + if (size != 0 && n > PY_SSIZE_T_MAX / size) { + PyErr_NoMemory(); + return NULL; + } - deque->state++; - for (i = 0 ; i < n-1 ; ) { - if (deque->rightindex == BLOCKLEN - 1) { - block *b = newblock(deque); - if (b == NULL) { - Py_SET_SIZE(deque, Py_SIZE(deque) + i); - return NULL; - } - b->leftlink = deque->rightblock; - CHECK_END(deque->rightblock->rightlink); - deque->rightblock->rightlink = b; - deque->rightblock = b; - MARK_END(b->rightlink); - deque->rightindex = -1; - } - m = n - 1 - i; - if (m > BLOCKLEN - 1 - deque->rightindex) - m = BLOCKLEN - 1 - deque->rightindex; - i += m; - while (m--) { - deque->rightindex++; - deque->rightblock->data[deque->rightindex] = Py_NewRef(item); - } + Py_ssize_t full_len = size * n; + Py_ssize_t drop = 0; + if (deque->maxlen >= 0 && full_len > deque->maxlen) { + drop = full_len - deque->maxlen; + full_len = deque->maxlen; + if (full_len <= 0) { + deque_clear((PyObject *)deque); + return Py_NewRef(deque); } - Py_SET_SIZE(deque, Py_SIZE(deque) + i); - return Py_NewRef(deque); } - if ((size_t)size > PY_SSIZE_T_MAX / (size_t)n) { - return PyErr_NoMemory(); + Py_ssize_t skip_full = drop / size; + Py_ssize_t offset = drop % size; + Py_ssize_t repeats = n - skip_full; + if (repeats <= 0) { + deque_clear((PyObject *)deque); + return Py_NewRef(deque); } + Py_ssize_t full_output = size * repeats; + Py_ssize_t final_size = full_output - offset; + assert(final_size == full_len); - seq = PySequence_List((PyObject *)deque); - if (seq == NULL) - return seq; - - /* Reduce the number of repetitions when maxlen would be exceeded */ - if (deque->maxlen >= 0 && n * size > deque->maxlen) - n = (deque->maxlen + size - 1) / size; + if (deque_ensure_capacity(deque, full_output) < 0) { + return NULL; + } + if (deque_make_contiguous(deque) < 0) { + return NULL; + } + if (offset && deque_rotate_left_contiguous(deque, offset) < 0) { + return NULL; + } - for (i = 0 ; i < n-1 ; i++) { - rv = deque_extend_impl(deque, seq); - if (rv == NULL) { - Py_DECREF(seq); - return NULL; + PyObject **items = deque->ob_item; + if (repeats > 1) { + for (Py_ssize_t j = 0; j < size; j++) { + _Py_RefcntAdd(items[j], repeats - 1); } - Py_DECREF(rv); + _Py_memory_repeat((char *)items, + sizeof(PyObject *) * full_output, + sizeof(PyObject *) * size); } - Py_INCREF(deque); - Py_DECREF(seq); - return (PyObject *)deque; + + Py_ssize_t drop_from_end = full_output - final_size; + for (Py_ssize_t i = 0; i < drop_from_end; i++) { + Py_ssize_t idx = final_size + i; + Py_DECREF(items[idx]); + items[idx] = NULL; + } + + Py_SET_SIZE(deque, final_size); + deque->first_element = 0; + deque->state++; + return Py_NewRef(deque); } static PyObject * @@ -911,42 +863,10 @@ deque_repeat(PyObject *self, Py_ssize_t n) return rv; } -/* The rotate() method is part of the public API and is used internally -as a primitive for other methods. - -Rotation by 1 or -1 is a common case, so any optimizations for high -volume rotations should take care not to penalize the common case. - -Conceptually, a rotate by one is equivalent to a pop on one side and an -append on the other. However, a pop/append pair is unnecessarily slow -because it requires an incref/decref pair for an object located randomly -in memory. It is better to just move the object pointer from one block -to the next without changing the reference count. - -When moving batches of pointers, it is tempting to use memcpy() but that -proved to be slower than a simple loop for a variety of reasons. -Memcpy() cannot know in advance that we're copying pointers instead of -bytes, that the source and destination are pointer aligned and -non-overlapping, that moving just one pointer is a common case, that we -never need to move more than BLOCKLEN pointers, and that at least one -pointer is always moved. - -For high volume rotations, newblock() and freeblock() are never called -more than once. Previously emptied blocks are immediately reused as a -destination block. If a block is left-over at the end, it is freed. -*/ - static int _deque_rotate(dequeobject *deque, Py_ssize_t n) { - block *b = NULL; - block *leftblock = deque->leftblock; - block *rightblock = deque->rightblock; - Py_ssize_t leftindex = deque->leftindex; - Py_ssize_t rightindex = deque->rightindex; Py_ssize_t len=Py_SIZE(deque), halflen=len>>1; - int rv = -1; - if (len <= 1) return 0; if (n > halflen || n < -halflen) { @@ -958,106 +878,38 @@ _deque_rotate(dequeobject *deque, Py_ssize_t n) } assert(len > 1); assert(-halflen <= n && n <= halflen); + if (n == 0) { + return 0; + } - deque->state++; - while (n > 0) { - if (leftindex == 0) { - if (b == NULL) { - b = newblock(deque); - if (b == NULL) - goto done; - } - b->rightlink = leftblock; - CHECK_END(leftblock->leftlink); - leftblock->leftlink = b; - leftblock = b; - MARK_END(b->leftlink); - leftindex = BLOCKLEN; - b = NULL; - } - assert(leftindex > 0); - { - PyObject **src, **dest; - Py_ssize_t m = n; - - if (m > rightindex + 1) - m = rightindex + 1; - if (m > leftindex) - m = leftindex; - assert (m > 0 && m <= len); - rightindex -= m; - leftindex -= m; - src = &rightblock->data[rightindex + 1]; - dest = &leftblock->data[leftindex]; - n -= m; - do { - *(dest++) = *(src++); - } while (--m); - } - if (rightindex < 0) { - assert(leftblock != rightblock); - assert(b == NULL); - b = rightblock; - CHECK_NOT_END(rightblock->leftlink); - rightblock = rightblock->leftlink; - MARK_END(rightblock->rightlink); - rightindex = BLOCKLEN - 1; - } + PyObject **items = deque->ob_item; + Py_ssize_t first = deque->first_element; + Py_ssize_t allocated = deque->allocated; + Py_ssize_t mask = allocated - 1; // Since allocated is a power of 2 + + if (len == allocated) { + // Special case: we only need to move the first element index. + deque->first_element = (first - n) & mask; + return 0; } - while (n < 0) { - if (rightindex == BLOCKLEN - 1) { - if (b == NULL) { - b = newblock(deque); - if (b == NULL) - goto done; - } - b->leftlink = rightblock; - CHECK_END(rightblock->rightlink); - rightblock->rightlink = b; - rightblock = b; - MARK_END(b->rightlink); - rightindex = -1; - b = NULL; - } - assert (rightindex < BLOCKLEN - 1); - { - PyObject **src, **dest; - Py_ssize_t m = -n; - - if (m > BLOCKLEN - leftindex) - m = BLOCKLEN - leftindex; - if (m > BLOCKLEN - 1 - rightindex) - m = BLOCKLEN - 1 - rightindex; - assert (m > 0 && m <= len); - src = &leftblock->data[leftindex]; - dest = &rightblock->data[rightindex + 1]; - leftindex += m; - rightindex += m; - n += m; - do { - *(dest++) = *(src++); - } while (--m); - } - if (leftindex == BLOCKLEN) { - assert(leftblock != rightblock); - assert(b == NULL); - b = leftblock; - CHECK_NOT_END(leftblock->rightlink); - leftblock = leftblock->rightlink; - MARK_END(leftblock->leftlink); - leftindex = 0; - } + + // For positive rotation, we move elements from end to beginning + if (n > 0) { + Py_ssize_t src_start = (first + len - n) & mask; + Py_ssize_t dst_start = (first - n) & mask; + circular_mem_move(items, allocated, dst_start, src_start, n); + deque->first_element = (first - n) & mask; + } + // For negative rotation, we move elements from beginning to end + else { + Py_ssize_t src_start = first; + Py_ssize_t dst_start = (first + len) & mask; + circular_mem_move(items, allocated, dst_start, src_start, -n); + deque->first_element = (first - n) & mask; } - rv = 0; -done: - if (b != NULL) - freeblock(deque, b); - deque->leftblock = leftblock; - deque->rightblock = rightblock; - deque->leftindex = leftindex; - deque->rightindex = rightindex; - return rv; + deque->state++; + return 0; } /*[clinic input] @@ -1065,18 +917,71 @@ _deque_rotate(dequeobject *deque, Py_ssize_t n) _collections.deque.rotate as deque_rotate deque: dequeobject - n: Py_ssize_t = 1 + n: object(py_default="1") = NULL / Rotate the deque n steps to the right. If n is negative, rotates left. [clinic start generated code]*/ static PyObject * -deque_rotate_impl(dequeobject *deque, Py_ssize_t n) -/*[clinic end generated code: output=96c2402a371eb15d input=5bf834296246e002]*/ +deque_rotate_impl(dequeobject *deque, PyObject *n) +/*[clinic end generated code: output=8f7784d674cced93 input=2a97f0aa4801e67d]*/ { - if (!_deque_rotate(deque, n)) + Py_ssize_t len = Py_SIZE(deque); + Py_ssize_t steps = 1; + PyObject *index_obj = NULL; + PyObject *arg = n ? n : _PyLong_GetOne(); + + if (len <= 1) { Py_RETURN_NONE; + } + + index_obj = PyNumber_Index(arg); + if (index_obj == NULL) { + return NULL; + } + + steps = PyLong_AsSsize_t(index_obj); + if (steps == -1 && PyErr_Occurred()) { + if (!PyErr_ExceptionMatches(PyExc_OverflowError)) { + Py_DECREF(index_obj); + return NULL; + } + PyErr_Clear(); + PyObject *len_obj = PyLong_FromSsize_t(len); + if (len_obj == NULL) { + Py_DECREF(index_obj); + return NULL; + } + PyObject *remainder = PyNumber_Remainder(index_obj, len_obj); + Py_DECREF(len_obj); + if (remainder == NULL) { + Py_DECREF(index_obj); + return NULL; + } + steps = PyLong_AsSsize_t(remainder); + Py_DECREF(remainder); + if (steps == -1 && PyErr_Occurred()) { + Py_DECREF(index_obj); + return NULL; + } + if (steps != 0) { + int negative = PyObject_RichCompareBool( + index_obj, _PyLong_GetZero(), Py_LT); + if (negative < 0) { + Py_DECREF(index_obj); + return NULL; + } + if (negative) { + steps -= len; + } + } + } + Py_DECREF(index_obj); + + if (!_deque_rotate(deque, steps)) { + Py_RETURN_NONE; + } return NULL; } @@ -1093,38 +998,33 @@ static PyObject * deque_reverse_impl(dequeobject *deque) /*[clinic end generated code: output=bdeebc2cf8c1f064 input=26f4167fd623027f]*/ { - block *leftblock = deque->leftblock; - block *rightblock = deque->rightblock; - Py_ssize_t leftindex = deque->leftindex; - Py_ssize_t rightindex = deque->rightindex; - Py_ssize_t n = Py_SIZE(deque) >> 1; - PyObject *tmp; + Py_ssize_t size = Py_SIZE(deque); + if (size <= 1) { + Py_RETURN_NONE; + } - while (--n >= 0) { - /* Validate that pointers haven't met in the middle */ - assert(leftblock != rightblock || leftindex < rightindex); - CHECK_NOT_END(leftblock); - CHECK_NOT_END(rightblock); - - /* Swap */ - tmp = leftblock->data[leftindex]; - leftblock->data[leftindex] = rightblock->data[rightindex]; - rightblock->data[rightindex] = tmp; - - /* Advance left block/index pair */ - leftindex++; - if (leftindex == BLOCKLEN) { - leftblock = leftblock->rightlink; - leftindex = 0; - } + Py_ssize_t first = deque->first_element; + Py_ssize_t allocated = deque->allocated; + Py_ssize_t mask = allocated - 1; + PyObject **items = deque->ob_item; - /* Step backwards with the right block/index pair */ - rightindex--; - if (rightindex < 0) { - rightblock = rightblock->leftlink; - rightindex = BLOCKLEN - 1; - } + // Calculate the last element's position + Py_ssize_t last = (first + size - 1) & mask; + + // Swap elements from both ends until we meet in the middle + Py_ssize_t count = size / 2; + while (count--) { + // Swap elements + PyObject *temp = items[first]; + items[first] = items[last]; + items[last] = temp; + + // Move indices, handling wrap-around + first = (first + 1) & mask; + last = (last - 1) & mask; } + + deque->state++; Py_RETURN_NONE; } @@ -1143,17 +1043,20 @@ static PyObject * deque_count_impl(dequeobject *deque, PyObject *v) /*[clinic end generated code: output=2ca26c49b6ab0400 input=4ef67ef2b34dc1fc]*/ { - block *b = deque->leftblock; - Py_ssize_t index = deque->leftindex; Py_ssize_t n = Py_SIZE(deque); Py_ssize_t count = 0; + if (n == 0) { + return PyLong_FromLong(0); + } size_t start_state = deque->state; + Py_ssize_t first = deque->first_element; + Py_ssize_t mask = deque->allocated - 1; + PyObject **items = deque->ob_item; PyObject *item; int cmp; while (--n >= 0) { - CHECK_NOT_END(b); - item = Py_NewRef(b->data[index]); + item = Py_NewRef(items[first]); cmp = PyObject_RichCompareBool(item, v, Py_EQ); Py_DECREF(item); if (cmp < 0) @@ -1166,12 +1069,8 @@ deque_count_impl(dequeobject *deque, PyObject *v) return NULL; } - /* Advance left block/index pair */ - index++; - if (index == BLOCKLEN) { - b = b->rightlink; - index = 0; - } + /* Advance to next element, handling wrap-around */ + first = (first + 1) & mask; } return PyLong_FromSsize_t(count); } @@ -1179,16 +1078,19 @@ deque_count_impl(dequeobject *deque, PyObject *v) static int deque_contains_lock_held(dequeobject *deque, PyObject *v) { - block *b = deque->leftblock; - Py_ssize_t index = deque->leftindex; Py_ssize_t n = Py_SIZE(deque); + if (n == 0) { + return 0; + } size_t start_state = deque->state; + Py_ssize_t first = deque->first_element; + Py_ssize_t mask = deque->allocated - 1; + PyObject **items = deque->ob_item; PyObject *item; int cmp; while (--n >= 0) { - CHECK_NOT_END(b); - item = Py_NewRef(b->data[index]); + item = Py_NewRef(items[first]); cmp = PyObject_RichCompareBool(item, v, Py_EQ); Py_DECREF(item); if (cmp) { @@ -1199,11 +1101,7 @@ deque_contains_lock_held(dequeobject *deque, PyObject *v) "deque mutated during iteration"); return -1; } - index++; - if (index == BLOCKLEN) { - b = b->rightlink; - index = 0; - } + first = (first + 1) & mask; } return 0; } @@ -1247,10 +1145,11 @@ deque_index_impl(dequeobject *deque, PyObject *v, Py_ssize_t start, Py_ssize_t stop) /*[clinic end generated code: output=df45132753175ef9 input=90f48833a91e1743]*/ { - Py_ssize_t i, n; + Py_ssize_t n; PyObject *item; - block *b = deque->leftblock; - Py_ssize_t index = deque->leftindex; + Py_ssize_t first = deque->first_element; + Py_ssize_t mask = deque->allocated - 1; // Since allocated is a power of 2 + PyObject **items = deque->ob_item; size_t start_state = deque->state; int cmp; @@ -1270,21 +1169,12 @@ deque_index_impl(dequeobject *deque, PyObject *v, Py_ssize_t start, start = stop; assert(0 <= start && start <= stop && stop <= Py_SIZE(deque)); - for (i=0 ; i < start - BLOCKLEN ; i += BLOCKLEN) { - b = b->rightlink; - } - for ( ; i < start ; i++) { - index++; - if (index == BLOCKLEN) { - b = b->rightlink; - index = 0; - } - } + // Calculate the current position in the ring buffer + Py_ssize_t current_item = (first + start) & mask; - n = stop - i; + n = stop - start; while (--n >= 0) { - CHECK_NOT_END(b); - item = Py_NewRef(b->data[index]); + item = Py_NewRef(items[current_item]); cmp = PyObject_RichCompareBool(item, v, Py_EQ); Py_DECREF(item); if (cmp > 0) @@ -1296,24 +1186,12 @@ deque_index_impl(dequeobject *deque, PyObject *v, Py_ssize_t start, "deque mutated during iteration"); return NULL; } - index++; - if (index == BLOCKLEN) { - b = b->rightlink; - index = 0; - } + current_item = (current_item + 1) & mask; } PyErr_SetString(PyExc_ValueError, "deque.index(x): x not in deque"); return NULL; } -/* insert(), remove(), and delitem() are implemented in terms of - rotate() for simplicity and reasonable performance near the end - points. If for some reason these methods become popular, it is not - hard to re-implement this using direct data movement (similar to - the code used in list slice assignments) and achieve a performance - boost (by moving each pointer only once instead of twice). -*/ - /*[clinic input] @critical_section _collections.deque.insert as deque_insert @@ -1331,75 +1209,144 @@ deque_insert_impl(dequeobject *deque, Py_ssize_t index, PyObject *value) /*[clinic end generated code: output=ef4d2c15d5532b80 input=dbee706586cc9cde]*/ { Py_ssize_t n = Py_SIZE(deque); - PyObject *rv; - if (deque->maxlen == Py_SIZE(deque)) { - PyErr_SetString(PyExc_IndexError, "deque already at its maximum size"); - return NULL; + // Handle negative indices + if (index < 0) { + index += n; + if (index < 0) + index = 0; } - if (index >= n) - return deque_append_impl(deque, value); - if (index <= -n || index == 0) - return deque_appendleft_impl(deque, value); - if (_deque_rotate(deque, -index)) - return NULL; - if (index < 0) - rv = deque_append_impl(deque, value); - else - rv = deque_appendleft_impl(deque, value); - if (rv == NULL) + if (index > n) + index = n; + + if (deque->maxlen >= 0 && Py_SIZE(deque) >= deque->maxlen) { + PyErr_SetString(PyExc_IndexError, + "deque already at its maximum size"); return NULL; - Py_DECREF(rv); - if (_deque_rotate(deque, index)) + } + + if (deque_grow_ensure(deque, n + 1) < 0) { return NULL; + } + Py_ssize_t first = deque->first_element; + Py_ssize_t mask = deque->allocated - 1; + PyObject **items = deque->ob_item; + + Py_ssize_t left_count = index; + Py_ssize_t right_count = n - index; + Py_ssize_t insert_pos; + + if (left_count <= right_count) { + first = (first - 1) & mask; + deque->first_element = first; + insert_pos = (first + index) & mask; + for (Py_ssize_t k = 0; k < left_count; k++) { + Py_ssize_t src = (first + k + 1) & mask; + Py_ssize_t dst = (first + k) & mask; + items[dst] = items[src]; + } + } + else { + insert_pos = (first + index) & mask; + for (Py_ssize_t k = right_count; k > 0; k--) { + Py_ssize_t src = (insert_pos + k - 1) & mask; + Py_ssize_t dst = (src + 1) & mask; + items[dst] = items[src]; + } + } + + items[insert_pos] = Py_NewRef(value); + Py_SET_SIZE(deque, n + 1); + deque->state++; Py_RETURN_NONE; } -static int -valid_index(Py_ssize_t i, Py_ssize_t limit) + +static PyObject * +deque_slice_lock_held(dequeobject *deque, PyObject *slice) { - /* The cast to size_t lets us use just a single comparison - to check whether i is in the range: 0 <= i < limit */ - return (size_t) i < (size_t) limit; + Py_ssize_t start, stop, step, slicelength; + if (PySlice_GetIndicesEx(slice, Py_SIZE(deque), + &start, &stop, &step, &slicelength) < 0) { + return NULL; + } + + collections_state *state = find_module_state_by_def(Py_TYPE(deque)); + Py_ssize_t mask = deque->allocated ? deque->allocated - 1 : 0; + + if (Py_IS_TYPE(deque, state->deque_type)) { + dequeobject *new_deque; + new_deque = (dequeobject *)deque_new(state->deque_type, NULL, NULL); + if (new_deque == NULL) { + return NULL; + } + new_deque->maxlen = deque->maxlen; + if (slicelength == 0) { + return (PyObject *)new_deque; + } + if (deque_ensure_capacity(new_deque, slicelength) < 0) { + Py_DECREF(new_deque); + return NULL; + } + Py_ssize_t index = start; + for (Py_ssize_t i = 0; i < slicelength; i++, index += step) { + Py_ssize_t pos = (deque->first_element + index) & mask; + PyObject *value = deque->ob_item[pos]; + new_deque->ob_item[i] = Py_NewRef(value); + } + Py_SET_SIZE(new_deque, slicelength); + return (PyObject *)new_deque; + } + + PyObject *values = PyList_New(slicelength); + if (values == NULL) { + return NULL; + } + Py_ssize_t index = start; + for (Py_ssize_t i = 0; i < slicelength; i++, index += step) { + Py_ssize_t pos = (deque->first_element + index) & mask; + PyObject *value = deque->ob_item[pos]; + PyList_SET_ITEM(values, i, Py_NewRef(value)); + } + + PyObject *result; + if (deque->maxlen < 0) { + result = PyObject_CallOneArg((PyObject *)(Py_TYPE(deque)), values); + } + else { + result = PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "On", + values, deque->maxlen); + } + Py_DECREF(values); + if (result != NULL && !PyObject_TypeCheck(result, state->deque_type)) { + PyErr_Format(PyExc_TypeError, + "%.200s() must return a deque, not %.200s", + Py_TYPE(deque)->tp_name, Py_TYPE(result)->tp_name); + Py_DECREF(result); + return NULL; + } + return result; } static PyObject * deque_item_lock_held(dequeobject *deque, Py_ssize_t i) { - block *b; - PyObject *item; - Py_ssize_t n, index=i; + Py_ssize_t n = Py_SIZE(deque); + Py_ssize_t first = deque->first_element; + Py_ssize_t mask = deque->allocated - 1; // Since allocated is a power of 2 + PyObject **items = deque->ob_item; - if (!valid_index(i, Py_SIZE(deque))) { + if (i < 0) { + i += n; + } + if (!valid_index(i, n)) { PyErr_SetString(PyExc_IndexError, "deque index out of range"); return NULL; } - if (i == 0) { - i = deque->leftindex; - b = deque->leftblock; - } else if (i == Py_SIZE(deque) - 1) { - i = deque->rightindex; - b = deque->rightblock; - } else { - i += deque->leftindex; - n = (Py_ssize_t)((size_t) i / BLOCKLEN); - i = (Py_ssize_t)((size_t) i % BLOCKLEN); - if (index < (Py_SIZE(deque) >> 1)) { - b = deque->leftblock; - while (--n >= 0) - b = b->rightlink; - } else { - n = (Py_ssize_t)( - ((size_t)(deque->leftindex + Py_SIZE(deque) - 1)) - / BLOCKLEN - n); - b = deque->rightblock; - while (--n >= 0) - b = b->leftlink; - } - } - item = b->data[i]; - return Py_NewRef(item); + // Calculate the actual position in the ring buffer + Py_ssize_t pos = (first + i) & mask; + return Py_NewRef(items[pos]); } static PyObject * @@ -1413,20 +1360,77 @@ deque_item(PyObject *self, Py_ssize_t i) return result; } +static PyObject * +deque_subscript(PyObject *self, PyObject *item) +{ + dequeobject *deque = dequeobject_CAST(self); + PyObject *result = NULL; + + Py_BEGIN_CRITICAL_SECTION(deque); + if (_PyIndex_Check(item)) { + Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); + if (i == -1 && PyErr_Occurred()) { + goto done; + } + result = deque_item_lock_held(deque, i); + goto done; + } + if (PySlice_Check(item)) { + result = deque_slice_lock_held(deque, item); + goto done; + } + PyErr_Format(PyExc_TypeError, + "deque indices must be integers or slices, not %.200s", + Py_TYPE(item)->tp_name); +done: + ; // keep label legal even when Py_END_CRITICAL_SECTION() is empty + Py_END_CRITICAL_SECTION(); + return result; +} + static int deque_del_item(dequeobject *deque, Py_ssize_t i) { - PyObject *item; - int rv; + Py_ssize_t n = Py_SIZE(deque); + Py_ssize_t first = deque->first_element; + Py_ssize_t mask = deque->allocated - 1; + PyObject **items = deque->ob_item; - assert (i >= 0 && i < Py_SIZE(deque)); - if (_deque_rotate(deque, -i)) + if (!valid_index(i, n)) { + PyErr_SetString(PyExc_IndexError, "deque index out of range"); return -1; - item = deque_popleft_impl(deque); - rv = _deque_rotate(deque, i); - assert (item != NULL); + } + + Py_ssize_t pos = (first + i) & mask; + PyObject *item = items[pos]; Py_DECREF(item); - return rv; + + Py_ssize_t left_count = i; + Py_ssize_t right_count = n - i - 1; + + if (left_count <= right_count) { + for (Py_ssize_t k = left_count; k > 0; k--) { + Py_ssize_t src = (first + k - 1) & mask; + Py_ssize_t dst = (src + 1) & mask; + items[dst] = items[src]; + } + items[first] = NULL; + deque->first_element = (first + 1) & mask; + } + else { + for (Py_ssize_t k = 0; k < right_count; k++) { + Py_ssize_t src = (pos + k + 1) & mask; + Py_ssize_t dst = (src - 1) & mask; + items[dst] = items[src]; + } + Py_ssize_t last = (first + n - 1) & mask; + items[last] = NULL; + } + + Py_SET_SIZE(deque, n - 1); + deque->state++; + deque_maybe_shrink(deque); + return 0; } /*[clinic input] @@ -1444,73 +1448,57 @@ static PyObject * deque_remove_impl(dequeobject *deque, PyObject *value) /*[clinic end generated code: output=54cff28b8ef78c5b input=60eb3f8aa4de532a]*/ { - PyObject *item; - block *b = deque->leftblock; - Py_ssize_t i, n = Py_SIZE(deque), index = deque->leftindex; + Py_ssize_t n = Py_SIZE(deque); + Py_ssize_t first = deque->first_element; + Py_ssize_t mask = deque->allocated - 1; // Since allocated is a power of 2 + PyObject **items = deque->ob_item; + Py_ssize_t i; + int cmp; size_t start_state = deque->state; - int cmp, rv; - for (i = 0 ; i < n; i++) { - item = Py_NewRef(b->data[index]); + for (i = 0; i < n; i++) { + PyObject *item = Py_NewRef(items[(first + i) & mask]); + if (item == NULL) { + return NULL; + } cmp = PyObject_RichCompareBool(item, value, Py_EQ); Py_DECREF(item); - if (cmp < 0) { - return NULL; + if (cmp > 0) { + if (deque_del_item(deque, i) < 0) + return NULL; + Py_RETURN_NONE; } + if (cmp < 0) + return NULL; if (start_state != deque->state) { PyErr_SetString(PyExc_IndexError, "deque mutated during iteration"); return NULL; } - if (cmp > 0) { - break; - } - index++; - if (index == BLOCKLEN) { - b = b->rightlink; - index = 0; - } - } - if (i == n) { - PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque"); - return NULL; } - rv = deque_del_item(deque, i); - if (rv == -1) { - return NULL; - } - Py_RETURN_NONE; + PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque"); + return NULL; } static int deque_ass_item_lock_held(dequeobject *deque, Py_ssize_t i, PyObject *v) { - block *b; - Py_ssize_t n, len=Py_SIZE(deque), halflen=(len+1)>>1, index=i; + Py_ssize_t n = Py_SIZE(deque); + Py_ssize_t first = deque->first_element; + Py_ssize_t mask = deque->allocated - 1; // Since allocated is a power of 2 + PyObject **items = deque->ob_item; - if (!valid_index(i, len)) { + if (!valid_index(i, n)) { PyErr_SetString(PyExc_IndexError, "deque index out of range"); return -1; } - if (v == NULL) + if (v == NULL) { + // Delete item return deque_del_item(deque, i); - - i += deque->leftindex; - n = (Py_ssize_t)((size_t) i / BLOCKLEN); - i = (Py_ssize_t)((size_t) i % BLOCKLEN); - if (index <= halflen) { - b = deque->leftblock; - while (--n >= 0) - b = b->rightlink; - } else { - n = (Py_ssize_t)( - ((size_t)(deque->leftindex + Py_SIZE(deque) - 1)) - / BLOCKLEN - n); - b = deque->rightblock; - while (--n >= 0) - b = b->leftlink; - } - Py_SETREF(b->data[i], Py_NewRef(v)); + } + // Set item + Py_ssize_t pos = (first + i) & mask; + Py_SETREF(items[pos], Py_NewRef(v)); return 0; } @@ -1529,48 +1517,25 @@ static void deque_dealloc(PyObject *self) { dequeobject *deque = dequeobject_CAST(self); - PyTypeObject *tp = Py_TYPE(deque); - Py_ssize_t i; - - PyObject_GC_UnTrack(deque); + PyObject_GC_UnTrack(self); /* stop GC from revisiting us */ + deque_clear(self); /* drop elements (once only) */ FT_CLEAR_WEAKREFS(self, deque->weakreflist); - if (deque->leftblock != NULL) { - (void)deque_clear(self); - assert(deque->leftblock != NULL); - freeblock(deque, deque->leftblock); - } - deque->leftblock = NULL; - deque->rightblock = NULL; - for (i=0 ; i < deque->numfreeblocks ; i++) { - PyMem_Free(deque->freeblocks[i]); - } - tp->tp_free(deque); - Py_DECREF(tp); + Py_TYPE(self)->tp_free(self); } static int deque_traverse(PyObject *self, visitproc visit, void *arg) { dequeobject *deque = dequeobject_CAST(self); - Py_VISIT(Py_TYPE(deque)); - block *b; - PyObject *item; - Py_ssize_t index; - Py_ssize_t indexlo = deque->leftindex; - Py_ssize_t indexhigh; + Py_ssize_t n = Py_SIZE(deque); - for (b = deque->leftblock; b != deque->rightblock; b = b->rightlink) { - for (index = indexlo; index < BLOCKLEN ; index++) { - item = b->data[index]; - Py_VISIT(item); - } - indexlo = 0; - } - indexhigh = deque->rightindex; - for (index = indexlo; index <= indexhigh; index++) { - item = b->data[index]; - Py_VISIT(item); + PyObject **buf = deque->ob_item; + Py_ssize_t mask = deque->allocated - 1; + Py_ssize_t i = deque->first_element; + for (Py_ssize_t k = 0; k < n; k++) { + Py_VISIT(buf[i]); + i = (i + 1) & mask; } return 0; } @@ -1646,80 +1611,81 @@ deque_repr(PyObject *deque) static PyObject * deque_richcompare(PyObject *v, PyObject *w, int op) { - PyObject *it1=NULL, *it2=NULL, *x, *y; - Py_ssize_t vs, ws; - int b, cmp=-1; + dequeobject *deque1 = dequeobject_CAST(v); + dequeobject *deque2 = dequeobject_CAST(w); + Py_ssize_t i, len1, len2; - collections_state *state = find_module_state_by_def(Py_TYPE(v)); - if (!PyObject_TypeCheck(v, state->deque_type) || - !PyObject_TypeCheck(w, state->deque_type)) { + if (v == w) { + if (op == Py_EQ) { + Py_RETURN_TRUE; + } + if (op == Py_NE) { + Py_RETURN_FALSE; + } + } + if (!PyObject_TypeCheck(w, Py_TYPE(v))) { Py_RETURN_NOTIMPLEMENTED; } - /* Shortcuts */ - vs = Py_SIZE(v); - ws = Py_SIZE(w); - if (op == Py_EQ) { - if (v == w) - Py_RETURN_TRUE; - if (vs != ws) + len1 = Py_SIZE(deque1); + len2 = Py_SIZE(deque2); + + if (len1 != len2 && (op == Py_EQ || op == Py_NE)) { + /* Shortcut: if the lengths differ, the deques are different */ + if (op == Py_EQ) Py_RETURN_FALSE; + else + Py_RETURN_TRUE; } - if (op == Py_NE) { - if (v == w) + Py_ssize_t min_len = len1 < len2 ? len1 : len2; + Py_ssize_t first1 = deque1->first_element; + Py_ssize_t first2 = deque2->first_element; + Py_ssize_t mask1 = deque1->allocated ? deque1->allocated - 1 : 0; + Py_ssize_t mask2 = deque2->allocated ? deque2->allocated - 1 : 0; + + for (i = 0; i < min_len; i++) { + PyObject *item1 = deque1->ob_item[(first1 + i) & mask1]; + PyObject *item2 = deque2->ob_item[(first2 + i) & mask2]; + if (item1 == item2) { + continue; + } + PyObject *tmp1 = Py_NewRef(item1); + PyObject *tmp2 = Py_NewRef(item2); + if (tmp1 == NULL || tmp2 == NULL) { + Py_XDECREF(tmp1); + Py_XDECREF(tmp2); + return NULL; + } + int eq = PyObject_RichCompareBool(tmp1, tmp2, Py_EQ); + Py_DECREF(tmp1); + Py_DECREF(tmp2); + if (eq < 0) { + return NULL; + } + if (eq) { + continue; + } + /* items differ */ + if (op == Py_EQ) { Py_RETURN_FALSE; - if (vs != ws) + } + if (op == Py_NE) { Py_RETURN_TRUE; - } - - /* Search for the first index where items are different */ - it1 = PyObject_GetIter(v); - if (it1 == NULL) - goto done; - it2 = PyObject_GetIter(w); - if (it2 == NULL) - goto done; - for (;;) { - x = PyIter_Next(it1); - if (x == NULL && PyErr_Occurred()) - goto done; - y = PyIter_Next(it2); - if (x == NULL || y == NULL) - break; - b = PyObject_RichCompareBool(x, y, Py_EQ); - if (b == 0) { - cmp = PyObject_RichCompareBool(x, y, op); - Py_DECREF(x); - Py_DECREF(y); - goto done; } - Py_DECREF(x); - Py_DECREF(y); - if (b < 0) - goto done; - } - /* We reached the end of one deque or both */ - Py_XDECREF(x); - Py_XDECREF(y); - if (PyErr_Occurred()) - goto done; - switch (op) { - case Py_LT: cmp = y != NULL; break; /* if w was longer */ - case Py_LE: cmp = x == NULL; break; /* if v was not longer */ - case Py_EQ: cmp = x == y; break; /* if we reached the end of both */ - case Py_NE: cmp = x != y; break; /* if one deque continues */ - case Py_GT: cmp = x != NULL; break; /* if v was longer */ - case Py_GE: cmp = y == NULL; break; /* if w was not longer */ + tmp1 = Py_NewRef(item1); + tmp2 = Py_NewRef(item2); + if (tmp1 == NULL || tmp2 == NULL) { + Py_XDECREF(tmp1); + Py_XDECREF(tmp2); + return NULL; + } + PyObject *result = PyObject_RichCompare(tmp1, tmp2, op); + Py_DECREF(tmp1); + Py_DECREF(tmp2); + return result; } -done: - Py_XDECREF(it1); - Py_XDECREF(it2); - if (cmp == 1) - Py_RETURN_TRUE; - if (cmp == 0) - Py_RETURN_FALSE; - return NULL; + Py_RETURN_RICHCOMPARE(len1, len2, op); } /*[clinic input] @@ -1739,23 +1705,48 @@ deque_init_impl(dequeobject *deque, PyObject *iterable, PyObject *maxlenobj) /*[clinic end generated code: output=7084a39d71218dcd input=2b9e37af1fd73143]*/ { Py_ssize_t maxlen = -1; + PyObject *it = NULL; + PyObject *item; + if (maxlenobj != NULL && maxlenobj != Py_None) { maxlen = PyLong_AsSsize_t(maxlenobj); - if (maxlen == -1 && PyErr_Occurred()) + if (maxlen == -1 && PyErr_Occurred()) { + if (PyErr_ExceptionMatches(PyExc_OverflowError)) { + PyErr_Clear(); + PyErr_SetString(PyExc_ValueError, + "maxlen must not exceed PY_SSIZE_T_MAX"); + } return -1; + } if (maxlen < 0) { PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative"); return -1; } } deque->maxlen = maxlen; - if (Py_SIZE(deque) > 0) - (void)deque_clear((PyObject *)deque); + + /* Initialize the ring buffer */ + deque->allocated = 0; // Start with a power of 2 + deque->ob_item = NULL; + deque->first_element = 0; + Py_SET_SIZE(deque, 0); + if (iterable != NULL) { - PyObject *rv = deque_extend_impl(deque, iterable); - if (rv == NULL) + it = PyObject_GetIter(iterable); + if (it == NULL) + return -1; + + while ((item = PyIter_Next(it)) != NULL) { + if (deque_append_lock_held(deque, item, maxlen) < 0) { + Py_DECREF(item); + Py_DECREF(it); + return -1; + } + Py_DECREF(item); + } + Py_DECREF(it); + if (PyErr_Occurred()) return -1; - Py_DECREF(rv); } return 0; } @@ -1774,11 +1765,7 @@ deque___sizeof___impl(dequeobject *deque) /*[clinic end generated code: output=4d36e9fb4f30bbaf input=762312f2d4813535]*/ { size_t res = _PyObject_SIZE(Py_TYPE(deque)); - size_t blocks; - blocks = (size_t)(deque->leftindex + Py_SIZE(deque) + BLOCKLEN - 1) / BLOCKLEN; - assert(((size_t)deque->leftindex + (size_t)Py_SIZE(deque) - 1) == - ((blocks - 1) * BLOCKLEN + (size_t)deque->rightindex)); - res += blocks * sizeof(block); + res += deque->allocated * sizeof(PyObject *); return PyLong_FromSize_t(res); } @@ -1816,7 +1803,15 @@ static PyGetSetDef deque_getset[] = { {0} }; +static PyMemberDef deque_members[] = { + {"__weaklistoffset__", T_PYSSIZET, + offsetof(dequeobject, weakreflist), Py_READONLY}, + {0} +}; + + static PyObject *deque_iter(PyObject *deque); +static PyObject *deque_subscript(PyObject *self, PyObject *item); static PyMethodDef deque_methods[] = { DEQUE_APPEND_METHODDEF @@ -1842,11 +1837,6 @@ static PyMethodDef deque_methods[] = { {NULL, NULL} /* sentinel */ }; -static PyMemberDef deque_members[] = { - {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(dequeobject, weakreflist), Py_READONLY}, - {NULL}, -}; - static PyType_Slot deque_slots[] = { {Py_tp_dealloc, deque_dealloc}, {Py_tp_repr, deque_repr}, @@ -1874,6 +1864,7 @@ static PyType_Slot deque_slots[] = { {Py_sq_contains, deque_contains}, {Py_sq_inplace_concat, deque_inplace_concat}, {Py_sq_inplace_repeat, deque_inplace_repeat}, + {Py_mp_subscript, deque_subscript}, {0, NULL}, }; @@ -1890,11 +1881,10 @@ static PyType_Spec deque_spec = { typedef struct { PyObject_HEAD - block *b; Py_ssize_t index; + Py_ssize_t len; dequeobject *deque; size_t state; /* state when the iterator is created */ - Py_ssize_t counter; /* number of items remaining for iteration */ } dequeiterobject; #define dequeiterobject_CAST(op) ((dequeiterobject *)(op)) @@ -1910,11 +1900,10 @@ deque_iter(PyObject *self) if (it == NULL) return NULL; Py_BEGIN_CRITICAL_SECTION(deque); - it->b = deque->leftblock; - it->index = deque->leftindex; + it->index = 0; + it->len = Py_SIZE(deque); it->deque = (dequeobject*)Py_NewRef(deque); it->state = deque->state; - it->counter = Py_SIZE(deque); Py_END_CRITICAL_SECTION(); PyObject_GC_Track(it); return (PyObject *)it; @@ -1923,55 +1912,50 @@ deque_iter(PyObject *self) static int dequeiter_traverse(PyObject *op, visitproc visit, void *arg) { - dequeiterobject *dio = dequeiterobject_CAST(op); - Py_VISIT(Py_TYPE(dio)); - Py_VISIT(dio->deque); + dequeiterobject *mio = dequeiterobject_CAST(op); + Py_VISIT(Py_TYPE(mio)); + Py_VISIT(mio->deque); return 0; } static int dequeiter_clear(PyObject *op) { - dequeiterobject *dio = dequeiterobject_CAST(op); - Py_CLEAR(dio->deque); + dequeiterobject *mio = dequeiterobject_CAST(op); + Py_CLEAR(mio->deque); return 0; } static void -dequeiter_dealloc(PyObject *dio) +dequeiter_dealloc(PyObject *mio) { /* bpo-31095: UnTrack is needed before calling any callbacks */ - PyTypeObject *tp = Py_TYPE(dio); - PyObject_GC_UnTrack(dio); - (void)dequeiter_clear(dio); - PyObject_GC_Del(dio); + PyTypeObject *tp = Py_TYPE(mio); + PyObject_GC_UnTrack(mio); + (void)dequeiter_clear(mio); + PyObject_GC_Del(mio); Py_DECREF(tp); } static PyObject * dequeiter_next_lock_held(dequeiterobject *it, dequeobject *deque) { - PyObject *item; - if (it->deque->state != it->state) { - it->counter = 0; + it->len = 0; PyErr_SetString(PyExc_RuntimeError, "deque mutated during iteration"); return NULL; } - if (it->counter == 0) + if (it->index >= it->len) { return NULL; - assert (!(it->b == it->deque->rightblock && - it->index > it->deque->rightindex)); - - item = it->b->data[it->index]; - it->index++; - it->counter--; - if (it->index == BLOCKLEN && it->counter > 0) { - CHECK_NOT_END(it->b->rightlink); - it->b = it->b->rightlink; - it->index = 0; } + if (deque->allocated == 0) { + return NULL; + } + Py_ssize_t mask = deque->allocated - 1; + Py_ssize_t pos = (deque->first_element + it->index) & mask; + PyObject *item = deque->ob_item[pos]; + it->index++; return Py_NewRef(item); } @@ -1980,13 +1964,9 @@ dequeiter_next(PyObject *op) { PyObject *result; dequeiterobject *it = dequeiterobject_CAST(op); - // It's safe to access it->deque without holding the per-object lock for it - // here; it->deque is only assigned during construction of it. - dequeobject *deque = it->deque; - Py_BEGIN_CRITICAL_SECTION2(it, deque); - result = dequeiter_next_lock_held(it, deque); + Py_BEGIN_CRITICAL_SECTION2(it, it->deque); + result = dequeiter_next_lock_held(it, it->deque); Py_END_CRITICAL_SECTION2(); - return result; } @@ -2007,20 +1987,14 @@ dequeiter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) /* consume items from the queue */ for(i=0; icounter) { + if (item == NULL) { + if (PyErr_Occurred()) { Py_DECREF(it); return NULL; - } else - break; + } + break; } + Py_DECREF(item); } return (PyObject*)it; } @@ -2029,8 +2003,11 @@ static PyObject * dequeiter_len(PyObject *op, PyObject *Py_UNUSED(dummy)) { dequeiterobject *it = dequeiterobject_CAST(op); - Py_ssize_t len = FT_ATOMIC_LOAD_SSIZE(it->counter); - return PyLong_FromSsize_t(len); + Py_ssize_t remaining = it->len - it->index; + if (remaining < 0) { + remaining = 0; + } + return PyLong_FromSsize_t(remaining); } PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); @@ -2043,12 +2020,11 @@ dequeiter_reduce(PyObject *op, PyObject *Py_UNUSED(dummy)) // It's safe to access it->deque without holding the per-object lock for it // here; it->deque is only assigned during construction of it. dequeobject *deque = it->deque; - Py_ssize_t size, counter; + Py_ssize_t index; Py_BEGIN_CRITICAL_SECTION2(it, deque); - size = Py_SIZE(deque); - counter = it->counter; + index = it->index; Py_END_CRITICAL_SECTION2(); - return Py_BuildValue("O(On)", ty, deque, size - counter); + return Py_BuildValue("O(On)", ty, deque, index); } static PyMethodDef dequeiter_methods[] = { @@ -2089,11 +2065,10 @@ deque_reviter(dequeobject *deque) if (it == NULL) return NULL; Py_BEGIN_CRITICAL_SECTION(deque); - it->b = deque->rightblock; - it->index = deque->rightindex; + it->index = 0; + it->len = Py_SIZE(deque); it->deque = (dequeobject*)Py_NewRef(deque); it->state = deque->state; - it->counter = Py_SIZE(deque); Py_END_CRITICAL_SECTION(); PyObject_GC_Track(it); return (PyObject *)it; @@ -2102,27 +2077,24 @@ deque_reviter(dequeobject *deque) static PyObject * dequereviter_next_lock_held(dequeiterobject *it, dequeobject *deque) { - PyObject *item; - if (it->counter == 0) - return NULL; - if (it->deque->state != it->state) { - it->counter = 0; + it->len = 0; PyErr_SetString(PyExc_RuntimeError, "deque mutated during iteration"); return NULL; } - assert (!(it->b == it->deque->leftblock && - it->index < it->deque->leftindex)); + if (it->index >= it->len) { + return NULL; + } - item = it->b->data[it->index]; - it->index--; - it->counter--; - if (it->index < 0 && it->counter > 0) { - CHECK_NOT_END(it->b->leftlink); - it->b = it->b->leftlink; - it->index = BLOCKLEN - 1; + if (deque->allocated == 0) { + return NULL; } + Py_ssize_t mask = deque->allocated - 1; + Py_ssize_t offset = it->len - it->index - 1; + Py_ssize_t pos = (deque->first_element + offset) & mask; + PyObject *item = deque->ob_item[pos]; + it->index++; return Py_NewRef(item); } @@ -2131,11 +2103,8 @@ dequereviter_next(PyObject *self) { PyObject *item; dequeiterobject *it = dequeiterobject_CAST(self); - // It's safe to access it->deque without holding the per-object lock for it - // here; it->deque is only assigned during construction of it. - dequeobject *deque = it->deque; - Py_BEGIN_CRITICAL_SECTION2(it, deque); - item = dequereviter_next_lock_held(it, deque); + Py_BEGIN_CRITICAL_SECTION2(it, it->deque); + item = dequereviter_next_lock_held(it, it->deque); Py_END_CRITICAL_SECTION2(); return item; } @@ -2157,20 +2126,14 @@ dequereviter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) /* consume items from the queue */ for(i=0; icounter) { + if (item == NULL) { + if (PyErr_Occurred()) { Py_DECREF(it); return NULL; - } else - break; + } + break; } + Py_DECREF(item); } return (PyObject*)it; } @@ -2660,6 +2623,14 @@ tuplegetter_new_impl(PyTypeObject *type, Py_ssize_t index, PyObject *doc) return (PyObject *)self; } +static int +valid_index(Py_ssize_t i, Py_ssize_t limit) +{ + /* The cast to size_t lets us use just a single comparison + to check whether i is in the range: 0 <= i < limit */ + return (size_t) i < (size_t) limit; +} + static PyObject * tuplegetter_descr_get(PyObject *self, PyObject *obj, PyObject *type) { diff --git a/Modules/clinic/_collectionsmodule.c.h b/Modules/clinic/_collectionsmodule.c.h index b5c315c680e782..b55af5bfab1860 100644 --- a/Modules/clinic/_collectionsmodule.c.h +++ b/Modules/clinic/_collectionsmodule.c.h @@ -236,13 +236,13 @@ PyDoc_STRVAR(deque_rotate__doc__, {"rotate", _PyCFunction_CAST(deque_rotate), METH_FASTCALL, deque_rotate__doc__}, static PyObject * -deque_rotate_impl(dequeobject *deque, Py_ssize_t n); +deque_rotate_impl(dequeobject *deque, PyObject *n); static PyObject * deque_rotate(PyObject *deque, PyObject *const *args, Py_ssize_t nargs) { PyObject *return_value = NULL; - Py_ssize_t n = 1; + PyObject *n = NULL; if (!_PyArg_CheckPositional("rotate", nargs, 0, 1)) { goto exit; @@ -250,18 +250,7 @@ deque_rotate(PyObject *deque, PyObject *const *args, Py_ssize_t nargs) if (nargs < 1) { goto skip_optional; } - { - Py_ssize_t ival = -1; - PyObject *iobj = _PyNumber_Index(args[0]); - if (iobj != NULL) { - ival = PyLong_AsSsize_t(iobj); - Py_DECREF(iobj); - } - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - n = ival; - } + n = args[0]; skip_optional: Py_BEGIN_CRITICAL_SECTION(deque); return_value = deque_rotate_impl((dequeobject *)deque, n); @@ -632,4 +621,4 @@ tuplegetter_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) exit: return return_value; } -/*[clinic end generated code: output=b9d4d647c221cb9f input=a9049054013a1b77]*/ +/*[clinic end generated code: output=ffc64fa17584cdec input=a9049054013a1b77]*/