Trust your compiler: Modern C++
This technical deep-dive into modern C++ challenges long-held performance beliefs, demonstrating that today's compilers often optimize clear, idiomatic code as effectively, if not more so, than traditional hand-optimizations. The author provides empirical evidence across several C++ features, encouraging developers to embrace modern language constructs and trust their compilers. It's a popular topic on HN, pitting old-school C++ wisdom against the evolving capabilities of modern toolchains.
The Lowdown
The article "Trust your compiler: Modern C++" critically examines and debunks several common performance myths prevalent among C++ programmers, arguing that contemporary compilers are sophisticated enough to optimize straightforward, intent-driven code to a level that often matches or surpasses
The Gossip
Ranges & Readability Rumble
The discussion around C++ ranges and algorithms highlights a perennial debate: readability versus conciseness. Some users find raw `for` loops more explicit and readable for simple tasks than `std::accumulate` or ranges, questioning the article's premise that these constructs inherently improve readability. Others counter that ranges, especially with `std::reduce`, offer superior expressiveness for complex data pipelines and can facilitate parallelization, making them clearer for developers familiar with functional programming paradigms. The consensus acknowledges that while the performance might be similar, the choice often boils down to style and familiarity.
Fast-Math & Floating Point Follies
Commenters raised concerns about the use of `-ffast-math` in the benchmarks, pointing out that this flag trades floating-point precision for speed and cannot be universally applied in production code due to potential accuracy issues. This led to a broader discussion on the complexities of floating-point arithmetic and IEEE-754 nuances in C++. Some users highlighted Rust's approach of offering explicit 'algebraic' operators as a way to manage performance vs. precision tradeoffs in a more controlled manner, rather than relying on a compiler-wide flag.
Compiler Complexities & Canine Trust Issues
A significant portion of the discussion centered on the provocative title, 'Trust your compiler.' Skeptics expressed distrust, citing the sheer complexity of modern compilers, their perceived inability to fully utilize modern CPU opcodes, and the potential for 'invisible backdoor injectors.' Some argued that the complexity of C++ itself makes it difficult to ascertain what the compiler is truly doing. Conversely, proponents defended compiler sophistication, arguing that their complexity is a necessary trade-off if it simplifies application-level code, and that much of the instruction set bloat is due to hardware designers ensuring backward compatibility.
Polymorphism Performance Ponderings
The section on virtual versus static polymorphism sparked some nuanced debate. One commenter questioned the article's assertion that `std::visit` necessarily lowers to a switch, suggesting it might still involve virtual calls. However, the article's primary point—that data locality, often improved by `std::variant` over heap-allocated polymorphic objects, is a far greater performance factor than the dispatch mechanism itself—was generally accepted, underscoring that design choices influencing memory layout have profound performance implications.
Exception Evasion & Error-Handling Efficacy
The topic of exception handling performance, a classic C++ concern, saw general agreement with the article's nuanced take. Commenters acknowledged that while exceptions have minimal overhead when not thrown, they become significantly expensive upon being activated. `std::expected` was widely endorsed as a superior alternative for handling anticipated error conditions, offering better performance and clearer intent compared to exceptions or raw error codes. Mentions of future C++ proposals for 'better exceptions' also surfaced, indicating ongoing evolution in error-handling strategies.