diff --git a/modules/graph/grin/src/index/order.cc b/modules/graph/grin/src/index/order.cc index c7dddf2f..64e5e543 100644 --- a/modules/graph/grin/src/index/order.cc +++ b/modules/graph/grin/src/index/order.cc @@ -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(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(g)->g, _vl); + if (likely(_vl->is_simple)) return v - _vl->begin_; auto _cache = static_cast(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 diff --git a/modules/graph/grin/src/predefine.h b/modules/graph/grin/src/predefine.h index 9c763e5f..ac10a994 100644 --- a/modules/graph/grin/src/predefine.h +++ b/modules/graph/grin/src/predefine.h @@ -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 struct GRIN_DATATYPE_ENUM { static constexpr GRIN_DATATYPE value = GRIN_DATATYPE::Undefined; diff --git a/modules/graph/grin/src/topology/adjacentlist.cc b/modules/graph/grin/src/topology/adjacentlist.cc index 7ed7f24c..670cea01 100644 --- a/modules/graph/grin/src/topology/adjacentlist.cc +++ b/modules/graph/grin/src/topology/adjacentlist.cc @@ -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(al); - if (_al->is_simple) return _al->end_ - _al->begin_; - if (_al->offsets.empty()) __grin_init_complex_adjacent_list(static_cast(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(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(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(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(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; @@ -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(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; @@ -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(g)->g, _al); + if (likely(_al->is_simple)) return GRIN_NULL_EDGE; + if (unlikely(_al->offsets.empty())) __grin_init_complex_adjacent_list(static_cast(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; @@ -132,7 +132,7 @@ void grin_get_next_adjacent_list_iter(GRIN_GRAPH g, GRIN_ADJACENT_LIST_ITERATOR auto _ali = static_cast(ali); _ali->current_++; if (_ali->current_ < _ali->end_) return; - if (_ali->is_simple) { + if (likely(_ali->is_simple)) { _ali->etype_current++; return; } diff --git a/modules/graph/grin/src/topology/vertexlist.cc b/modules/graph/grin/src/topology/vertexlist.cc index 0c598422..85e95578 100644 --- a/modules/graph/grin/src/topology/vertexlist.cc +++ b/modules/graph/grin/src/topology/vertexlist.cc @@ -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; } @@ -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(vl); if (_vl->is_simple) return _vl->end_ - _vl->begin_; - if (_vl->offsets.empty()) __grin_init_complex_vertex_list(static_cast(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(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(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; @@ -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(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(g)->g; _GRIN_GRAPH_T::vertex_range_t vr; _vli->vtype_current++;