Can you strip unneeded code somehow from MP?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Taki7o7
Posts: 9
Joined: Sun Jul 03, 2022 11:50 pm

Can you strip unneeded code somehow from MP?

Post by Taki7o7 » Thu Jul 07, 2022 5:29 am

I wonder if it's possible to automate compilation of micropython, while it only uses stuff a given (Micro)Python script needs, so that micropython would not waste space for unused functionality.

Is this theoretically possible? Or is there even some "ready to use" solution to this? Sorry if i misunderstood how things work.

Just a thought, i am not used to work with stuff where you have to keep an eye on storage ^^

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

Re: Can you strip unneeded code somehow from MP?

Post by jimmo » Thu Jul 07, 2022 7:53 am

Taki7o7 wrote:
Thu Jul 07, 2022 5:29 am
I wonder if it's possible to automate compilation of micropython, while it only uses stuff a given (Micro)Python script needs, so that micropython would not waste space for unused functionality.
Interesting question!

There's no automated way to do this right now. I guess there are two ways you could approach this, either by turning off unused features (at the level defined in py/mpconfig.h (e.g. MICROPY_PY_BUILTINS_SET if the code never uses the set() builtin), or by actually dropping functionality at the level of individual functions etc.

Doing this in a general purpose automated way is difficult for a dynamic language like Python, and I suspect can only be done usefully by doing runtime analysis rather than static analysis.

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

Re: Can you strip unneeded code somehow from MP?

Post by pythoncoder » Thu Jul 07, 2022 8:07 am

This sounds like a CS question akin to the Halting Problem: is it possible to write a program which, given as input a program P and a line number N, will return True if line N will eventually be executed?
Peter Hinch
Index to my micropython libraries.

martincho
Posts: 96
Joined: Mon May 16, 2022 9:59 pm

Re: Can you strip unneeded code somehow from MP?

Post by martincho » Mon Jul 11, 2022 6:22 am

Not an easy problem. As an example, a number of IDE's try to help you by pointing out code that will never run. I use PyCharm Pro and, while I like the IDE, I see instances of the tool getting it wrong on a daily basis. One can only imagine the amount of effort and development that has gone into analyzing your code in real time to provide such recommendations.

There are other issues. For example, a set of functions that run based on external inputs (sensors, ADC, I2C, SPI, serial, etc.). It is impossible to know which functions will be needed without a deep enough understanding of the application domain.

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

Re: Can you strip unneeded code somehow from MP?

Post by jimmo » Mon Jul 11, 2022 7:12 am

pythoncoder wrote:
Thu Jul 07, 2022 8:07 am
This sounds like a CS question akin to the Halting Problem: is it possible to write a program which, given as input a program P and a line number N, will return True if line N will eventually be executed?
martincho wrote:
Mon Jul 11, 2022 6:22 am
Not an easy problem. As an example, a number of IDE's try to help you by pointing out code that will never run. I use PyCharm Pro and, while I like the IDE, I see instances of the tool getting it wrong on a daily basis. One can only imagine the amount of effort and development that has gone into analyzing your code in real time to provide such recommendations.

There are other issues. For example, a set of functions that run based on external inputs (sensors, ADC, I2C, SPI, serial, etc.). It is impossible to know which functions will be needed without a deep enough understanding of the application domain.
Yep, exactly. Static analysis is not useful for this problem.

But, if the developer promises that a particular set of test scenarios definitely covers every possible piece of functionality, then at least at the level of the options in mpconfig.h it should be possible to determine that a configuration is valid, and so you could imagine you could "search" for a minimal configuration in an automated way.

(It was just a thought though, I definitely don't think this is worth investing in! For the same time investment I think manually figuring out which options to disable will be a far more effective result, and more importantly might guide the developer into finding ways to optimise their code by seeing what can be disabled).

Post Reply