2.3 bytecodehacks.inline

This module provides inline functions for Python. This module is the most involved bytecodehack. I am not entirely sure it is free of bugs, and when it goes wrong it can do very odd things. I would appreciate people trying this module out on ``real world'' examples. Email me (mwh21@cam.ac.uk) any failures, I'll try and sort them out.

This module takes two arguments, both functions. It inlines any calls to the second in the first, i.e. injects the bytecode from the second into that of the first. There's some stack shuffling and details to sort out, but that's roughly it.

A (slightly extended) example:

Suppose you want to integrate the ODE y'=y with the initial condition y(0)=0. Then (if you were slightly insane) you might write a simple Euler scheme integrator as follows:

def f(x,y):
    return y

def integ_global(dx,x0,x1,y):
    while x<x1:
    return y

This works, slowly.

If you want to speed it up, you could obviously make the function f a parameter, coincidentally enabling you to integrate more than just this ODE, but instead I propose you do this:

from bytecodehacks.inline import inline


Then integ_inline does the same job as integ_global, but the code of f has been inlined into that of the integ_inline, eliminating a costly function call, and so running faster.

Now some caveats. As with the rest of this package, I've ignored varadic functions. Default parameters are out. Keyword parameter passing is OK.

Some of the preceding may change as an when I feel like generalizing this module, but that isn't just yet. I'll try for default arguments next, I think.

inline (func,funcname=functoinline)
Return a new function where all calls to the function called funcname in func replaced by the code of functoinline. That doesn't make much sense, but it's intuitive, honest. Stuff will probably get printed to stdout; if this offends you, comment the prints out. The prints will probably disappear when I feel confident of the code.

Send comments to mwh@python.net