blog:230129_mpy-cross_the_ultimate_micropython_precompilation_tool._download_available

MPY-CROSS: The ultimate MicroPython precompilation tool. Download available

MicroPython is a straightforward and effective Python version created for microcontrollers and other devices with limited resources. One of MicroPython's key features is the ability to execute Python code that has been precompiled to bytecode, which can help the interpreter operate better and consume less memory.

Mpy-cross is one of the programs that is most frequently used to precompile Python code for MicroPython. With the help of this command-line utility, Python code created in a MicroPython program can be translated into bytecode that can be run on a device that supports MicroPython. iteration 6 of mpy-cross is the most recent iteration. This version has bug fixes and additional features.The main feature of this version is that it supports the latest version of MicroPython and it includes a new option to control the optimization level of the generated bytecode, which can help to reduce the size of the generated code and improve performance.

Another feature is that it now supports the compilation of Python modules, which can be used to improve the performance of the interpreter and reduce the amount of memory used by the program. Additionally, mpy-cross can be used to create standalone MicroPython executables that can be run on devices without a full MicroPython installation.

Using mpy-cross is very simple. Once you have installed the tool, you can use it to convert Python code into bytecode by running the following command:

mpy-cross myfile.py

Full information about its capabilities is available in help text bellow:

usage: mpy-cross-6.exe [<opts>] [-X <implopt>] <input filename>
Options:
--version : show version information
-o : output file for compiled bytecode (defaults to input with .mpy extension)
-s : source filename to embed in the compiled bytecode (defaults to input file)
-v : verbose (trace various operations); can be multiple
-O[N] : apply bytecode optimizations of level N

Target specific options:
-msmall-int-bits=number : set the maximum bits used to encode a small-int
-march=<arch> : set architecture for native emitter; x86, x64, armv6, armv6m, armv7m, armv7em, armv7emsp, armv7emdp, xtensa, xtensawin

Implementation specific options:
  emit={bytecode,native,viper} -- set the default code emitter
  heapsize=<n> -- set the heap size for the GC (default 2097152)

In addition to the performance benefits, mpy-cross is also a useful tool when working with MicroPython scripts that are short on RAM memory. When running a MicroPython script, the interpreter must load the entire script into memory in order to execute it. This can be a problem for devices with limited RAM, as it may not be able to load large or complex scripts.

By precompiling the script using mpy-cross, the size of the script is reduced, which means that less memory is required to load and execute the script. This can be particularly beneficial when working with memory-constrained devices, such as those with limited RAM or flash storage.

For example, if you have a script that is using up all of the available RAM on your device, you can use mpy-cross to precompile the script and see if that reduces the amount of memory used by the script. You can also use the new option to control the optimization level of the generated bytecode, which can further help to reduce the size of the generated code and improve performance.

In summary, mpy-cross is a valuable tool for MicroPython developers working with memory-constrained devices. By precompiling Python scripts into bytecode, it can help to reduce the amount of memory required to load and execute the script, which can improve the performance of the device and enable the execution of more complex scripts.

The -O option is a command-line switch that can be used when running mpy-cross to control the level of optimization that is applied to the generated bytecode. When using this option, you can specify a numerical value from 0 to 3, with 0 indicating no optimization and 3 indicating the highest level of optimization.

The higher the optimization level, the more aggressive the optimization will be. At level 3, the mpy-cross will try to make the code as small as possible and run as fast as possible, but this may increase the compilation time.

The optimization level can have a significant impact on the size and performance of the generated bytecode. For example, at level 0, the generated bytecode will be larger and may run slower, while at level 3, the generated bytecode will be smaller and may run faster.

The default optimization level is -O2, which provides a good balance between code size and performance. If you are working with a device that has limited flash storage and/or RAM, you may want to try using a higher optimization level, such as -O3, to reduce the size of the generated bytecode.

It's worth noting that when you are using mpy-cross to compile modules, the -O option applies only to the module and not to the whole project.

In summary, the -O option in mpy-cross is used to control the level of optimization that is applied to the generated bytecode. By specifying a numerical value from 0 to 3, you can determine how aggressive the optimization should be and make trade-offs between code size and performance. Keep in mind that the default optimization level is -O2, which provides a good balance between code size and performance, and if you are working with a memory-constrained device, you may want to try using a higher optimization level to reduce the size of the generated bytecode.

always check all your files for viruses

MicroPython release.mpy versionVirusTotal report
v1.19 and upVersion 6Read
v1.12 - v1.18Version 5Read
Enter your comment. Wiki syntax is allowed:
If you can't read the letters on the image, download this .wav file to get them read to you.
 
  • blog/230129_mpy-cross_the_ultimate_micropython_precompilation_tool._download_available.txt
  • Last modified: 2023/04/03 10:40
  • by Ignas