Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Here, the simplification analysis uses the conditional hidden in the assert() macro to figure out that we only execute the urem instruction if cur < count (see the isImpliedByDomCondition() method). This can have the unexpected consequence of making builds with assertions enabled faster in some cases; we can restore performance by replacing disabled assertions with assume attributes, which have no runtime impact.

It surprises me that the compiler doesn't still take the inference from the assert and just disable emitting the code to perform the check. Over the last 15 years I've worked on many codebases that are written with unnecessary asserts, partly as documentation, but maybe because people assumed it helped the compiler.

I've also worked on many codebases (and written code like this on my own projects) where the code looks like: assert(condition); if (condition) { ... } because I still want that safety check in release builds, and want the exception in debug builds but absolutely not ever in release builds.



> It surprises me that the compiler doesn't still take the inference from the assert and just disable emitting the code to perform the check.

That's because that's what the <assert.h> assert() must do; it's specified to do and imply nothing when assertions are disabled. (the standard literally fully defines it as `#define assert(...) ((void)0)` when NDEBUG)

Whereas `[[assume(...)]]` is a thing specifically for that "infer things from this without actually emitting any code".


Yeah, good point. Honestly it's been so long since I've added that to a project (it's normally hidden in some include that everything else includes) that I'd forgotten it wasn't a compiler level reserved keyword for C++ code.


> It surprises me that the compiler doesn't still take the inference from the assert and just disable emitting the code to perform the check.

The compiler isn't as clever as I think you're envisioning: assert() only works that way because it exits the control flow if the statement isn't true.


My point is that even though the assert() is optimised out, the compiler could still assume that the condition is valid.


How? The assert() has no special significance to the compiler at all, it's just code. Usually it's just an empty macro for non-debug builds.

I guess you could define your assert() use [[assume]] in C++ for non-debug builds... but that seems like a very bad idea to me.

Just leave the asserts in prod. They almost certainly don't have measurable overhead. The few that do can be dealt with separately.

The Linux kernel has thousands of asserts which are always checked at runtime (BUG_ON).


There's an interesting comment on this over on Lobsters: https://lobste.rs/s/e4y5ps/two_studies_compiler_optimisation...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: