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 ^^
Can you strip unneeded code somehow from MP?
Re: Can you strip unneeded code somehow from MP?
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.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Can you strip unneeded code somehow from MP?
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.
Index to my micropython libraries.
Re: Can you strip unneeded code somehow from MP?
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.
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.
Re: Can you strip unneeded code somehow from MP?
pythoncoder wrote: ↑Thu Jul 07, 2022 8:07 amThis 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?
Yep, exactly. Static analysis is not useful for this problem.martincho wrote: ↑Mon Jul 11, 2022 6:22 amNot 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.
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).