3 ms·
Why shocked? Certainly a micro-optimization, but order of things often matters if the compiler cannot prove it's safe to speculate about the execution order. Ma
by vnorilo 5y ago
Why shocked? Certainly a micro-optimization, but order of things often matters if the compiler cannot prove it's safe to speculate about the execution order. Maybe the surprising thing here is the theme of TFA, that the order of argument evaluation is not defined.
- simiones 5y agoGiven the out of order nature of processors, and the relatively limited scope for complex expressions given as function arguments, I would not expect this to matter in real-world programs. It seems this was also the opinion of the people drafting the new evaluation order changes [0] in C++17: > We do not believe that such a nondeterminism brings any substantial added optimization benefit, but it does perpetuate the confusion and hazards around order of evaluations in function calls.It perpetuates unnecessary confusion around brace-initialization vs. direct initialization using parenthesis. > We found that some entries in the benchmark suite ran slower, others ran faster compared to the scenario where the evaluation of the argument list is left unspecified. The variation is between -4% and +4%. It is worth noting that these results are for the worst case scenario where the optimizers have not yet been updated to be aware of, and take advantage of the new evaluation rules and they are blindly forced to evaluate function calls from left to right. It is clear that the left-to-right evaluation strategy is triggering new optimization paths (different inlining decisions and different register allocation) affecting the variations in the benchmark performance. It appears those opportunities have not traditionally been exploited, even though permitted under the unspecified order regime. Unfortunately, they seem to have been overruled by the Core Working Group [1], for reasons I have not been able to dig up. [0] http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0145r3.pdf http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0145r3.... [1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0400r0.html http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p040...