[Python-ideas] namedtuple from ordereddict

Serhiy Storchaka storchaka at gmail.com
Fri Jul 21 01:28:40 EDT 2017


20.07.17 23:17, Jim J. Jewett пише:
> Several of the replies seemed to suggest that re-using the current
> dict structure for a tuple wouldn't work.  Since I'm not sure whether
> people are still thinking of the old structure, or I'm missing
> something obvious, I'll be more explicit.
> 
> Per https://github.com/python/cpython/blob/master/Include/dictobject.h#L40
> the last element of a dict object is now a separate pointer to
> ma_values, which is an array of objects.
> 
> Per https://github.com/python/cpython/blob/master/Include/tupleobject.h#L27
> the last element of a tuple object is also an array of objects.
> 
> Is there some reason these arrays cannot be the same memory?  e.g.,
> does a tuple header *have* to be contiguous with its data, and if so,
> is there a reason that the dict's ma_array can't be allocated with an
> extra tuple-header prefix?

Having a tuple header to be contiguous with its data decreases a total 
size of consumed memory, decreases memory fragmentation, speeds up 
tuple's creation and item access. Memory consumption and performance of 
tuples are critically important.

Allocating the dict's ma_values with an extra tuple-header prefix will 
increase memory consumption for instance dictionaries and will 
complicate the dict implementation (this can harm the performance). This 
also will increase a code coupling between dicts and tuples.

Named tuples are rarely used in comparision with ordinary tuples and 
dictionaries, and they shouldn't be improved at the cost of tuples and 
dicts.



More information about the Python-ideas mailing list