Page 1 of 1

compile vs freeze module (with esp32 spiram support enabled)

Posted: Thu Nov 04, 2021 2:44 pm
by aleskeyfedorovich
I've always used the already compiled micropython firmware for esp32 with spiram support.

I investigated the possibility of improving the performance of the microcontroller (ESP32 WROVER) by compiling modules with mpy-cross and/or including (?freezing?) them into the whole micropython firmware.

1. what's the difference in terms of performance between importing a complied module or including it in the firmware?

2. Is the module code visible in any of the two cases?

3. while compiling a module is relatively easy following this guide, I couldn't find an up-to-date, easy to follow guide for including modules inside the firmware, with SPIRAM enabled. Could someone link to such a resource and/or help me with the necessary operations to obtain a micropython firmware with SPIRAM enabled and a custom module frozen inside it?

Thank you

Re: compile vs freeze module (with esp32 spiram support enabled)

Posted: Thu Nov 04, 2021 5:37 pm
by pythoncoder
The main purpose of freezing is to conserve RAM; with SPIRAM this is rarely an issue. If you cross-compile the code, the source is no longer "visible": the file contains only bytecode.

Re: compile vs freeze module (with esp32 spiram support enabled)

Posted: Thu Nov 04, 2021 10:58 pm
by rcolistete
The import time of frozen modules is usually smaller.

For example, see some benchmarks for Pycom WiPy 3, last table of section 4.2 :
https://gitlab.com/rcolistete/micropyth ... C3%B3dulos

Yeah, I need to translate to english...

Re: compile vs freeze module (with esp32 spiram support enabled)

Posted: Fri Nov 05, 2021 10:21 am
by aleskeyfedorovich
thank you for your answers.

So regarding question 1. I interpret that there is no big difference in terms of performance between just compiling modules with mpy-cross and freezing them into the firmware.

Re: compile vs freeze module (with esp32 spiram support enabled)

Posted: Fri Nov 05, 2021 5:51 pm
by pythoncoder
That is correct. However performance measurements on ESP32 are not straightforward due to the chip architecture: users have reported considerable variability. Also SPIRAM adds a great deal to GC time (I've measured 100ms). SPIRAM also had a reputation for unreliability, but this may have been fixed.

Re: compile vs freeze module (with esp32 spiram support enabled)

Posted: Fri Nov 26, 2021 1:44 pm
by bwyld
@pythoncoder,
Do you have a pointer to somewhere recording performance measurements for micropython execution on ESP32?
I'm trying to optimise my micropython for an ESP32 (pycom) port and would be grateful for some pointers on what python constructs are more or less speedy on this arch? (and also which cause more or less heap fragmentation in exeuction)

For example,
- optimising use of dicts : is the perf better with a key which is a int, a char, a short string or a long string? what about using d.get(k) rather than d[k]? what about an array with index instead of a dict?
- try/except execution cost?
- return error result as return value or raise an exception?
- function call cost vs inlining?

I'd like my code to remain as 'nice' as possible but the performance cost can be high with python - which is not such a big deal on a server but with micropython can be a go/nogo decision...

thanks!