Function unrolling (was Re: Speed: bytecode vz C API calls)

Aahz aahz at pythoncraft.com
Tue Dec 9 15:52:45 EST 2003


In article <tyfd6ayylhy.fsf at pcepsft001.cern.ch>,
Jacek Generowicz  <jacek.generowicz at cern.ch> wrote:
>aahz at pythoncraft.com (Aahz) writes:
>>
>> I think you're going to need to write a solution that doesn't call
>> functions (function unrolling, so to speak).  Instead of returning a
>> memoizer function, just use a dict of dicts.
>
>Sounds interesting. But how do I get the dictionary to use the
>function which it is memoizing, to calculate values which haven't been
>cached yet?

Same way you decide which function to call currently.  Essentially, you
do something like this:

def foo():
    pass

def bar():
    pass

lookup = {
    'foo': {'func': foo, 'cache': {}},
    'bar': {'func': bar, 'cache': {}},
    }

for operation,args in WorkQueue:
    curr = lookup[operation]
    try:
        return curr['cache'][args]
    except KeyError:
        return curr['cache'].setdefault(args, curr['func'](*args))

(Note: untested; you'll have to fill in the blanks)
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote 
programs, then the first woodpecker that came along would destroy civilization.




More information about the Python-list mailing list