This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author anacrolix
Recipients anacrolix, eric.araujo, gvanrossum, petri.lehtinen
Date 2012-03-15.16:55:43
SpamBayes Score 3.0376972e-07
Marked as misclassified No
Message-id <1331830545.62.0.37534358264.issue14320@psf.upfronthosting.co.za>
In-reply-to
Content
set.add can return True to indicate a newly added item to the set, or False if the item was already present.

The C function PySet_Add returns -1 on error, and 0 on success currently. This is extended to return 1 if the item is newly added, and 0 if it was already present. Precedents exist for PySet_Contains and PySet_Discard with the same semantics. There are only 3 calls that need to be patched in the entire core, these are included in the patch.

The Python function set.add currently returns None. This is extended to return True if if the item is new to the set, and False otherwise. No object overhead is introduced as set.add is already currently executing Py_RETURN_NONE.

Benchmarks indicate that set.add if the item isn't already present (returning True in the patched instance) reduces time by 5-9%. set.add if the item already exists increases time by 1-3%. I'm happy to put these down to effects of touching True and False instead of None on return from set.add.

Patch and primitive performance test attached.
History
Date User Action Args
2012-03-15 16:55:46anacrolixsetrecipients: + anacrolix, gvanrossum, eric.araujo, petri.lehtinen
2012-03-15 16:55:45anacrolixsetmessageid: <1331830545.62.0.37534358264.issue14320@psf.upfronthosting.co.za>
2012-03-15 16:55:45anacrolixlinkissue14320 messages
2012-03-15 16:55:44anacrolixcreate