One Python 2.1 idea

Tim Peters tim.one at home.com
Tue Dec 26 13:59:37 EST 2000


[Russell Turpin]
> Has there been some discussion about caching? If done
> well, the vast majority of references would reach
> their object's attribute or method for the cost of a
> couple of integer compares and array fetches. This
> should be an order of magnitude faster than string
> lookup in a dictionary. This especially benefits
> method invocation, since that costs a failed lookup
> to the instance dictionary, and a successful lookup
> in the class dictionary. That's almost enough to
> scare me away from methods!

Martin von Löwis plugged away at this and reported on his experience at the
1998 Python Conference.  Go to

  http://www.foretec.com/python/workshops/1998-11/proceedings.html

and look for "Virtual Methods Tables in Python".  The paper is available
online in HTML form, or downloadable in postscript.

The results were inconclusive, and the reasons for why the results were
inconclusive were also muddy <wink>.  Two gotchas:

1. Methods and attributes can be created or destroyed on the fly, and indeed
even the class hierarchy can mutate in arbitrary ways at runtime.  For that
reason, Martin based his experiments on ideas borrowed from a recent
Smalltalk optimization paper (reference [2] in Martin's paper).  It's a
complex scheme.

2. As you say:

> Most work in a Python program involves object methods
> or attributes. (At least, this is the case in my
> programs. ;-) Increasing the speed of reference to
> these should have appreciable benefit, across the
> board.

Because it is so speed-critical, the dict-based lookup has been optimized
repeatedly over the years -- it's not as slow as you might guess at first.
Or, it might be slower, depending on your first guess <wink>.  There
certainly seems to be great potential here, but, alas, so far nothing has
tapped into it convincingly.

the-pursuit-of-speed-is-a-cruel-mistress-ly y'rs  - tim





More information about the Python-list mailing list