diff --git a/src/qvi-bbuff.cc b/src/qvi-bbuff.cc index 9161590..d4480a6 100644 --- a/src/qvi-bbuff.cc +++ b/src/qvi-bbuff.cc @@ -31,7 +31,7 @@ struct qvi_bbuff_s { { capacity = min_growth; data = calloc(capacity, sizeof(byte_t)); - if (!data) throw qvi_runtime_error(); + if (qvi_unlikely(!data)) throw qvi_runtime_error(); } /** Copy constructor. */ qvi_bbuff_s( @@ -39,7 +39,7 @@ struct qvi_bbuff_s { ) : qvi_bbuff_s() { const int rc = qvi_bbuff_append(this, src.data, src.size); - if (rc != QV_SUCCESS) throw qvi_runtime_error(); + if (qvi_unlikely(rc != QV_SUCCESS)) throw qvi_runtime_error(); } /** Destructor. */ ~qvi_bbuff_s(void) @@ -95,7 +95,7 @@ qvi_bbuff_append( // New capacity. const size_t new_capacity = req_capacity + buff->min_growth; void *new_data = calloc(new_capacity, sizeof(byte_t)); - if (!new_data) return QV_ERR_OOR; + if (qvi_unlikely(!new_data)) return QV_ERR_OOR; // Memory allocation successful. memmove(new_data, buff->data, buff->size); free(buff->data); diff --git a/src/qvi-macros.h b/src/qvi-macros.h index 80d8ad4..6b6906a 100644 --- a/src/qvi-macros.h +++ b/src/qvi-macros.h @@ -17,6 +17,16 @@ #ifndef QVI_MACROS_H #define QVI_MACROS_H +/** + * Add branch prediction information: will likely happen. + */ +#define qvi_likely(x) __builtin_expect(!!(x), 1) + +/** + * Add branch prediction information: won't likely happen. + */ +#define qvi_unlikely(x) __builtin_expect(!!(x), 0) + /** * Convenience macro used to silence warnings about unused variables. * diff --git a/src/qvi-task.cc b/src/qvi-task.cc index ddb70ed..7bb2ebf 100644 --- a/src/qvi-task.cc +++ b/src/qvi-task.cc @@ -37,7 +37,7 @@ struct qvi_task_s { { std::string url; const int rc = qvi_url(url); - if (rc != QV_SUCCESS) { + if (qvi_unlikely(rc != QV_SUCCESS)) { qvi_log_error("{}", qvi_conn_ers()); return rc; } @@ -52,7 +52,7 @@ struct qvi_task_s { const int rc = qvi_rmi_task_get_cpubind( rmi, me(), ¤t_bind ); - if (rc != QV_SUCCESS) return rc; + if (qvi_unlikely(rc != QV_SUCCESS)) return rc; stack.push(qvi_hwloc_bitmap_s(current_bind)); hwloc_bitmap_free(current_bind); @@ -62,13 +62,13 @@ struct qvi_task_s { qvi_task_s(void) { int rc = qvi_rmi_client_new(&rmi); - if (rc != QV_SUCCESS) throw qvi_runtime_error(); + if (qvi_unlikely(rc != QV_SUCCESS)) throw qvi_runtime_error(); // Connect to our server. rc = connect_to_server(); - if (rc != QV_SUCCESS) throw qvi_runtime_error(); + if (qvi_unlikely(rc != QV_SUCCESS)) throw qvi_runtime_error(); // Initialize our bind stack. rc = bind_stack_init(); - if (rc != QV_SUCCESS) throw qvi_runtime_error(); + if (qvi_unlikely(rc != QV_SUCCESS)) throw qvi_runtime_error(); } /** Copy constructor. */ qvi_task_s(const qvi_task_s &src) = delete; @@ -91,7 +91,7 @@ struct qvi_task_s { const int rc = qvi_rmi_task_set_cpubind_from_cpuset( rmi, me(), bitmap_copy.cdata() ); - if (rc != QV_SUCCESS) return rc; + if (qvi_unlikely(rc != QV_SUCCESS)) return rc; // Push bitmap onto stack. stack.push(bitmap_copy); return rc;