logoalt Hacker News

derodero24today at 11:44 AM0 repliesview on HN

The assert-based optimization surprised me the first time I saw it in the wild. I had a hot path where I added an assert to document a precondition, and the generated code got measurably faster — the optimizer eliminated a downstream branch family it couldn't have known was dead otherwise.

The division UB case is thornier. In Rust you get explicit checked_div/wrapping_div, which makes the programmer's intent visible at the call site. The C approach of "trust that this is never zero" is the same mechanism but with none of the documentation.