The examples of "c extern" in the "Doc/includes/newtypes" directory of Python 3.11 and later versions need to be optimized, the memory structure of the __dict__
in the C extern example under Doc/includes/newtypes
is outdated.
#123951
Labels
docs
Documentation in the Doc dir
Documentation
For Python 3.11 and later, refer to:

faster-cpython/ideas#80 (implementation details in CPython source:
_PyObject_InitializeDict
)https://www.youtube.com/watch?v=xKk7IXm0XO0
The memory structure of the Python object dict pointer (
dict ptr
) is optimized:However, in CPython directory
Doc/includes/newtypes
, the C extern example that involves adding a__dict__
, such as:still uses the old

dict ptr
structure (prior to Python 3.10).This involves two issues:
dict ptr
structure uses the previous type (as stated above)dict ptr
memory optimization in Python 3.11 and later does not work in the C extern example:The memory optimization in
_PyObject_InitializeDict->init_inline_values
is not utilized:When objects are created in large quantities, the memory per single object can differ by nearly 200 ~ 300bytes 。
detail
memory of ItemData2 = 392 bytes
"The memory size allocated for


new_values
asPyDictValues
is determined by argsize
。arg
size
default value is 30(when the__dict__
contains fewer than 30 members):When arg
size
is 30, the memory size ofPyDictValues
is 272 bytes:"total size:
memory of ItemData = 104 bytes
when the
__dict__
contains 2 memebers:I try to fix it, like:
The text was updated successfully, but these errors were encountered: