attrs
attrs copied to clipboard
Use cached hash codes to short-circuit equality checks
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).
@hynek : What do you think of this?
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.
Yeah I don't think that would be great, because Python does an identity check on its own quite often. 🤔
@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? :-)
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.