Skip to content

Commit

Permalink
Add branch prediction information and start using it. (#226)
Browse files Browse the repository at this point in the history
Signed-off-by: Samuel K. Gutierrez <[email protected]>
  • Loading branch information
samuelkgutierrez authored Jul 14, 2024
1 parent de44bb6 commit a41578f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/qvi-bbuff.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ 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(
const qvi_bbuff_s &src
) : 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)
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions src/qvi-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
12 changes: 6 additions & 6 deletions src/qvi-task.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -52,7 +52,7 @@ struct qvi_task_s {
const int rc = qvi_rmi_task_get_cpubind(
rmi, me(), &current_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);
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit a41578f

Please sign in to comment.