Question

In CPython, I think, import means compile the py file to a pyc file and execute the file in the current frame, and the next time CPython will load the pyc file directly without compiling again. What about import in ironpython? I guess ironpython don't have a pyc-like format. Does it compile every time when import?

Was it helpful?

Solution

Yes, IronPython recompiles the imported module on every run. Twice, actually.

It's complicated.

On the first pass, the Python code is parsed into an AST, the AST is converted into a DLR expression tree, and the expression tree is stored. When it is time to execute it, the expression tree is compiled into a set of instructions for a simple stack-based interpreter and the module code is executed on that interpreter. It's not fast when running, but it has very little start up time.

After a piece of code has run for a while, IronPython gets fed up with how slow it is, goes back to the expression tree, and recompiles the code into a .NET delegate. This means that it gets converted to MSIL and then native code by the .NET JIT. This code is fast to execute, but takes time to create.

This conversion is done a per-function (or even per-loop) basis, so that if you use one function from a module repeatedly and none of the rest, only the one commonly-used function will undergo full IL code generation and JITting.

None of this compilation is saved to disk, however. The pyc.py program included with IronPython can precompile the code, but it's not done automatically because the code generated at runtime is different than the code generated by pyc.py. The runtime code is usually collectible, while the code generated by pyc.py is not - and generating non-collectible code at runtime leads to memory leaks. pyc.py should make imports faster by saving a few steps, but I'm not sure by how much.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top