Skip to content

Arduino fails to optimize out unreachable Serial library code #266

Closed
@EtherFidelity

Description

@EtherFidelity

Many libraries use Serial output in debugging routines, and often times the serial interface is used for any number of other purposes.

Sometimes though, in a finished device, the serial interface is not used at all. In these instances, those serial output routines are never called, yet the Serial library wastes precious Flash space and SRAM.

Here's an empty Arduino project, and its compilation output:

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}

Sketch uses 450 bytes (1%) of program storage space. Maximum is 32,256 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2,039 bytes for local variables. Maximum is 2,048 bytes.

Now the same code again with a simple, but unused function added:

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}

void unusedFunction() {
  Serial.println();
}

Sketch uses 1,388 bytes (4%) of program storage space. Maximum is 32,256 bytes.
Global variables use 182 bytes (8%) of dynamic memory, leaving 1,866 bytes for local variables. Maximum is 2,048 bytes.

As you can see, 173 bytes or SRAM and 938 bytes of Flash are now used that should have been available to the programmer. The SRAM is of the most concern... That's almost 17% of the SRAM available on 1k chips!

And to trigger this bug, all you have to do is have a stray Serial library reference in and file you're including (or anything it includes) and goodbye 173 bytes of SRAM.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions