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

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
appels
Posts: 10
Joined: Thu Dec 03, 2020 3:14 pm

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

Post by appels » Sun Jan 10, 2021 10:25 am

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!

User avatar
jimmo
Posts: 2072
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

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

Post by jimmo » Mon Jan 11, 2021 12:55 am

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).

User avatar
pythoncoder
Posts: 4804
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

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

Post by pythoncoder » Mon Jan 11, 2021 8:50 am

Precompiling saves time and electrical energy on wakeup because the compiler uses both.
Peter Hinch

Post Reply