Minimum compiled code is not really minimum

Creating new sketch, this code is automatically inserted:

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

}

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

}

ROM: 444
RAM: 9

This should be real compiled minimum code:

int main()
{}

ROM: 138
RAM: 0

I'm aware that some classes/functions are by default compiled and linked in first sample, however I believe these should be ignored by Arduino preprocessor and/or compiler as they are never used.

Most of that comes from the init() call:

int main() {
  init();
}

Compiled for Uno with Arduino AVR Boards 1.6.15:

Sketch uses 418 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.

Then we have this idiotic thing:

if (serialEventRun) serialEventRun();

Which is sitting there wasting memory and clock cycles in every single Arduino sketch even though serialEvent() is probably only used in 0.001% of those sketches. I felt so betrayed when I found that stupidity. I don't know what the they were thinking. I don't see any benefit over just writing:

if(Serial.available()) {

in your code like any sane person would do. So I define my own main in all my sketches now just so I can get rid of that.

So with init() and the serialEventRun() call:

int main() {
  init();
  for (;;) {
    if (serialEventRun) serialEventRun();
  }
}
Sketch uses 444 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.

(It actually ends up 4 bytes larger without the for loop.)

There used to be some overhead for the setup() and loop() calls in Arduino AVR Boards 1.6.11 and earlier because they weren't inlined but using the recent versions they are.

Whenever I start to "dissect" an application or compiler, many issues goes to the surface.

The quality of work sometimes moderately vary depending on how many different people work on the project. I have seen peaces of catastrophically badly written low level code, but as well extremely well - no difference, publicly available free or even expensive commercial sources.

Some authors take that pragmatically and made corrections, while most of them take that as insult and spitting on his personality, work... name it. Then it is the best to give up...

So far, so good. We will see further reactions.

int main() {}

Sure, but that would be just a C program, not an Arduino sketch.

it is the best to give up

Please do. Or rather, take more time to study what actually exists, what has been done before, and what has already been rejected, and what (people think) really matters in the Arduino community. Arduino is more than 10years old, and we've been over most of this before. It's not intended to be smallest, or fastest, or have the best IDE, or the most elegant libraries. It's not meant to be a serious IDE for seasoned programmers. It's not meant to be a stepping stone to becoming a real C programmer. The constant stream of "gee, the Arduino should do XXX" are ... tiring ... to those who suggested the same thing years ago, and ... gave up. Some things are still on the "maybe, someday" list (code completion, for example), but more efficient digitalWrite, smaller/faster UART code, formatted printing, and some others have been considered, even written, and ... rejected. And REALLY, no one cares at all how big the smallest possible sketch is, once compiled.
http://forum.arduino.cc/index.php/topic,5680.0.html

If you want to write better code for Arduino users, get involved with one of the projects to port the core and libraries to some other chip environment. Or work on the existing chips whose support is still in the "It doesn't quite actually work, yet" (Due, Zero, all those non-AVR chips have problems in their libraries (and especially 3rd party libraries) that are far more serious than matters of style and efficiency. (Or, there's also the non-arduino environments. I heat that mBed is swell, as long as you run it on the original NXP hardware, but troublesome on a lot of the other hardware that it is supposed to support these days.)

westfw:
Please do. Or rather, take more time to study what actually exists....

I will give up. I do not have enough time to search and read numerous posts 10 years ago with the same issue still persistent. I would rater use ATMEL Studio 7+, if they make Linux version in the future.

I would as well jump to the ARM platform, but still that require much more expensive base. While it is enough to make non-trivial project with 8-bit MCUs and required performance I will use them.

westfw:
If you want to write better code for Arduino users, get involved with...

No. I do not have a time for that. I believe there is many people here capable to do that and probably suggested they improvements, but still rejected. I would rater spend free time on my own compiler and IDE.

Yeah, we all have real jobs and our own projects too. If you're not willing to help then please don't complain. There are plenty of grown-up IDEs that you can use if you're not satisfied with this little kiddie toy.

Delta_G:
If you're not willing to help then please don't complain.

Fairly funny construction, indeed...

I had some free time to take a look and point some issues may be helpful for novices.

However, I pointed out as well serious problem with SD lib, pointed until now by many people years ago (including the author of the lib) and similar solution in few lines of code. I have no idea why that is not implemented years ago...

Etc. No more suggestions nor comments of any kind about Arduino software. It may be fine for kids, on other hand may be not - never mind.

pert:
Which is sitting there wasting memory and clock cycles in every single Arduino sketch even though serialEvent() is probably only used in 0.001% of those sketches.

That code has always been excised from main if serialEventRun is not defined1 and is currently excised even when serialEventRun is defined but empty2. That behaviour is the only clever thing I have seen out of the older linker.

1 Based on what I can remember.
2 Based on recent testing.

But it always will be defined(with the AVR core at least) because they define it in HardwareSerial.cpp.

Using Arduino IDE 1.6.13 w/ Arduino AVR Boards 1.6.15, compiling for Uno:

BareMinimum.ino compiled with stock main.cpp:

Sketch uses 444 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.

With the line

if (serialEventRun) serialEventRun();

commented out in main.cpp:

Sketch uses 432 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.

Good point! You can just add a blank serialEventRun definition to your sketch since it's weakly defined in the core. Now that loop and setup are being inlined that's a good alternative to writing my own main and actually may be better because I've never figured out how to call initVariant from the sketch.

Huh. I misinterpreted the code at the end of main. There are 12 bytes of useless code that are not removed by the optimizer (compare of constant zero to constant zero followed by a branch if true).

But, if an empty serialEventRun is defined in the sketch the optimizer does the right thing.

Nicely spotted.