CPython internal structures

GAE has various limitations, one of which is the size of the largest allocated memory block in the amount of 1 MB (now 10 times larger, but this does not change the question). The restriction means that you cannot put more than a certain number of elements in list (), since CPython will try to allocate an adjacent block of memory for element pointers. Having a huge list of () s may be considered bad programming practice, but even if a huge structure is not created in the program itself, CPython supports some behind the scenes.

It seems that CPython supports one global list of objects or something else. That is, an application with many small objects tends to allocate larger and larger individual memory blocks.

The first idea was gc, and disabling it slightly changed the behavior of the application, but some structures are still preserved.

The simplest short application that is having a problem:

a = b = []
number_of_lists = 8000000
for i in xrange(number_of_lists):
    b.append([])
    b = b[0]

Can someone enlighten me on how to prevent CPython from allocating huge internal structures when there are many objects in the application?

+3
source share
4 answers

32- 8000000 20 , 16 . , (20 + 16) * 8000000 = 20168000000 , 20 . , malloc , .

:

  • 2 PyListObject (. listobject.h)
  • 1 Py_ssize_t PyObject_HEAD (. object.h)
  • Py_ssize_t PyObject_VAR_HEAD ( .h)

, - . list_resize listobject.c. 0, 4, 8, 16, 25, 35, 46, 58, 72, 88,... , 4 .

, - . 12 , , , . , , .

+8

, . , . a, ( b ) . 'a =', .

: , , Python , , . ? , / , , .

0

a = b = []

b = b[0]

? , Python, , : a b - (, C). , ( !), , , .

, , , , , -, . , ... , , Pythonic- .

0

, Python . --without-pyalloc .

- 256 , . Python , --with-pydebug. .

I suspect your hunch and I am sure that the correct diagnosis is correct. The list uses continuous memory, so if your list becomes too large for the system arena, you're out of luck. If you're really adventurous, you can reimplement PyList to use multiple blocks, but that will be a lot of work, as various bits of Python expect continuous data.

0
source

Source: https://habr.com/ru/post/1703614/


All Articles