[Python-Dev] Speeding up name lookups

Tim Peters tim_one@email.msn.com
Wed, 21 Nov 2001 14:18:27 -0500


Keep in mind that the normal path for an interned-string-key dict lookup in
2.2 is just this, where PyString_CheckExact succeeds, and assuming the key
is present:

	register unsigned int mask = mp->ma_mask;
	dictentry *ep0 = mp->ma_table;
	register dictentry *ep;
	if (!PyString_CheckExact(key)) {
		mp->ma_lookup = lookdict;
		return lookdict(mp, key, hash);
	}
	i = hash & mask;
	ep = &ep0[i];
	if (ep->me_key == NULL || ep->me_key == key)
		return ep;

(Hmm -- the tests in that last "if" should check the second part first.)  So
there's usually not much to be saved by caching the index.  The main benefit
from Python's local-vrbl optimization has more to do with saving function
calls than dict lookups (the latter are usually very cheap for interned
string keys that are present).