Arduino smallest code config uploads and works, but won't work when optimized as fastest despite adequate program storage capacity.. What is happening?

What exactly is the optimization doing that would cause this sort of failure?

That’s not supposed to happen, and it usually indicates a bug in ProffieOS.
I would appreciate it if you post your config and ProffieOS version so I can debug it.

When you turn on higher levels optimizations, the compiler spends more time trying to figure out how to make things faster. There are lot of different ways this can happen, but what they all have in common is that they manipulate the program in ways that should do the same thing, but faster. These manipulations can become problematic if the program isn’t written quite right. How do you know if it’s safe to do B before A? Well, obviously you have to do A before B if B depends on A in some way. As long as the compiler can see that dependency, everything is fine, and it won’t try to re-order those operations. But if the dependency is not visible to the compiler, all bets are off…

It’s also possible that the compiler has bugs in it, causing it to forget, miss, or ignore that B depends on A, then re-ordering the operations anyways, but that’s unusual.

This is just one example obviously, but all bugs of this nature works similarly; most of the time the program doesn’t express what it’s doing in a way that’s safe against all types of optimizations.

We used to have an issue a long time ago where sounds would be played super slow when using certain compiler options. (32x slower in fact.) I’m still not entirely sure if my code was broken, but I re-wrote it in a way that gave the compilers less choices for optimizations, and the problem went away.

There is also another class of bugs that can be triggered by optimization: race conditions. The code could have a bug where if two things happen very close to each other, it could trigger a problem. WIth optimizations, things happen faster, and those two things could end up happen closer together more often, or possibly all the time.

These are pretty high-level descriptions of what’s going on. I could go into more detail of course, but I’m not sure how useful that would be.

1 Like

I’m not a programmer, so no need to get more detailed, that’s exactly the sort of answer I was looking for.

I will see if I can dig up or re-create the config that was giving me issues. I usually only save my stable configs lol.

Ironically, I’m most interested in the configs that don’t work…
(But not when it’s a simple user error, which can be difficult to know…)