Page 1 of 2

Support loading of .mpy files

Posted: Wed May 25, 2016 7:55 am
by Roberthh
For everyone running into memory errors during importing a module, the following hack is useful, enabling the loading of modules precompiled with mpy-cross.
1. Add the following line to esp8266/mpconfigport.h:

Code: Select all

#define MICROPY_PERSISTENT_CODE_LOAD (1)
2. Change line 430 of py/emitglue.c into

Code: Select all

#elif defined(__thumb2__) || defined(__xtensa__)
and rebuild the image. It does not execute .mpy as a frozen module, the .mpy file have to be in the user file system. Obviously, frozen byte code is the better choice. But that will come soon, I hope.

Re: Support loading of .mpy files

Posted: Fri Jun 03, 2016 10:08 pm
by wendlers
Hi,

very nice :-). Just tried it, and it works. Beside the memory savings, it looks like my module is loading much faster when pre-compiled.

regards,
stefan

Re: Support loading of .mpy files

Posted: Sat Oct 08, 2016 8:32 pm
by chrisgp
It looks like this still isn't available in the main repository. Is there any downside to turning this on or reason why it would not be included in the main repository?

If I understand correctly, frozen bytecode (which is now available) is still more desirable because the bytecode is executed from flash and an .mpy file results in the bytecode consuming ram. Even so, it seems like having the option of using an .mpy file is nice for cases when someone might not want to flash new firmware.

Re: Support loading of .mpy files

Posted: Tue Oct 11, 2016 3:36 pm
by platforma
There is a PR by Damien: https://github.com/micropython/micropython/pull/2502
That introduces this for the esp8266, I would expect it to be merged into the master fairly soon.

Re: Support loading of .mpy files

Posted: Wed Oct 12, 2016 12:09 am
by mianos
Exiting news! 'mpy' is merged in ESP8266. :D :D :D :D :D :D :D :D :D :D :D :D

Re: Support loading of .mpy files

Posted: Thu Oct 13, 2016 5:40 pm
by Llwy
This is one of the best announcement on this project in a while !

Re: Support loading of .mpy files

Posted: Fri Oct 21, 2016 8:14 am
by Lornioiz
Hello everyone,

as an lower-intermediate level Python desktop user I fairly early crashed into the "memory wall" of mcu world...
I started to read and learn as much as I can about the methodology needed in order to lower the footprint of my application but it is not so easy transition for me, as I'm used to use an infinite quantity of memory.
About frozen bytecode and .mpy I would like to ask a couple of questions:
Which is the difference between frozen bytecode (that, if I understand, are module loaded in a custom firmware) and using a precompiled file .mpy? aren't both bytecode, why one of them is more memory efficient?
One more question (among many that are stemmed from my ignorance): the memory saving is linear among data types? in other words, frozen bytecode or mpy file give us the same memory saving (percentual wise) if used to store a floating point number or a string?
Finally, if you can point me to some resource that can help me understand how this works I would be grateful.

Thank you!

Re: Support loading of .mpy files

Posted: Fri Oct 21, 2016 8:46 am
by dhylands
bytecode can exist as an .mpy file, in which case its loaded into RAM.
frozen bytecode is compiled the firmware, in which case the bytecode is in flash.

So regular bytecode takes up RAM where frozen bytecode doesn't. The exception to this is on the WiPy, where all of the firmware is executed from RAM, so it doesn't matter whether its frozen or not, it still takes up the same amount of RAM.

Re: Support loading of .mpy files

Posted: Fri Oct 21, 2016 10:52 am
by Lornioiz
[So regular bytecode takes up RAM where frozen bytecode doesn't.[/quote]

Thank you very much Dave,

That means that in a esp8266 I can import as much modules as I want (by the import statement) without using any ram if they are saved in the firmware? just curious, how severe is the speed penalty in doing so (I'm thinking about freezing as much of the application I can by splitting it in various modules)?

Thanks!!

Re: Support loading of .mpy files

Posted: Fri Oct 21, 2016 1:58 pm
by Roberthh
There is some speed penalty documented somewhere. As far as I recall, it was like 25%.