[Python-Dev] Python3 regret about deleting list.sort(cmp=...)

"Martin v. Löwis" martin at v.loewis.de
Sat Mar 12 23:41:56 CET 2011


Am 12.03.11 16:58, schrieb Nick Coghlan:
> On Sat, Mar 12, 2011 at 4:50 PM, Reid Kleckner<reid.kleckner at gmail.com>  wrote:
>> They should be able to use a slotted cmp_to_key style class:
>> http://docs.python.org/howto/sorting.html
>>
>> That will allocate 1 Python object with no dict per key, but that
>> might not be good enough.
>
> Tuples are already slotted, so that isn't likely to help in this case.

Why not? IIUC, the current key function creates three objects: the 
tuple, the short string, and the int. With the class

class cmp_to_key:
   __slots__=['obj']
   def __init__(self, obj):
       self.obj = obj
   def __lt__(self):
       ...

you would only create a single object, so you save the string and the 
integer. In addition, on a 64-bit system, the size of a cmp_to_key
instance is 56 bytes, whereas a two-tuple is 72 bytes, so you also
save 16 bytes per object. Whether that would already create a sufficient
reduction for the case at hand, I don't know.

Regards,
Martin


More information about the Python-Dev mailing list