How to reduce code size on teensy 2.0

Status
Not open for further replies.

larry_berlin

Well-known member
Hello,

I have a problem with the size of the compiled code for teensy 2.0.
This size is 106% of the available space.

I have 2 general questions for the experienced forum members:
1. Is it relational to reduce the code size below 100%?
2. How would you proceed in principle?

I can't post the code, but there are a few points:
- wire lib included
- SPI lib included
- Hardware serial used
- Flexitimer 2
- EEPROM
- OLED SSD13xx lib included
- OneButton included

Using the aforementioned libraries, some functions are defined as well as setup () and loop ().

My coding level and kowlege of GCC is limited.
Can someone give me a direction on how to proceed here?
Thanks
 
2. How would you proceed in principle?
Unless doing this for a high volume product I'd consider buying a T3.2 or T4.1 instead. Price difference equates to about 5min engineering time.
If you need to stick with a LC, first thing I'd look at is strings for your display, debug messages etc. The amount of storage needed for text is often underestimated.
 
Unless doing this for a high volume product I'd consider buying a T3.2 or T4.1 instead. Price difference equates to about 5min engineering time.
If you need to stick with a LC, first thing I'd look at is strings for your display, debug messages etc. The amount of storage needed for text is often underestimated.

Your are right, but unfortunatly the hardware is fixed at this stage of project.
 
I'm afraid the only solution might be the difficult task of trimming your code.

But one easy thing you might try is editing boards.txt. Look for this line:

Code:
teensy2.build.flags.cpp=-fno-exceptions -fpermissive -felide-constructors -std=gnu++11

Try changing it to this:

Code:
teensy2.build.flags.cpp=-fno-exceptions -fpermissive -std=gnu++11

Does that shrink the code size? I'm really curious to hear how much difference it makes, if any?

Something else you might try is looking for string constants within your code. Do they have F() around them? Maybe try adding or even removing F() from small some strings. Remove it might seem counter-intuitive, since strings without F() consume RAM. But the compiler has ways to access RAM which require less manipulation of registers, and the actual string data needs to be stored in flash either way, so maybe this can buy you a small savings.

Also look for any places where you have something like Serial.print("a") and change then to Serial.print('a'). Avoiding strings can help a bit.
 
Thank you for your answer.

I always use F() Makro for strings.

I will try your suggestions tonight (Europe time) and give feedback later on.
 
I'm afraid the only solution might be the difficult task of trimming your code.

But one easy thing you might try is editing boards.txt. Look for this line:

Code:
teensy2.build.flags.cpp=-fno-exceptions -fpermissive -felide-constructors -std=gnu++11

Try changing it to this:

Code:
teensy2.build.flags.cpp=-fno-exceptions -fpermissive -std=gnu++11

Does that shrink the code size? I'm really curious to hear how much difference it makes, if any?

Something else you might try is looking for string constants within your code. Do they have F() around them? Maybe try adding or even removing F() from small some strings. Remove it might seem counter-intuitive, since strings without F() consume RAM. But the compiler has ways to access RAM which require less manipulation of registers, and the actual string data needs to be stored in flash either way, so maybe this can buy you a small savings.

Also look for any places where you have something like Serial.print("a") and change then to Serial.print('a'). Avoiding strings can help a bit.


This is the difference between original boards.txt:
Der Sketch verwendet 34362 Bytes (106%) des Programmspeicherplatzes. Das Maximum sind 32256 Bytes.
Globale Variablen verwenden 1405 Bytes (54%) des dynamischen Speichers, 1155 Bytes für lokale Variablen verbleiben. Das Maximum sind 2560 Bytes.

and modified boards.txt:
Der Sketch verwendet 34362 Bytes (106%) des Programmspeicherplatzes. Das Maximum sind 32256 Bytes.
Globale Variablen verwenden 1405 Bytes (54%) des dynamischen Speichers, 1155 Bytes für lokale Variablen verbleiben. Das Maximum sind 2560 Bytes.

I see no difference for same sketch. I had restarted Arduino after editing boards.txt
 
Last edited:
Well, at least it didn't take a lot of work to try it. When I added -felide-constructors many years ago (around Arduino 0019, when String was added), there were a couple cases where -felide-constructors increased code size. Looks like your project must not be using any of those cases. Especially with String usage, on AVR with -Os some code would cause the compiler to create and then soon destroy temporary copies of String, which would quickly add up to memory fragmentation. That's why we've used -felide-constructors all these years. I've sometimes wondered if it was really "wasting" code space. I guess the answer is "probably not".
 
Thank you for your efforts in teensy product lines. I appreciate this.
For my project, I think it is unrealistic, especially with my programming skills, to reduce the code by 7% by source code optimization.
So I have deleted some functionalities and thereby come to 87% of the available flash memory.
 
Status
Not open for further replies.
Back
Top