[Python-Dev] Caching tuple hashes

Guido van Rossum guido at python.net
Mon Aug 4 14:07:54 EDT 2003


On Mon, Aug 04, 2003 at 02:51:29AM -0400, Raymond Hettinger wrote:
> Strings save their hash values to avoid recomputation in subsequent
> hashings.  In contrast, tuples recompute on every call.   I've googled
> around and cannot find the rationale for this.  Some ideas are:
> 
> * the time to initialize and check the hash fields isn't repaid on average
> * it isn't worth an extra structure field for storing the hash value
> * C code can mutate immutables so there is a safety risk
> * nobody ever thought of it (unlikely) or got around to it (more likely).

Long ago, strings didn't cache their hash values either.  The cache
was added because strings are the singlemost common dict key type
and the hash calculation was a significant part of the cost of dict lookup,
and experiments showed that caching the hash significantly sped up
almost any Python program, thus justifying the extra expense of space.

I don't see tuples in the same situation -- their hash is rarely in the
critical path of an application, and certainly not of all apps.

--Guido van Rossum



More information about the Python-Dev mailing list