Use compiler-provided assume builtins when available
There are several benefits to this: 1. It's cleaner and more reliable to use the builtin to inform the compiler of assumptions instead of hoping that the optimizer understands your intentions. 2. `clang` will warn you if any of your assumptions would produce side-effects (which the compiler will discard). [This blog post](https://fastcompression.blogspot.com/2019/01/compiler-checked-contracts.html) by Yann Collet highlights that a hazard of using the `unreachable()`-based method of signaling assumptions is that it can sometimes result in additional instructions being generated (see [this Godbolt link](https://godbolt.org/z/lKNMs3) from the blog post for an example).
This commit is contained in:
parent
3aae792b10
commit
4f50f782fa
@ -378,7 +378,7 @@ arena_dalloc(tsdn_t *tsdn, void *ptr, tcache_t *tcache,
|
||||
if (caller_alloc_ctx != NULL) {
|
||||
alloc_ctx = *caller_alloc_ctx;
|
||||
} else {
|
||||
util_assume(!tsdn_null(tsdn));
|
||||
util_assume(tsdn != NULL);
|
||||
emap_alloc_ctx_lookup(tsdn, &arena_emap_global, ptr,
|
||||
&alloc_ctx);
|
||||
}
|
||||
|
@ -65,12 +65,19 @@ get_errno(void) {
|
||||
#endif
|
||||
}
|
||||
|
||||
JEMALLOC_ALWAYS_INLINE void
|
||||
util_assume(bool b) {
|
||||
if (!b) {
|
||||
unreachable();
|
||||
}
|
||||
}
|
||||
#ifdef _MSC_VER
|
||||
#define util_assume __assume
|
||||
#elif defined(__clang__) && (__clang_major__ > 3 || \
|
||||
(__clang_major__ == 3 && __clang_minor__ >= 6))
|
||||
#define util_assume __builtin_assume
|
||||
#else
|
||||
#define util_assume(expr) \
|
||||
do { \
|
||||
if (!(expr)) { \
|
||||
unreachable(); \
|
||||
} \
|
||||
} while(0)
|
||||
#endif
|
||||
|
||||
/* ptr should be valid. */
|
||||
JEMALLOC_ALWAYS_INLINE void
|
||||
|
Loading…
Reference in New Issue
Block a user