Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve ABSL_ASSERT performance by guaranteeing it is optimized away …
…under NDEBUG in C++20 Also fix the old definition by verifying that the condition is contextually convertible to bool. The new C++20 definition reduces codegen bloat and guarantees fast performance while still retaining the expression (so that unused-variable warnings don't trigger). As an example where this makes a difference, compare the following snippet under `-DABSL_INTERNAL_CPLUSPLUS_LANG=202002`: https://godbolt.org/z/hjf59n84v ``` #include <stdlib.h> template<class T> struct S { S() { abort(); } static S const s; }; template<class T> S<T> const S<T>::s = {}; #if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L #define ABSL_ASSERT(expr) (decltype((expr) ? void() : void())()) #else #define ABSL_ASSERT(expr) (false ? ((expr) ? void() : void()) : void()) #endif void foo() { ABSL_ASSERT(((void)&S<int>::s, true)); } ``` We see that, in unoptimized builds, code is still generated with the old definition of `ABSL_ASSERT`. Moreover, even under optimizations (with `-O2`), the call to abort() still lingers with the old definition of `ABSL_ASSERT`. Therefore the extra generated code can affect both compile- and run-time performance. PiperOrigin-RevId: 681563573 Change-Id: I7fbfadcc7fd198e8e1daf14615c33687f6b23af7
- Loading branch information