Skip to content

Commit

Permalink
optimize if branch using likely
Browse files Browse the repository at this point in the history
  • Loading branch information
andydiwenzhu committed May 24, 2023
1 parent c5e5b3a commit a04fc92
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
6 changes: 2 additions & 4 deletions modules/graph/grin/src/index/order.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ bool grin_smaller_vertex(GRIN_GRAPH g, GRIN_VERTEX v1, GRIN_VERTEX v2) {
#if defined(GRIN_ASSUME_ALL_VERTEX_LIST_SORTED) && defined(GRIN_ENABLE_VERTEX_LIST_ARRAY)
size_t grin_get_position_of_vertex_from_sorted_list(GRIN_GRAPH g, GRIN_VERTEX_LIST vl, GRIN_VERTEX v) {
auto _vl = static_cast<GRIN_VERTEX_LIST_T*>(vl);
if (v < _vl->end_ && v >= _vl->begin_) return v - _vl->begin_;
if (_vl->is_simple) return GRIN_NULL_SIZE;
if (_vl->offsets.empty()) __grin_init_complex_vertex_list(static_cast<GRIN_GRAPH_T*>(g)->g, _vl);
if (likely(_vl->is_simple)) return v - _vl->begin_;
auto _cache = static_cast<GRIN_GRAPH_T*>(g)->cache;
auto vtype = _cache->id_parser.GetLabelId(v);
return v - _vl->offsets[vtype].second + _vl->offsets[vtype].first;
return v - _vl->offsets[vtype].second;
}
#endif
17 changes: 17 additions & 0 deletions modules/graph/grin/src/predefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ extern "C" {
#include "client/client.h"
#include "arrow/api.h"

#if (defined(__GNUC__) && (__GNUC__ >= 3)) || defined(__IBMC__) || \
defined(__INTEL_COMPILER) || defined(__clang__)
#ifndef unlikely
#define unlikely(x_) __builtin_expect(!!(x_), 0)
#endif
#ifndef likely
#define likely(x_) __builtin_expect(!!(x_), 1)
#endif
#else
#ifndef unlikely
#define unlikely(x_) (x_)
#endif
#ifndef likely
#define likely(x_) (x_)
#endif
#endif

template <typename T>
struct GRIN_DATATYPE_ENUM {
static constexpr GRIN_DATATYPE value = GRIN_DATATYPE::Undefined;
Expand Down
18 changes: 9 additions & 9 deletions modules/graph/grin/src/topology/adjacentlist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ void grin_destroy_adjacent_list(GRIN_GRAPH g, GRIN_ADJACENT_LIST al) {

size_t grin_get_adjacent_list_size(GRIN_GRAPH g, GRIN_ADJACENT_LIST al) {
auto _al = static_cast<GRIN_ADJACENT_LIST_T*>(al);
if (_al->is_simple) return _al->end_ - _al->begin_;
if (_al->offsets.empty()) __grin_init_complex_adjacent_list(static_cast<GRIN_GRAPH_T*>(g)->g, _al);
if (likely(_al->is_simple)) return _al->end_ - _al->begin_;
if (unlikely(_al->offsets.empty())) __grin_init_complex_adjacent_list(static_cast<GRIN_GRAPH_T*>(g)->g, _al);
return _al->offsets[_al->etype].first;
}

GRIN_VERTEX grin_get_neighbor_from_adjacent_list(GRIN_GRAPH g, GRIN_ADJACENT_LIST al, size_t idx) {
auto _al = static_cast<GRIN_ADJACENT_LIST_T*>(al);
auto nbr = _al->begin_ + idx;
if (nbr < _al->end_) return nbr->vid;
if (_al->is_simple) return GRIN_NULL_VERTEX;
if (_al->offsets.empty()) __grin_init_complex_adjacent_list(static_cast<GRIN_GRAPH_T*>(g)->g, _al);
if (likely(nbr < _al->end_)) return nbr->vid;
if (likely(_al->is_simple)) return GRIN_NULL_VERTEX;
if (unlikely(_al->offsets.empty())) __grin_init_complex_adjacent_list(static_cast<GRIN_GRAPH_T*>(g)->g, _al);
for (unsigned i = 0; i < _al->etype; ++i) {
if (idx < _al->offsets[i+1].first) {
nbr = _al->offsets[i].second + idx - _al->offsets[i].first;
Expand All @@ -66,7 +66,7 @@ GRIN_VERTEX grin_get_neighbor_from_adjacent_list(GRIN_GRAPH g, GRIN_ADJACENT_LIS
GRIN_EDGE grin_get_edge_from_adjacent_list(GRIN_GRAPH g, GRIN_ADJACENT_LIST al, size_t idx) {
auto _al = static_cast<GRIN_ADJACENT_LIST_T*>(al);
auto nbr = _al->begin_ + idx;
if (nbr < _al->end_) {
if (likely(nbr < _al->end_)) {
auto e = new GRIN_EDGE_T();
e->dir = _al->dir;
e->etype = _al->etype;
Expand All @@ -80,8 +80,8 @@ GRIN_EDGE grin_get_edge_from_adjacent_list(GRIN_GRAPH g, GRIN_ADJACENT_LIST al,
}
return e;
}
if (_al->is_simple) return GRIN_NULL_EDGE;
if (_al->offsets.empty()) __grin_init_complex_adjacent_list(static_cast<GRIN_GRAPH_T*>(g)->g, _al);
if (likely(_al->is_simple)) return GRIN_NULL_EDGE;
if (unlikely(_al->offsets.empty())) __grin_init_complex_adjacent_list(static_cast<GRIN_GRAPH_T*>(g)->g, _al);
for (unsigned i = 0; i < _al->etype; ++i) {
if (idx < _al->offsets[i+1].first) {
nbr = _al->offsets[i].second + idx - _al->offsets[i].first;
Expand Down Expand Up @@ -132,7 +132,7 @@ void grin_get_next_adjacent_list_iter(GRIN_GRAPH g, GRIN_ADJACENT_LIST_ITERATOR
auto _ali = static_cast<GRIN_ADJACENT_LIST_ITERATOR_T*>(ali);
_ali->current_++;
if (_ali->current_ < _ali->end_) return;
if (_ali->is_simple) {
if (likely(_ali->is_simple)) {
_ali->etype_current++;
return;
}
Expand Down
12 changes: 5 additions & 7 deletions modules/graph/grin/src/topology/vertexlist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ GRIN_VERTEX_LIST grin_get_vertex_list(GRIN_GRAPH g) {
__grin_init_simple_vertex_list(_g, vl);
} else {
vl->is_simple = false;
__grin_init_complex_vertex_list(_g, vl);
}
return vl;
}
Expand All @@ -42,16 +43,14 @@ void grin_destroy_vertex_list(GRIN_GRAPH g, GRIN_VERTEX_LIST vl) {
size_t grin_get_vertex_list_size(GRIN_GRAPH g, GRIN_VERTEX_LIST vl) {
auto _vl = static_cast<GRIN_VERTEX_LIST_T*>(vl);
if (_vl->is_simple) return _vl->end_ - _vl->begin_;
if (_vl->offsets.empty()) __grin_init_complex_vertex_list(static_cast<GRIN_GRAPH_T*>(g)->g, _vl);
return _vl->offsets[_vl->vtype].first;
}

GRIN_VERTEX grin_get_vertex_from_list(GRIN_GRAPH g, GRIN_VERTEX_LIST vl, size_t idx) {
auto _vl = static_cast<GRIN_VERTEX_LIST_T*>(vl);
auto v = _vl->begin_ + idx;
if (v < _vl->end_) return v;
if (_vl->is_simple) return GRIN_NULL_VERTEX;
if (_vl->offsets.empty()) __grin_init_complex_vertex_list(static_cast<GRIN_GRAPH_T*>(g)->g, _vl);
if (likely(v < _vl->end_)) return v;
if (likely(_vl->is_simple)) return GRIN_NULL_VERTEX;
for (unsigned i = 0; i < _vl->vtype; ++i) {
if (idx < _vl->offsets[i+1].first) {
v = _vl->offsets[i].second + idx - _vl->offsets[i].first;
Expand Down Expand Up @@ -89,12 +88,11 @@ void grin_destroy_vertex_list_iter(GRIN_GRAPH g, GRIN_VERTEX_LIST_ITERATOR vli)
void grin_get_next_vertex_list_iter(GRIN_GRAPH g, GRIN_VERTEX_LIST_ITERATOR vli) {
auto _vli = static_cast<GRIN_VERTEX_LIST_ITERATOR_T*>(vli);
_vli->current_++;
if (_vli->current_ < _vli->end_) return;
if (_vli->is_simple) {
if (likely(_vli->current_ < _vli->end_)) return;
if (likely(_vli->is_simple)) {
_vli->vtype_current++;
return;
}

auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
_GRIN_GRAPH_T::vertex_range_t vr;
_vli->vtype_current++;
Expand Down

0 comments on commit a04fc92

Please sign in to comment.