[CMake] Set `-O2` instead of the default `-O3` for `Release` build type.
Hello!
I have checked and found that CMake by default sets -O3 -DNDEBUG for Release builds. This is an issue because -O3 is known to cause performance regressions and code bloating in many cases. Details with a regression example can be found in Arch Wiki: Makepkg#Higher_optimization_level.
I was wondering if this was intended? If not, then it should probably be explicitly set to -O2 -DNDEBUG as -O2 is the safe optimization default used by almost every C or C++ project. Moreover, I don't think a linker would benefit from -O3, and while this is safer on LLVM, GCC has a much more aggressive -O3, making Release builds somewhat unsafe.
I don't have any benchmarks and am honestly not sure how can I benchmark this, if you know how, please tell me!
Thanks!
Source: rui314/mold