@Jacob: there is a difference in the distribution of objects, but they are not copied to the record. Python allocates a โfree listโ of a fixed size, where it can quickly highlight dictionary objects (until it fills up). Layers highlighted using the {} syntax (or calling C on PyDict_New ) can come from this free list. When the dictionary is no longer referenced, it is returned to the free list and this memory block can be reused (although the fields are reset first).
This first dictionary immediately returns to the free list, and the next will reuse its memory space:
>>> id({}) 340160 >>> id({1: 2}) 340160
If you save the link, the following dictionary will appear from the following free slot:
>>> x = {} >>> id(x) 340160 >>> id({}) 340016
But we can remove the link to this dictionary and release its slot again:
>>> del x >>> id({}) 340160
Since the syntax {} processed in byte code, it can use the optimization mentioned above. On the other hand, dict() treated like a regular class constructor, and Python uses a shared memory allocator that does not follow an easily predictable pattern, such as a free list.
Also, looking at compile.c from Python 2.6, with the syntax {} , the hash table seems to be pre-sized based on the number of elements it stores, which is known during parsing.
Matt Good Mar 25 '09 at 12:28 2009-03-25 12:28
source share