The real issue is that <tt>Tcl_FirstHashEntry</tt> is being used repeatedly, since that does a linear scan from the beginning of the list of buckets. At the very least, the cleanup code should avoid that. It's not the responsibility of the hash table code to make people use its API correctly.