pxt icon indicating copy to clipboard operation
pxt copied to clipboard

Code size info

Open Mr-Meow-ZA opened this issue 1 year ago • 9 comments

Hi there, I have often hit a hurdle where my code is too large and I get the warning saying the "program is too big by X bytes!" But as there is no way of knowing which blocks are using up the most space it is very difficult to optimize the code or to know what to remove and what to leave. If there was a way to determine or even estimate memory usage by blocks then it would make doing larger projects far easier!

Mr-Meow-ZA avatar May 18 '23 09:05 Mr-Meow-ZA

Another report of code size regression. Related: https://github.com/microsoft/pxt-microbit/issues/4808

eanders-ms avatar May 18 '23 23:05 eanders-ms

@Mr-Meow-ZA Can I assume this is a microbit project? If so:

  • Are you using microbit v1 or v2?
  • Please try your project on the latest beta and let us know if you see improvement. https://makecode.microbit.org/beta. We've made some improvements to code size that aren't released yet, but you can try it in beta.

Can you share your project?

eanders-ms avatar May 18 '23 23:05 eanders-ms

@eanders-ms Thanks for the reply and the link to the related post.

  • microbit V2

  • I will try out the beta, thanks for that.

  • I can share some of the project. It's still in early development so the examples I'll send are just for testing.

  • We made a multipurpose android app that links to the microbit so I've been testing it with many different uses. I've changed the way the MakeCode interacts with the data sent from app, which has improved it a lot but I need to learn more about this to avoid future issues.

  • I am also using hall encoders on TT motors, in order to do more precise tracking of the smart car's motion. Test code works well, although I need to improve this further too, but I'm now needing to learn more about the advanced side of MakeCode.

  • Some of the projects I have managed to get quite big, but others hit the size wall. I am going to try reproduce some of those now. The main issues come when adding Neopixel lights, music sound, etc. I also noticed in earlier version of talking to app that data strings, parsing values, etc was a problem, but I've fixed that.

  1. File 1 - is for 4 wheeled omni car using the early bluetooth app.
  2. File 2 - testing the encoders running as interrupt events
  3. File 3 - testing the encoders with the app

The below all use File 1 as an example.

  1. Pic01: I add a "on button A pressed" block with the Neopixel set strip light to rainbow inside it. File is now 3588 bytes too big.
  2. Pic02: I delete lots of blocks. File is 2368 bytes too big.
  3. Pic03: Delete more. File is still 2024 bytes too big. Pic04: *also after deleting some blocks and adding Neopixel or Sound I get warning in my "bluetooth uart read" blocks. I think I've fixed that in later version.

I'll remember to save projects in future when I encounter the issue again as these examples I reproduced now quick do not really show it that well.

makecode examples.zip

Mr-Meow-ZA avatar May 19 '23 10:05 Mr-Meow-ZA

You may get some insight using a new url parameter available in beta that enables calculation of subsystem sizes in the hex: ?compiler=size, e.g.: https://makecode.microbit.org/beta?compiler=size

How to see the output:

  1. With this parameter added to the url, open your project.
  2. Switch to the JavaScript editor.
  3. Click Download to trigger a hex file build.
  4. Expand the Explorer view below the simulator.
  5. Find and expand the built folder.
  6. Click on the file mbcodal-binary.asm. This is the assembler source for the v2 hex.
  7. At the top of file, you should see a code size summary, and then a detailed view of objects and their sizes, something like this:
; generated code sizes (bytes): 6908 (incl. 5008 user, 1274 helpers, 306 vtables, 320 lits); src size 0
...
;
; Code size:
;
;    566 helper: bind [_pxt_helper_bind]
;        by helper: get [_pxt_helper_get]
;    436 radio/radio.ts(63,5): radio.handleDataReceived [radio_handleDataReceived__P3115]
;        by radio/radio.ts(57,5): radio.init [radio_init__P3114]
;    338 _code_helper__conv_22 [_code_helper__conv_22]
;        by many, including main.ts(24,15): inline
;    324 core/game.ts(780,5): game.plot [game_plot__P2999]
...

eanders-ms avatar May 19 '23 18:05 eanders-ms

@eanders-ms thank you very much! That is a very useful trick. I just tried it now and it worked great, I can also use this to estimate sizes by comparison. Going to play with it this week :)

Mr-Meow-ZA avatar May 22 '23 10:05 Mr-Meow-ZA

@Mr-Meow-ZA We made some improvements to code size, and I'm curious if it helps you here. The changes aren't public yet, but can you test your project with this private build and let us know if it works? https://microbit.staging.pxt.io/app/90d23a3465264bc5f2cb314fce5d4be956dd7b12-3596625c81#

eanders-ms avatar Jun 15 '23 23:06 eanders-ms

@eanders-ms thanks for letting me know. I will check it out this week. Sounds promising :)

Mr-Meow-ZA avatar Jun 20 '23 08:06 Mr-Meow-ZA

@Mr-Meow-ZA these changes are released now and available at https://makecode.microbit.org. No need to test in the special build I linked.

eanders-ms avatar Jun 20 '23 15:06 eanders-ms

@eanders-ms that is fantastic, thanks again. It is definitely much better now

Mr-Meow-ZA avatar Jun 21 '23 11:06 Mr-Meow-ZA