[Python-Dev] Faster Set.discard() method?

Andrew McNamara andrewm at object-craft.com.au
Fri Mar 18 07:19:44 CET 2005

>The C implementation has this code:
>	if (PyDict_DelItem(so->data, item) == -1) {
>		if (!PyErr_ExceptionMatches(PyExc_KeyError))
>			return NULL;
>		PyErr_Clear();
>	}
>Which is more-or-less the same as the sets.Set version, right?  What I was
>wondering was whether changing that C to a C version of your dict.pop()
>version would also result in speedups.  Are Exceptions really that slow,
>even at the C level?

No, exceptions are fast at the C level - all they do is set a flag. The
expense of exceptions is saving a restoring python frames, I think,
which doesn't happen in this case. So the current implementation is
ideal for C code - clear and fast.

Andrew McNamara, Senior Developer, Object Craft

More information about the Python-Dev mailing list