Next: Performance expectations
Up: Caveats
Previous: Patched functions
Contents
Unsupported Python constructs
Psyco only compiles functions. It will not accelerate any code that runs outside any function, like:
- top-level module code
- the code defining a class -- i.e. the execution of the class statement. Methods themselves are accelerated just fine when you actually call them.
- the code run by an exec statement or execfile or eval.
You can always work around the above limitations by creating functions and calling them instead of directly executing a time-consuming source. For example, instead of writing a short test script like
some_big_list = ...
for x in some_big_list:
do_something()
write instead
def process_list(lst):
for x in lst:
do_something()
process_list(...)
As another example, a function like
def f(some_expr):
for x in range(100):
print eval(some_expr) # where some_expr can depend on x
should instead be written
def f(some_expr):
my_func = eval("lambda x: " + some_expr) # -> a function object
for x in range(100):
print my_func(x)
In addition, inside a function, some syntactic constructs are not supported by Psyco. It does not mean that a function using them will fail; it merely means that the whole function will not be accelerated. The following table lists the unsupported constructs, along with the corresponding bytecode instruction name and number. Log files only report the bytecode instruction number, which you need to look up here.
23#23
Notes:
- (1)
- Psyco cannot accelerate class definitions, i.e. the execution of the body of the class statement - i.e. the creation of the class object itself. This does not prevent it from accelerating methods in the class.
- (2)
- Functions using this construct cannot be accelerated.
- (3)
- Generators (i.e. any function using the yield keyword) cannot be accelerated currently. If there is enough interest I can consider implementing them. This includes generator expressions (Python 2.4). Warning! The function containing a generator expression will be compiled by Psyco, but not the generator expression itself. If the latter calls other functions compiled by Psyco, then performance will be very bad: calling from Psyco to Python to Psyco comes with a significant overhead.
- (4)
- Using nested scopes (i.e. variables shared by a function and an inner sub-function) will prevent both the outer and the inner function to be accelerated. This too could be worked around if there is enough interest, at least for accelerating the unrelated parts of the functions - the accesses to the shared variables themselves might be difficult to optimize.
- (5)
- These constructs can appear in class definitions (see (1)) or at the module top-level. It is possible to enable support for module top-level code, but not recommended; instead, try to put all the code you want accelerated in function bodies.
- (6)
- The with statement is not supported.
- (7)
- The syntax for sets is not supported. Note that you can still use sets and have them accelerated as long as you don't use the special syntax to make them, introduced in Python 2.7.
Next: Performance expectations
Up: Caveats
Previous: Patched functions
Contents
2011-11-26