classification
Title: cmp_to_key generated class should use __slots__
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: Chris.Colbert, eric.araujo, ncoghlan, python-dev, rhettinger, stutzbach
Priority: normal Keywords:

Created on 2011-03-21 22:13 by Chris.Colbert, last changed 2011-03-22 20:21 by python-dev. This issue is now closed.

Messages (6)
msg131691 - (view) Author: Chris Colbert (Chris.Colbert) Date: 2011-03-21 22:13
The cmp_to_key func acts as a class factory for generating key objects that behave according to a user defined cmp function. 

Many patterns/libs that make use of key functions (for example blist and the SortedCollection recipe) store the the results of the key function internally to provide faster subsequent operations. For large datasets, the number of key objects can become large, and it makes sense to keep their memory footprint as small as possible. 

From this stance, I propose to add "__slots__ = ('obj',)" to the class definition of "K" in cmp_to_key to prevent the creation of the un-needed instance dict and save the memory. 

I find it hard to conjure up a case where a user would need to weakref or add attributes to the return value of a key func. But should that need arise, that functionality could be provided as optional kwargs to cmp_to_key, or just have the user subclass the return value of cmp_to_key. Alternatively, the addition of __slots__ could be specified as a kwarg to cmp_to_key. 

I'd be happy to make a patch for any of these (or alternative) options.
msg131712 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-03-22 00:25
I think this is a good idea.
Am not sure whether it should be backported though.
msg131713 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-03-22 00:37
Right, I edited the versions field to match the versions that contain cmp_to_key, but if this behavior change is just for performance, it’s not a candidate for backport.

Chris: I’d go with the simpler implementation.
msg131714 - (view) Author: Roundup Robot (python-dev) Date: 2011-03-22 00:54
New changeset 97799a54728e by Raymond Hettinger in branch '3.2':
Issue #11628: cmp_to_key should use __slots__.
http://hg.python.org/cpython/rev/97799a54728e

New changeset 2e5e5d515c09 by Raymond Hettinger in branch 'default':
Issue #11628: cmp_to_key should use __slots__.
http://hg.python.org/cpython/rev/2e5e5d515c09
msg131715 - (view) Author: Chris Colbert (Chris.Colbert) Date: 2011-03-22 00:59
Man, you guys are quick! Cheers!
msg131775 - (view) Author: Roundup Robot (python-dev) Date: 2011-03-22 20:21
New changeset 136298326897 by Raymond Hettinger in branch '2.7':
Issue #11628: cmp_to_key should use__slots__.
http://hg.python.org/cpython/rev/136298326897
History
Date User Action Args
2011-03-22 20:21:08python-devsetnosy: rhettinger, ncoghlan, stutzbach, eric.araujo, python-dev, Chris.Colbert
messages: + msg131775
2011-03-22 00:59:33Chris.Colbertsetnosy: rhettinger, ncoghlan, stutzbach, eric.araujo, python-dev, Chris.Colbert
messages: + msg131715
2011-03-22 00:55:16rhettingersetstatus: open -> closed
nosy: rhettinger, ncoghlan, stutzbach, eric.araujo, python-dev, Chris.Colbert
resolution: fixed
2011-03-22 00:54:35python-devsetnosy: + python-dev
messages: + msg131714
2011-03-22 00:37:40eric.araujosetnosy: rhettinger, ncoghlan, stutzbach, eric.araujo, Chris.Colbert
messages: + msg131713
versions: - Python 2.7, Python 3.2
2011-03-22 00:25:04rhettingersetassignee: rhettinger
messages: + msg131712
nosy: rhettinger, ncoghlan, stutzbach, eric.araujo, Chris.Colbert
2011-03-21 23:17:40eric.araujosetnosy: + eric.araujo

versions: - Python 3.1, Python 3.4
2011-03-21 22:44:11stutzbachsetnosy: + stutzbach
2011-03-21 22:13:29Chris.Colbertcreate