attrs icon indicating copy to clipboard operation
attrs copied to clipboard

Use cached hash codes to short-circuit equality checks

Open gabbard opened this issue 6 years ago • 5 comments

In cases where cache_hash=True, we can short-circuit equality checks by first comparing hash codes. This can provide a very big speedup for "heavyweight" objects where field equality comparisons can be expensive (which is likely to be the case if the user bothered to enable hash caching).

gabbard avatar Feb 13 '19 21:02 gabbard

@hynek : What do you think of this?

gabbard avatar Feb 13 '19 21:02 gabbard

hmm - looking at https://github.com/python-attrs/attrs/blob/master/src/attr/_make.py#L1079 it looks like our equality methods currently don't do an is check to short-circuit comparisons. This is pretty standard in Java equality methods and can certainly provide a big performance boost for heavyweight objects. I'm not sure if there would be a performance hit in Python for small objects if it were used all the time.

gabbard avatar Feb 13 '19 22:02 gabbard

Yeah I don't think that would be great, because Python does an identity check on its own quite often. 🤔

hynek avatar Feb 14 '19 14:02 hynek

@hynek : Which thing is the "that" in "I don't think that would be great"? Did you mean "I don't think adding an identity check to short-circuit would be good because Python already does it for you" or "I don't think the performance cost of an identity check is high, because Python does them frequently"? Or something else? :-)

gabbard avatar Feb 14 '19 15:02 gabbard

Heh I meant adding an identity check would not be great because it would lead to a lot of double checks. E.g. in dicts and sets.

hynek avatar Feb 14 '19 20:02 hynek