Page 1 of 1

Does your code have to recompile everytime you wake up from (deep)sleep

Posted: Sun Jan 10, 2021 10:25 am
by appels
Hi,

I'm trying to get confirmation on something.

The Docs say that at start-up your python source code on the Flash memory gets compiled to byte code, this byte code gets stored in the RAM and then then the Virtual Machine interprets your byte code (which means executes your code).

Now my question: when you have a program that goes repeatedly to (deep)sleep, does your code get recompiled everytime you wake up?

I would think yes, because the byte code is stored on the RAM memory and this won't be kept in deepsleep. But I'm not certain about this.

Thanks in advance!

Re: Does your code have to recompile everytime you wake up from (deep)sleep

Posted: Mon Jan 11, 2021 12:55 am
by jimmo
appels wrote:
Sun Jan 10, 2021 10:25 am
I would think yes, because the byte code is stored on the RAM memory and this won't be kept in deepsleep. But I'm not certain about this.
Yes, if RAM is lost, then it must re-compile the code.

If this is a concern, then you can either compile your .py files to .mpy (in which case the bytecode will just be copied directly into RAM from flash), or even better, freeze the code into your firmware (in which the bytecode will be executed directly from ROM).

Re: Does your code have to recompile everytime you wake up from (deep)sleep

Posted: Mon Jan 11, 2021 8:50 am
by pythoncoder
Precompiling saves time and electrical energy on wakeup because the compiler uses both.