x42005e1f · GitHub

Bug report

Bug description:

queue.SimpleQueue (C level) uses a list on Python < 3.13[1][2][3][4], a ring buffer on Python >= 3.13[5][6]. However, it does not implement its own __sizeof__() method, and as a result, only the size of the simplequeueobject structure itself (basicsize) is taken as the size of the object, while the size of the underlying structure is ignored.

>>> from queue import SimpleQueue
>>> q = SimpleQueue()
>>> q.__sizeof__()
72  # 56 on Python < 3.13
>>> for _ in range(1_000):
...     q.put(object())
...
>>> q.__sizeof__()
72  # 56 on Python < 3.13

Expected (should be, on Python >= 3.13):

>>> from queue import SimpleQueue
>>> q = SimpleQueue()
>>> q.__sizeof__()
136  # == 72 + 8*8 == sizeof(simplequeueobject) + sizeof(PyObject *)*INITIAL_RING_BUF_CAPACITY
>>> for _ in range(1_000):
...     q.put(object())
...
>>> q.__sizeof__()
8264  # == 72 + 8*1024 == sizeof(simplequeueobject) + sizeof(PyObject *)*pow(2, ceil(log2(1000)))

CPython versions tested on:

3.9, 3.10, 3.11, 3.12, 3.13, 3.14

Operating systems tested on:

Linux

Linked PRs

Read the original on github.com ↗