Description
The Use Case
We have an AST that needs to be serialized. Currently we're defining the AST classes using attrs and serializing it with pickle, and we're in the middle of moving to msgspec for both aspects. (We really want the speed of Struct, and we need tagged unions.)
When processing the AST, we do a lot of hashing. Hundreds of thousands of calls. This is easily solved by using attrs's cache_hash option, cutting the number of __hash__() calls by a significant amount and saving us a good chunk of time.
The Feature
We'd really like msgspec to provide something like cache_hash. I think it should be easily implementable once private fields (#199) are available: _hash (or similar) would be a private field that's calculated when the struct is initialized, and __hash__() would just return _hash. (Or it could be calculated the first time __hash__() is called. Doesn't matter much, I think.)
It would also be nice if this was usable with a custom __hash__() implementation, as the default implementation does not work well for us.
I think this would be another configuration value, like frozen and the like. Alternatively, it could be enabled by default when frozen=True.
This is almost implementable by users already, except that frozen Structs can't be mutated (even in __post_init__) and the cached hash shouldn't be encoded/decoded.
Description
The Use Case
We have an AST that needs to be serialized. Currently we're defining the AST classes using
attrsand serializing it withpickle, and we're in the middle of moving tomsgspecfor both aspects. (We really want the speed ofStruct, and we need tagged unions.)When processing the AST, we do a lot of hashing. Hundreds of thousands of calls. This is easily solved by using
attrs'scache_hashoption, cutting the number of__hash__()calls by a significant amount and saving us a good chunk of time.The Feature
We'd really like msgspec to provide something like
cache_hash. I think it should be easily implementable once private fields (#199) are available:_hash(or similar) would be a private field that's calculated when the struct is initialized, and__hash__()would just return_hash. (Or it could be calculated the first time__hash__()is called. Doesn't matter much, I think.)It would also be nice if this was usable with a custom
__hash__()implementation, as the default implementation does not work well for us.I think this would be another configuration value, like
frozenand the like. Alternatively, it could be enabled by default whenfrozen=True.This is almost implementable by users already, except that frozen Structs can't be mutated (even in
__post_init__) and the cached hash shouldn't be encoded/decoded.