What happens when importing a module in ironpython?

In CPython, I think import means compiling the py file into a pyc file and executing the file in the current frame, and next time CPython will load the pyc file directly without compilation. What about imports in iron? IronPhthon probably doesn't have a peak format. Does it collect every time it is imported?

+6
source share
1 answer

Yes, IronPython recompiles the imported module every time it starts. Twice, actually.

It's complicated.

In the first pass, Python code is parsed into AST, AST is converted to a DLR expression tree, and the expression tree is saved. 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 this interpreter. It does not work fast, but it has very little startup time.

After some of the code has been running for some time, IronPython gets tired of how slow it is, returns to the expression tree and recompiles the code into a .NET delegate. This means that it is converted to MSIL, and then native .NET JIT code. This code runs quickly, but it takes time to create.

This conversion is performed for each function (or even for each cycle), so if you use one function from a module several times, and none of the others, only one commonly used function will undergo the complete generation of IL and JITting code.

However, none of these compilations are saved to disk. The pyc.py program included with IronPython can precompile the code, but this is not done automatically because the code generated at runtime is different from the code generated by pyc.py. The runtime code is usually collectible, and the code generated by pyc.py is not - and generating non-collectible code at runtime leads to memory leaks. pyc.py should import the import faster, saving a few steps, but I'm not sure how much.

+7
source

Source: https://habr.com/ru/post/903353/


All Articles