New submission from Matt Joiner <anacro...@gmail.com>:

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.

----------
components: Interpreter Core
files: set-add-return-bool.patch
keywords: patch
messages: 155909
nosy: anacrolix, eric.araujo, gvanrossum, petri.lehtinen
priority: normal
severity: normal
status: open
title: set.add can return boolean indicate newly added item
type: enhancement
versions: Python 3.3
Added file: http://bugs.python.org/file24862/set-add-return-bool.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue14320>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to