Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any reason *not* to cache an object's hash?

I've written a class whose .__hash__() implementation takes a long time to execute. I've been thinking to cache its hash, and store it in a variable like ._hash so the .__hash__() method would simply return ._hash. (Which will be computed either at the end of the .__init__() or the first time .__hash__() is called.)

My reasoning was: "This object is immutable -> Its hash will never change -> I can cache the hash."

But now that got me thinking: You can say the same thing about any hashable object. (With the exception of objects whose hash is their id.)

So is there ever a reason not to cache an object's hash, except for small objects whose hash computation is very fast?

like image 408
Ram Rachum Avatar asked Sep 24 '10 13:09

Ram Rachum


1 Answers

Sure, it's fine to cache the hash value. In fact, Python does so for strings itself. The trade-off is between the speed of the hash calculation and the space it takes to save the hash value. That trade-off is for example why tuples don't cache their hash value, but strings do (see request for enhancement #1462796).

like image 82
Thomas Wouters Avatar answered Oct 04 '22 20:10

Thomas Wouters