Skip to content

esp8266: Enable frozen bytecode, with scripts in modules/ subdir. - #2067

Closed
dpgeorge wants to merge 1 commit into
micropython:masterfrom
dpgeorge:esp-frozen
Closed

dpgeorge wants to merge 1 commit into
micropython:masterfrom
dpgeorge:esp-frozen

Conversation

@dpgeorge

Copy link
Copy Markdown
Member

This PR allows to use frozen bytecode with the ESP8266. The directory to freeze is called "modules/".

To start with, only the critical scripts _boot.py and flashbdev.py are frozen to improve performance and reduce RAM consumption. By freezing these 2 files the firmware size is reduced by about 900 bytes (.mpy is smaller than .py source file) and it saves about 1000 bytes of heap RAM for a bare boot with filesystem. (Actually, firmware size increases by about 1.3k to enable this feature, but then drops by about 900 bytes when moving the 2 files from scripts/ to modules/, so net change in firmware size is about +400 bytes; would be expected to go down once more scripts are frozen.)

Results from a fresh boot, then gc.collect(), then micropython.mem_info() are:

Before: GC: total: 28160, used: 5968, free: 22192
After:  GC: total: 28160, used: 4960, free: 23200

Freezing other scripts is blocked by #2066.

Unfortunately there is a hit in performance of about 12%:

BEFORE
>>> pystone_lowmem.main(10000)
Pystone(1.2) time for 10000 passes = 42.94
This machine benchmarks at 232.883 pystones/second
AFTER
>>> pystone_lowmem.main(10000)
Pystone(1.2) time for 10000 passes = 49.08
This machine benchmarks at 203.749 pystones/second

To start with, the critical scripts _boot.py and flashbdev.py are frozen
to improve performance and reduce RAM consumption.

Saves about 1000 bytes of heap RAM for a bare boot with filesystem.
@dpgeorge

dpgeorge commented Jun 1, 2016

Copy link
Copy Markdown
Member Author

Issue #2066 now seems to be fixed, so this PR could be merged.

Freezing webrepl (not part of this PR) takes heap usage (during a webrepl session) from about 9700 bytes used down to 7300, so about 2400 bytes of RAM saved.

@robert-hh

Copy link
Copy Markdown
Contributor

Hello Damien, did you tell above that webrepl is not supported yet with this PR? If yes, then I can confirm. Otherwise, starting webrepl from frozen byte code causes an error:

webrepl.start()
Fatal exception 3(LoadStoreErrorCause):
epc1=0x4000bf64, epc2=0x00000000, epc3=0x00000000, excvaddr=0x4027ae2b, depc=0x00000000

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

Other test code like ntptime and port_diag runs, as well as my test software, which used to crash with the previous attempt when used through webrepl.

@dpgeorge

dpgeorge commented Jun 2, 2016

Copy link
Copy Markdown
Member Author

did you tell above that webrepl is not supported yet with this PR?

Actually, for me, frozen webrepl is now supported with this PR (ie moving all scripts from scripts/ to modules/ works). Note that you need to upgrade to the latest esp-open-sdk and do: make clean-sdk; make. If it still doesn't work for you then please post your results here, thanks!

@robert-hh

Copy link
Copy Markdown
Contributor

After downloading and rebuilding the esp-open-sdk again and doing the same with the esp8266 port, now webrepl works. I had rebuilt the esp-open-sdk already a week ago. Something might have changed in between. Thanks for the great work!

@pfalcon

pfalcon commented Jun 27, 2016

Copy link
Copy Markdown
Contributor

Continuing from #2201 (comment) . I believe I fixed all issues reported on esp side which were on critical path to make this work, so this should be ok to merge. I myself didn't test it though, and don't know when I will be able to. So, I think it should be there just not-yet-official feature, for advanced users to play with. Unless @dpgeorge actually wants to switch any frozen scripts to bytecode soon, then please lead on that (but it wasn't part of KS campaign, so I think we should work on it in background, giving priority to esp-specific issues and stretch goals; in particular, I don't think that enabling frozen bytecode is a panacea to memory allocation issues).

@mianos

mianos commented Jun 27, 2016

Copy link
Copy Markdown

It is certainly not the panacea but it is needed to avoid the issue of parsing code on board blowing up memory. I totally agree that this won't very much at all if you are not diligent with memory allocation and collection for your application first.

@dpgeorge

Copy link
Copy Markdown
Member Author

Merged in db80c0e.

@dpgeorge dpgeorge closed this Jun 29, 2016
tannewt pushed a commit to tannewt/circuitpython that referenced this pull request Aug 17, 2019
@dpgeorge
dpgeorge deleted the esp-frozen branch July 5, 2022 05:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants