We currently have an application which is a heavy user of attrs. For certain operations, a majority of the application's time is being spent in the hash method of attrs objects. The frozen attrs objects in question are complex and themselves contain other frozen attrs objects as fields (this goes several layers deep). Being able to cache the hash code value by user request at upper levels of this object tree ((like prehash in Immutables)) would lead to a significant performance improvement for us.
Issue #261 discussed some performance issues with hashing and touched on this. However, this idea seemed to get lost in the following discussion, which is now closed, so it seems worth moving to its own issue.
On that issue @DRMacIver suggested the following:
- Add a
memoize_hash flag to attr.s defaulting to None.
- When
memoize_hash is True, store the hash on a field called __hash.
- If hash is
False (or inferred False from a default) it is an error to pass non-None memoize_hash
- If
slots and memoize_hash are both True, add a bonus __hash slot to the class to store it.
- If hash is
True, memoize_hash defaults to frozen and not slots
We currently have an application which is a heavy user of attrs. For certain operations, a majority of the application's time is being spent in the hash method of attrs objects. The frozen attrs objects in question are complex and themselves contain other frozen attrs objects as fields (this goes several layers deep). Being able to cache the hash code value by user request at upper levels of this object tree ((like
prehashin Immutables)) would lead to a significant performance improvement for us.Issue #261 discussed some performance issues with hashing and touched on this. However, this idea seemed to get lost in the following discussion, which is now closed, so it seems worth moving to its own issue.
On that issue @DRMacIver suggested the following:
memoize_hashflag toattr.sdefaulting toNone.memoize_hashisTrue, store the hash on a field called__hash.False(or inferredFalsefrom a default) it is an error to pass non-Nonememoize_hashslotsandmemoize_hashare bothTrue, add a bonus__hashslot to the class to store it.True,memoize_hashdefaults tofrozenand notslots