Writing smaller code - Books? Resources?


Well-known member

My background is Application- and Webdevelopment, so I tend to be wasteful with memory. With my Sequencer-Project I am hitting the memory limit (during compile time) over and over again, so I would like to make the "code" smaller, as there are still some screens and features left, that I want to add.

teensy_size: Memory Usage on Teensy 4.1:
teensy_size:   FLASH: code:415504, data:55792, headers:8572   free for files:7646596
teensy_size:    RAM1: variables:108480, code:329504, padding:30944   free for local variables:55360
teensy_size:    RAM2: variables:18400  free for malloc/new:505888

This is after I tried to clean up :(
Are there any resources, books etc. you can recommend?
I am reading heavily on OOP in my programming, trying to make it easier to build the UI.

I have tons of different screens, that I need to display, currently around 30, reusing a lot of code for rendering and passing UI Events around, but also with its own input and output handling, as every screen is doing different things.

I already tried to compile with -Os without any change in the result.


Try adding FLASHMEM to large functions that aren't speed critical.

FLASHMEM void myfunction() {
  // large amount of code here...

Sorry, I seem to wrote unclear: When I have classes (or methods defined in header files), do I have to add that FLASHMEM to the header-definition, the code, or both?
Testing it, header file seems to be enough
I think the C terminology you’re looking for is declarations and definitions. The declarations are often in a header file, and definitions are often in the C or C++ files.
Normally FLASHMEM is meant to be used on the function definition, the place with the function's actual source code, like I tried to show in msg #2.

For C++ classes, normally it would go on the actual member function definitions which are normally in a .cpp file.

For C++ class member functions that are inline defined within the C++ class declaration, you really have much less control. Often the compiler will place a copy of that code "inline" to whatever other function called it. In that inline case, you would want to use FLASHMEM on the calling function.

But this can quick get into many what-if hypothetical situations. I'm not a C++ expert who can say with confidence how the compiler handles every possible / hypothetical situation. Usually when writing C++ I strive to keep things relatively simple and inline functions quite small, and then if I really want to know what the compiler did I look at the .lst or .sym files.