microsoft / pxt

Microsoft MakeCode (PXT - Programming eXperience Toolkit)
https://makecode.com
MIT License
2.1k stars 586 forks source link

Code size info #9517

Open Mr-Meow-ZA opened 1 year ago

Mr-Meow-ZA commented 1 year ago

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!

eanders-ms commented 1 year ago

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

eanders-ms commented 1 year ago

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

Can you share your project?

Mr-Meow-ZA commented 1 year ago

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

  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

eanders-ms commented 1 year ago

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]
    ...
Mr-Meow-ZA commented 1 year ago

@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 :)

eanders-ms commented 1 year ago

@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#

Mr-Meow-ZA commented 1 year ago

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

eanders-ms commented 1 year ago

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

Mr-Meow-ZA commented 1 year ago

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