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

Aahz aahz at pythoncraft.com
Wed Dec 10 10:33:47 EST 2003


[Too busy to snip, sorry]

In article <tyfvfopzkpr.fsf at lxplus025.cern.ch>,
Jacek Generowicz  <jacek.generowicz at cern.ch> wrote:
>aahz at pythoncraft.com (Aahz) writes:
>> 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.
>     ^^^^^^^^
>
>(Maybe this is where we are misunderstanding eachother. On first
>reading, I thought that "memoizer" was just a typo. I don't return a
>memoizer function, I return a memoized function.)
>
>>>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.
>
>The way I decide which function to call currently is to type its name
>directly into the source at the call site. Deciding which function to
>call is not the problem: calling any function at all, once I've
>reduced myself to having _just_ a dictionary, is the problem.
>
>Maybe I didn't make the use of "memoize" clear enough.
>
>You use it thus:
>
>  def foo():
>      ...
>
>  foo = memoize(foo)
>
>And from now on foo is a lot faster than it was before[*].

That's why I changed the Subject: to "Function unrolling"; you're going
to need to change your code to avoid function calls in inner loops.  One
technique you might try is to create a generalized inner loop function:

def innerloop(iterable, func, cache):
    l = []
    for args in iterable:
        try:
            l.append(cache[args])
        except KeyError:
            l.append(cache.setdefault(args, func(args))

    return l

Yes, it's a performance hack, but I bet it's a lot clearer than C++
code.
-- 
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