2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-09-05 00:55:24 +00:00

Stop providing branch prediction information

The __builtin_expect() can be used to provide the compiler with branch
prediction information.  The Gcc manual says[1] on the subject:

    In general, you should prefer to use actual profile feedback for
    this (-fprofile-arcs), as programmers are notoriously bad at
    predicting how their programs actually perform.

Stop using __builtin_expect() and ISC_LIKELY() and ISC_UNLIKELY() macros
to provide the branch prediction information as the performance testing
shows that named performs better when the __builtin_expect() is not
being used.

1. https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fexpect
This commit is contained in:
Ondřej Surý
2021-10-14 10:33:24 +02:00
parent 80fedf9231
commit e603983ec9
25 changed files with 114 additions and 180 deletions

View File

@@ -45,7 +45,6 @@ libisc_la_HEADERS = \
include/isc/iterated_hash.h \
include/isc/lang.h \
include/isc/lex.h \
include/isc/likely.h \
include/isc/list.h \
include/isc/log.h \
include/isc/magic.h \

View File

@@ -292,7 +292,7 @@ void
isc__buffer_putuint8(isc_buffer_t *b, uint8_t val) {
isc_result_t result;
REQUIRE(ISC_BUFFER_VALID(b));
if (ISC_UNLIKELY(b->autore)) {
if (b->autore) {
result = isc_buffer_reserve(&b, 1);
REQUIRE(result == ISC_R_SUCCESS);
}
@@ -326,7 +326,7 @@ void
isc__buffer_putuint16(isc_buffer_t *b, uint16_t val) {
isc_result_t result;
REQUIRE(ISC_BUFFER_VALID(b));
if (ISC_UNLIKELY(b->autore)) {
if (b->autore) {
result = isc_buffer_reserve(&b, 2);
REQUIRE(result == ISC_R_SUCCESS);
}
@@ -339,7 +339,7 @@ void
isc__buffer_putuint24(isc_buffer_t *b, uint32_t val) {
isc_result_t result;
REQUIRE(ISC_BUFFER_VALID(b));
if (ISC_UNLIKELY(b->autore)) {
if (b->autore) {
result = isc_buffer_reserve(&b, 3);
REQUIRE(result == ISC_R_SUCCESS);
}
@@ -375,7 +375,7 @@ void
isc__buffer_putuint32(isc_buffer_t *b, uint32_t val) {
isc_result_t result;
REQUIRE(ISC_BUFFER_VALID(b));
if (ISC_UNLIKELY(b->autore)) {
if (b->autore) {
result = isc_buffer_reserve(&b, 4);
REQUIRE(result == ISC_R_SUCCESS);
}
@@ -416,7 +416,7 @@ isc__buffer_putuint48(isc_buffer_t *b, uint64_t val) {
uint32_t vallo;
REQUIRE(ISC_BUFFER_VALID(b));
if (ISC_UNLIKELY(b->autore)) {
if (b->autore) {
result = isc_buffer_reserve(&b, 6);
REQUIRE(result == ISC_R_SUCCESS);
}
@@ -433,7 +433,7 @@ isc__buffer_putmem(isc_buffer_t *b, const unsigned char *base,
unsigned int length) {
isc_result_t result;
REQUIRE(ISC_BUFFER_VALID(b));
if (ISC_UNLIKELY(b->autore)) {
if (b->autore) {
result = isc_buffer_reserve(&b, length);
REQUIRE(result == ISC_R_SUCCESS);
}
@@ -455,7 +455,7 @@ isc__buffer_putstr(isc_buffer_t *b, const char *source) {
* Do not use ISC__BUFFER_PUTSTR(), so strlen is only done once.
*/
l = strlen(source);
if (ISC_UNLIKELY(b->autore)) {
if (b->autore) {
result = isc_buffer_reserve(&b, l);
REQUIRE(result == ISC_R_SUCCESS);
}
@@ -478,7 +478,7 @@ isc_buffer_putdecint(isc_buffer_t *b, int64_t v) {
/* xxxwpk do it more low-level way ? */
l = snprintf(buf, 21, "%" PRId64, v);
RUNTIME_CHECK(l <= 21);
if (ISC_UNLIKELY(b->autore)) {
if (b->autore) {
result = isc_buffer_reserve(&b, l);
REQUIRE(result == ISC_R_SUCCESS);
}
@@ -515,7 +515,7 @@ isc_buffer_copyregion(isc_buffer_t *b, const isc_region_t *r) {
REQUIRE(ISC_BUFFER_VALID(b));
REQUIRE(r != NULL);
if (ISC_UNLIKELY(b->autore)) {
if (b->autore) {
result = isc_buffer_reserve(&b, r->length);
if (result != ISC_R_SUCCESS) {
return (result);
@@ -623,7 +623,7 @@ isc_buffer_printf(isc_buffer_t *b, const char *format, ...) {
return (ISC_R_FAILURE);
}
if (ISC_UNLIKELY(b->autore)) {
if (b->autore) {
result = isc_buffer_reserve(&b, n + 1);
if (result != ISC_R_SUCCESS) {
return (result);

View File

@@ -15,7 +15,6 @@
#include "entropy_private.h"
#include "isc/hash.h" /* IWYU pragma: keep */
#include "isc/likely.h"
#include "isc/once.h"
#include "isc/random.h"
#include "isc/result.h"
@@ -74,7 +73,7 @@ static uint8_t maptolower[] = {
const void *
isc_hash_get_initializer(void) {
if (ISC_UNLIKELY(!hash_initialized)) {
if (!hash_initialized) {
RUNTIME_CHECK(
isc_once_do(&isc_hash_once, isc_hash_initialize) ==
ISC_R_SUCCESS);
@@ -91,7 +90,7 @@ isc_hash_set_initializer(const void *initializer) {
* Ensure that isc_hash_initialize() is not called after
* isc_hash_set_initializer() is called.
*/
if (ISC_UNLIKELY(!hash_initialized)) {
if (!hash_initialized) {
RUNTIME_CHECK(
isc_once_do(&isc_hash_once, isc_hash_initialize) ==
ISC_R_SUCCESS);

View File

@@ -31,7 +31,7 @@ isc_hmac_new(void) {
void
isc_hmac_free(isc_hmac_t *hmac) {
if (ISC_UNLIKELY(hmac == NULL)) {
if (hmac == NULL) {
return;
}
@@ -70,7 +70,7 @@ isc_result_t
isc_hmac_update(isc_hmac_t *hmac, const unsigned char *buf, const size_t len) {
REQUIRE(hmac != NULL);
if (ISC_UNLIKELY(buf == NULL || len == 0)) {
if (buf == NULL || len == 0) {
return (ISC_R_SUCCESS);
}

View File

@@ -16,7 +16,6 @@
#include <isc/attributes.h>
#include <isc/lang.h>
#include <isc/likely.h>
ISC_LANG_BEGINDECLS
@@ -42,25 +41,25 @@ const char *
isc_assertion_typetotext(isc_assertiontype_t type);
#define ISC_REQUIRE(cond) \
((void)(ISC_LIKELY(cond) || \
((void)((cond) || \
((isc_assertion_failed)(__FILE__, __LINE__, \
isc_assertiontype_require, #cond), \
0)))
#define ISC_ENSURE(cond) \
((void)(ISC_LIKELY(cond) || \
((void)((cond) || \
((isc_assertion_failed)(__FILE__, __LINE__, \
isc_assertiontype_ensure, #cond), \
0)))
#define ISC_INSIST(cond) \
((void)(ISC_LIKELY(cond) || \
((void)((cond) || \
((isc_assertion_failed)(__FILE__, __LINE__, \
isc_assertiontype_insist, #cond), \
0)))
#define ISC_INVARIANT(cond) \
((void)(ISC_LIKELY(cond) || \
((void)((cond) || \
((isc_assertion_failed)(__FILE__, __LINE__, \
isc_assertiontype_invariant, #cond), \
0)))

View File

@@ -104,7 +104,6 @@
#include <isc/assertions.h>
#include <isc/formatcheck.h>
#include <isc/lang.h>
#include <isc/likely.h>
#include <isc/magic.h>
#include <isc/types.h>
@@ -899,7 +898,7 @@ ISC_LANG_ENDDECLS
#define ISC__BUFFER_PUTMEM(_b, _base, _length) \
do { \
if (ISC_UNLIKELY((_b)->autore)) { \
if ((_b)->autore) { \
isc_buffer_t *_tmp = _b; \
ISC_REQUIRE(isc_buffer_reserve(&_tmp, _length) == \
ISC_R_SUCCESS); \
@@ -917,7 +916,7 @@ ISC_LANG_ENDDECLS
unsigned int _length; \
unsigned char *_cp; \
_length = (unsigned int)strlen(_source); \
if (ISC_UNLIKELY((_b)->autore)) { \
if ((_b)->autore) { \
isc_buffer_t *_tmp = _b; \
ISC_REQUIRE(isc_buffer_reserve(&_tmp, _length) == \
ISC_R_SUCCESS); \
@@ -933,7 +932,7 @@ ISC_LANG_ENDDECLS
unsigned char *_cp; \
/* evaluate (_val) only once */ \
uint8_t _val2 = (_val); \
if (ISC_UNLIKELY((_b)->autore)) { \
if ((_b)->autore) { \
isc_buffer_t *_tmp = _b; \
ISC_REQUIRE(isc_buffer_reserve(&_tmp, 1) == \
ISC_R_SUCCESS); \
@@ -949,7 +948,7 @@ ISC_LANG_ENDDECLS
unsigned char *_cp; \
/* evaluate (_val) only once */ \
uint16_t _val2 = (_val); \
if (ISC_UNLIKELY((_b)->autore)) { \
if ((_b)->autore) { \
isc_buffer_t *_tmp = _b; \
ISC_REQUIRE(isc_buffer_reserve(&_tmp, 2) == \
ISC_R_SUCCESS); \
@@ -966,7 +965,7 @@ ISC_LANG_ENDDECLS
unsigned char *_cp; \
/* evaluate (_val) only once */ \
uint32_t _val2 = (_val); \
if (ISC_UNLIKELY((_b)->autore)) { \
if ((_b)->autore) { \
isc_buffer_t *_tmp = _b; \
ISC_REQUIRE(isc_buffer_reserve(&_tmp, 3) == \
ISC_R_SUCCESS); \
@@ -984,7 +983,7 @@ ISC_LANG_ENDDECLS
unsigned char *_cp; \
/* evaluate (_val) only once */ \
uint32_t _val2 = (_val); \
if (ISC_UNLIKELY((_b)->autore)) { \
if ((_b)->autore) { \
isc_buffer_t *_tmp = _b; \
ISC_REQUIRE(isc_buffer_reserve(&_tmp, 4) == \
ISC_R_SUCCESS); \

View File

@@ -18,7 +18,6 @@
#include <isc/attributes.h>
#include <isc/formatcheck.h>
#include <isc/lang.h>
#include <isc/likely.h>
ISC_LANG_BEGINDECLS
@@ -44,7 +43,7 @@ ISC_NORETURN void
isc_error_runtimecheck(const char *, int, const char *);
#define ISC_ERROR_RUNTIMECHECK(cond) \
((void)(ISC_LIKELY(cond) || \
((void)((cond) || \
((isc_error_runtimecheck)(__FILE__, __LINE__, #cond), 0)))
ISC_LANG_ENDDECLS

View File

@@ -1,28 +0,0 @@
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
#pragma once
/*%
* Performance
*/
#ifdef CPPCHECK
#define ISC_LIKELY(x) (x)
#define ISC_UNLIKELY(x) (x)
#else /* ifdef CPPCHECK */
#ifdef HAVE_BUILTIN_EXPECT
#define ISC_LIKELY(x) __builtin_expect(!!(x), 1)
#define ISC_UNLIKELY(x) __builtin_expect(!!(x), 0)
#else /* ifdef HAVE_BUILTIN_EXPECT */
#define ISC_LIKELY(x) (x)
#define ISC_UNLIKELY(x) (x)
#endif /* ifdef HAVE_BUILTIN_EXPECT */
#endif /* ifdef CPPCHECK */

View File

@@ -11,8 +11,6 @@
#pragma once
#include <isc/likely.h>
/*! \file isc/magic.h */
typedef struct {
@@ -25,8 +23,7 @@ typedef struct {
* The intent of this is to allow magic numbers to be checked even though
* the object is otherwise opaque.
*/
#define ISC_MAGIC_VALID(a, b) \
(ISC_LIKELY((a) != NULL) && \
ISC_LIKELY(((const isc__magic_t *)(a))->magic == (b)))
#define ISC_MAGIC_VALID(a, b) \
((a) != NULL && ((const isc__magic_t *)(a))->magic == (b))
#define ISC_MAGIC(a, b, c, d) ((a) << 24 | (b) << 16 | (c) << 8 | (d))

View File

@@ -195,7 +195,6 @@
/*%
* Performance
*/
#include <isc/likely.h>
#ifdef HAVE_BUILTIN_UNREACHABLE
#define ISC_UNREACHABLE() __builtin_unreachable();

View File

@@ -29,7 +29,7 @@ isc_md_new(void) {
void
isc_md_free(isc_md_t *md) {
if (ISC_UNLIKELY(md == NULL)) {
if (md == NULL) {
return;
}
@@ -66,7 +66,7 @@ isc_result_t
isc_md_update(isc_md_t *md, const unsigned char *buf, const size_t len) {
REQUIRE(md != NULL);
if (ISC_UNLIKELY(buf == NULL || len == 0)) {
if (buf == NULL || len == 0) {
return (ISC_R_SUCCESS);
}

View File

@@ -187,9 +187,8 @@ struct isc_mempool {
#else /* if !ISC_MEM_TRACKLINES */
#define TRACE_OR_RECORD (ISC_MEM_DEBUGTRACE | ISC_MEM_DEBUGRECORD)
#define SHOULD_TRACE_OR_RECORD(ptr) \
(ISC_UNLIKELY((isc_mem_debugging & TRACE_OR_RECORD) != 0) && \
ptr != NULL)
#define SHOULD_TRACE_OR_RECORD(ptr) \
((isc_mem_debugging & TRACE_OR_RECORD) != 0 && ptr != NULL)
#define ADD_TRACE(a, b, c, d, e) \
if (SHOULD_TRACE_OR_RECORD(b)) { \
@@ -303,8 +302,8 @@ delete_trace_entry(isc_mem_t *mctx, const void *ptr, size_t size,
idx = hash % DEBUG_TABLE_COUNT;
dl = ISC_LIST_HEAD(mctx->debuglist[idx]);
while (ISC_LIKELY(dl != NULL)) {
if (ISC_UNLIKELY(dl->ptr == ptr)) {
while (dl != NULL) {
if (dl->ptr == ptr) {
ISC_LIST_UNLINK(mctx->debuglist[idx], dl, link);
decrement_malloced(mctx, sizeof(*dl));
sdallocx(dl, sizeof(*dl), 0);
@@ -325,7 +324,7 @@ unlock:
#endif /* ISC_MEM_TRACKLINES */
#define ADJUST_ZERO_ALLOCATION_SIZE(s) \
if (ISC_UNLIKELY(s == 0)) { \
if (s == 0) { \
s = ZERO_ALLOCATION_SIZE; \
}
@@ -341,7 +340,7 @@ mem_get(isc_mem_t *ctx, size_t size) {
ret = mallocx(size, 0);
INSIST(ret != NULL);
if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0)) {
if ((ctx->flags & ISC_MEMFLAG_FILL) != 0) {
memset(ret, 0xbe, size); /* Mnemonic for "beef". */
}
@@ -356,7 +355,7 @@ static inline void
mem_put(isc_mem_t *ctx, void *mem, size_t size) {
ADJUST_ZERO_ALLOCATION_SIZE(size);
if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0)) {
if ((ctx->flags & ISC_MEMFLAG_FILL) != 0) {
memset(mem, 0xde, size); /* Mnemonic for "dead". */
}
sdallocx(mem, size, 0);
@@ -371,7 +370,7 @@ mem_realloc(isc_mem_t *ctx, void *old_ptr, size_t old_size, size_t new_size) {
new_ptr = rallocx(old_ptr, new_size, 0);
INSIST(new_ptr != NULL);
if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0)) {
if ((ctx->flags & ISC_MEMFLAG_FILL) != 0) {
ssize_t diff_size = new_size - old_size;
void *diff_ptr = (uint8_t *)new_ptr + old_size;
if (diff_size > 0) {
@@ -483,7 +482,7 @@ mem_create(isc_mem_t **ctxp, unsigned int flags) {
ISC_LIST_INIT(ctx->pools);
#if ISC_MEM_TRACKLINES
if (ISC_UNLIKELY((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0)) {
if ((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0) {
unsigned int i;
ctx->debuglist =
@@ -524,7 +523,7 @@ destroy(isc_mem_t *ctx) {
INSIST(ISC_LIST_EMPTY(ctx->pools));
#if ISC_MEM_TRACKLINES
if (ISC_UNLIKELY(ctx->debuglist != NULL)) {
if (ctx->debuglist != NULL) {
debuglink_t *dl;
for (i = 0; i < DEBUG_TABLE_COUNT; i++) {
for (dl = ISC_LIST_HEAD(ctx->debuglist[i]); dl != NULL;
@@ -903,10 +902,10 @@ isc__mem_reget(isc_mem_t *ctx, void *old_ptr, size_t old_size,
size_t new_size FLARG) {
void *new_ptr = NULL;
if (ISC_UNLIKELY(old_ptr == NULL)) {
if (old_ptr == NULL) {
REQUIRE(old_size == 0);
new_ptr = isc__mem_get(ctx, new_size FLARG_PASS);
} else if (ISC_UNLIKELY(new_size == 0)) {
} else if (new_size == 0) {
isc__mem_put(ctx, old_ptr, old_size FLARG_PASS);
} else {
DELETE_TRACE(ctx, old_ptr, old_size, file, line);
@@ -935,9 +934,9 @@ isc__mem_reallocate(isc_mem_t *ctx, void *old_ptr, size_t new_size FLARG) {
REQUIRE(VALID_CONTEXT(ctx));
if (ISC_UNLIKELY(old_ptr == NULL)) {
if (old_ptr == NULL) {
new_ptr = isc__mem_allocate(ctx, new_size FLARG_PASS);
} else if (ISC_UNLIKELY(new_size == 0)) {
} else if (new_size == 0) {
isc__mem_free(ctx, old_ptr FLARG_PASS);
} else {
size_t old_size = sallocx(old_ptr, 0);
@@ -1289,7 +1288,7 @@ isc__mempool_get(isc_mempool_t *restrict mpctx FLARG) {
mpctx->allocated++;
if (ISC_UNLIKELY(mpctx->items == NULL)) {
if (mpctx->items == NULL) {
isc_mem_t *mctx = mpctx->mctx;
const size_t fillcount = mpctx->fillcount;
/*
@@ -1442,7 +1441,7 @@ isc__mem_checkdestroyed(void) {
LOCK(&contextslock);
if (!ISC_LIST_EMPTY(contexts)) {
#if ISC_MEM_TRACKLINES
if (ISC_UNLIKELY((isc_mem_debugging & TRACE_OR_RECORD) != 0)) {
if ((isc_mem_debugging & TRACE_OR_RECORD) != 0) {
print_contexts(file);
}
#endif /* if ISC_MEM_TRACKLINES */

View File

@@ -126,7 +126,7 @@ tls_senddone(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
/* We are tying to avoid a memory allocation for small write
* requests. See the mirroring code in the tls_send_outgoing()
* function. */
if (ISC_UNLIKELY(send_req->data.length > sizeof(send_req->smallbuf))) {
if (send_req->data.length > sizeof(send_req->smallbuf)) {
isc_mem_put(handle->sock->mgr->mctx, send_req->data.base,
send_req->data.length);
} else {
@@ -247,7 +247,7 @@ tls_send_outgoing(isc_nmsocket_t *sock, bool finish, isc_nmhandle_t *tlshandle,
.data.length = pending };
/* Let's try to avoid a memory allocation for small write requests */
if (ISC_UNLIKELY((size_t)pending > sizeof(send_req->smallbuf))) {
if ((size_t)pending > sizeof(send_req->smallbuf)) {
send_req->data.base = isc_mem_get(sock->mgr->mctx, pending);
} else {
send_req->data.base = &send_req->smallbuf[0];