[Python-Dev] School IIb?
python-dev at zesty.ca
Tue Jun 27 00:41:13 CEST 2006
Here's a possible adjustment to the School-II approach that i think
avoids the issues i've been raising, while giving the desired
O(n)-to-O(1) speedup in common situations. It's basically School-II
dispatch, plus a check:
On compilation, freeze any cases that meet the School-II conditions
and have a trustworthy __hash__ method into a dictionary. At runtime,
when the dictionary yields a hit, check if the case expression yields
a different value. If the value has changed, use if/elif processing.
In most cases the case-equality check will be cheap (e.g. an attribute
lookup), but it would allow us to establish for sure that the switch
value really matches the case value when we branch to a particular
case, so we'd not be so vulnerable to __hash__ misbehaving, which
seems to be your main source of discomfort with if/elif semantics.
More information about the Python-Dev