What happens when import a module in ironpython?
Asked Answered
R

1

6

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?

Ramadan answered 8/12, 2011 at 6:19 Comment(1)
Still nobody answer? It seems I have to answer it myself...Ramadan
B
7

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.

Bascomb answered 8/12, 2011 at 16:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.