Pages: 1 [2]   Go Down
Author Topic: Code size reduction (30478 of 30720 bytes)  (Read 2323 times)
0 Members and 1 Guest are viewing this topic.
'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3266
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Can someone try this?

Code:
const int x = 9600;
int *ptr;
//#define x 9600

void setup() {
   ptr = &x;
   Serial.begin(*ptr); 
}

void loop() {
 
}
What I'm trying to prove is that this will create the actual "const int" variable. Of course that the code will be bigger, but the memory footprint will include the const int variable because now there is pointer to it. (Yes, I'm assuming the compiler is not that smart)
Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7198
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just looks like a very long program to me, around 2,000 lines.
I had a program (several files compared with your one file), that grew to 2,700 lines and took 27K FLASH. It had lots of strings for LCD but I bet the difference between mine and your program is includes. I didn't have that many includes.

Two suggestions I can think off:
1) USE a 24LC512 EEPROM to store your English letters. Hope this won't slow you down too much. You'll have to try.
2) See if you're overloading any functions. Try not to overload. Say you have a constructor chips(int) and another one chips(long). If you used both, just because you didn't bother to convert long into int, the compiler stores both constructors in FLASH.

This can get significant with Serial.print(). Say you're already using the awesome sprintf, you should NOT use serial.print(int or float or long), only use serial.print(string). Use sprintf to construct a string and print with serial.print(string). Try serial.print(string) and serial.print(float) in one program and get rid of serial.print(float)

Code:
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.print("3.14");
//  Serial.print(3.14);
}

This above program is only 1,894 on my 2009 and 3,726 if I uncomment the second line in loop!!!
Logged


'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3266
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

1) USE a 24LC512 EEPROM to store your English letters. Hope this won't slow you down too much. You'll have to try.

I don't see what you mean by English letters... do you mean strings?
Another possibility is to have commands (a couple of bytes) sent to the PC instead of a describing string. And multiply your floating points to 10 or 100 (depending on the size) and use those on the communication to the PC instead of floating point.

2) See if you're overloading any functions. Try not to overload. Say you have a constructor chips(int) and another one chips(long). If you used both, just because you didn't bother to convert long into int, the compiler stores both constructors in FLASH.

Also, great advice. smiley
Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7198
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What's english.h? It's not in your code. I suppose if it's a bunch of letter matrix for your persistent (?) display, you will need these matrix letters.
Logged


USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 78
ClockTHREE Rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you everyone for looking over the code.  I can verify that replacing all of the

const uint8_t bla = xx;

with

#define bla xx

retained the exact code size.

@westwf  The font is pretty large, 8 bytes / char.  Most symbols use only 7x7 pixels so I could save 128 bytes there.  I had a quick look at the optiboot.  Looks like the way to go.

Thanks again!
Justin
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8530
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can someone try this?

Code:
const int x = 9600;
int *ptr;
//#define x 9600

void setup() {
   ptr = &x;
   Serial.begin(*ptr); 
}

void loop() {
 
}

That doesn't compile which implies to me that the compiler knows you can't have a pointer to something that won't exist at run time.

______
Rob

 
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 208
Posts: 12933
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
const int x = 9600;
const int *ptr;

//#define x 9600

void setup() {
   ptr = &x;
   Serial.begin(*ptr);
}

void loop() {
 
}


What I remember from the GCC documentation...

"const" only uses storage (SRAM) if it appears in more than only module (source file).

"static const" nevery uses storage.

Except when the address is taken (like the example above) which forces the compiler to place the constant in memory.


In other words, so long as there is not a pointer involved, "#define" and "static const" are identical in terms of storage.  "static const" has the distinct advantage of also carrying the data-type.
Logged

Pages: 1 [2]   Go Up
Jump to: