Message 85832 - Python tracker (original) (raw)

Martin, any thoughts?

The same as always: Dan, do you have any benchmarks that demonstrate the speedup?

Looking at the usage of setdefault in the standard library, it seems that it's most of the time either an interned string, or an object hashed by memory address that gets passed as a key. Hashing these is very cheap, so I would be skeptical that any real benefits can be demonstrated.

I also agree with Raymond that, by nature, setdefault only calls hash() twice on the very first call, and not in subsequent calls, so even if there was an improvement, it shouldn't matter for the total runtime. I also agree that setdefault is somewhat inappropriate for performance critical code, since it will create the default value anyway (often a dict or a list), even if a value is already stored. So users who care about performance should write something like

try: l = d[k] except KeyError: l = d[k] = []

to avoid the list creation in the common case, or use a defaultdict in the first place.