← Back to context

Comment by im3w1l

5 hours ago

> No, that’s not why the /EHa option results in less efficient code. The possibility that any memory access or arithmetic operation could trigger an exception significantly impairs optimization opportunities. It means that all variables must be stable at the point memory accesses occur.

This is a good insight but I feel like stopping the analysis here is a little bit too early. We should also think about what they actually wanted to achieve. Did they actually need all variables to be stable at the point of any memory access? Maybe they want 90% of the benefits at 10% of the cost somehow?

> Did they actually need all variables to be stable at the point of any memory access?

One of the most important optimizations that a compiler can do is keeping a variable in a register and never even bother letting it hit memory in the first place. If every variable must get its own RAM address and the value at that RAM address must be faithful to a variable's "true" value at any given instruction, we should expect our software to slow down by an order of magnitude or two.

I don’t think there is a version of UB that gives you a predictable 90%, though. Either your program is exception-safe or it’s not. There’s no such thing as 90% safe.

  • A possible compromise could be to be able to tell the compiler, "I don't care about structured exceptions anywhere else, so do all your instruction reordering stuff there, but this one section of code I know could throw structured exceptions, so be more conservative here." It might need to generate duplicated code for some functions, though.

  • The majority of a program's runtime is usually spent in only a tiny section of its code. That is where optimization benefits are. If it helps to separate out that code and compile it with different compiler switches, the additional maintenance burden for the program structure and build system might be acceptable.

    • That's not a useful description of desktop "creative" software. Even though it might be true for audio that in many cases, the majority of the run time is spent handling the "process callback" from the audio subsystem, once the user starts actually working on things, the slow parts of the code (and the ones that impede the user or degrade their experience) are far removed from that core. This is a little less true of visual applications (video, drawing, image editing etc.) but I would imagine that similar considerations apply there too.

    • > The majority of a program's runtime is usually spent in only a tiny section of its code. That is where optimization benefits are.

      People who keep repeating this have only ever either looked at the profiles of zero or one types of programs.