Message 85824 - Python tracker (original) (raw)

In the depths of dictobject.c one can see that dict_setdefault uses two identical calls to PyObject_Hash and ma_lookup. The first to see if the item is in the dict, the second (only if key is not present) to add the item to the dict.

This second lookup (and hash) are not needed and can be avoided by inlining a portion of PyDict_SetItem instead of calling the entire subroutine.