Skip to content

Commit da59a45

Browse files
committed
grin graph cache optimize
1 parent 7dd3cf5 commit da59a45

File tree

8 files changed

+59
-34
lines changed

8 files changed

+59
-34
lines changed

modules/graph/grin/src/index/order.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ bool grin_smaller_vertex(GRIN_GRAPH g, GRIN_VERTEX v1, GRIN_VERTEX v2) {
2626
size_t grin_get_position_of_vertex_from_sorted_list(GRIN_GRAPH g, GRIN_VERTEX_LIST vl, GRIN_VERTEX v) {
2727
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
2828
auto _vl = static_cast<GRIN_VERTEX_LIST_T*>(vl);
29-
auto vtype = (unsigned)_g->vertex_label(_GRIN_VERTEX_T(v)); // TODO: optimize after rebase
29+
auto bg = static_cast<GRIN_GRAPH_T*>(g);
30+
auto vtype = bg->cache->id_parser.GetLabelId(v);
3031
if (vtype < _vl->type_begin || vtype >= _vl->type_end) return GRIN_NULL_SIZE;
3132
auto offset = v - _vl->vrs[vtype - _vl->type_begin].begin_value();
3233
if (offset < _vl->vrs[vtype - _vl->type_begin].size()) {

modules/graph/grin/src/partition/partition.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ GRIN_GRAPH grin_get_local_graph_by_partition(GRIN_PARTITIONED_GRAPH pg, GRIN_PAR
105105
g->client.Connect(_pg->socket);
106106
g->_g = std::dynamic_pointer_cast<_GRIN_GRAPH_T>(g->client.GetObject(_pg->lgs[p]));
107107
g->g = g->_g.get();
108+
_prepare_cache(g);
108109
return g;
109110
}
110111
#endif

modules/graph/grin/src/partition/reference.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ GRIN_VERTEX grin_get_vertex_from_vertex_ref(GRIN_GRAPH g, GRIN_VERTEX_REF vr) {
3737

3838
GRIN_PARTITION grin_get_master_partition_from_vertex_ref(GRIN_GRAPH g, GRIN_VERTEX_REF vr) {
3939
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
40-
auto id_parser = vineyard::IdParser<GRIN_VERTEX_REF_T>(); //TODO optimize after rebase
41-
id_parser.Init(_g->fnum(), _g->vertex_label_num());
42-
return id_parser.GetFid(vr);
40+
auto bg = static_cast<GRIN_GRAPH_T*>(g);
41+
return bg->cache->id_parser.GetFid(vr);
4342
}
4443

4544
const char* grin_serialize_vertex_ref(GRIN_GRAPH g, GRIN_VERTEX_REF vr) {
@@ -74,12 +73,12 @@ GRIN_VERTEX_REF grin_deserialize_to_vertex_ref(GRIN_GRAPH g, const char* msg) {
7473

7574
bool grin_is_master_vertex(GRIN_GRAPH g, GRIN_VERTEX v) {
7675
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
77-
return _g->IsInnerVertex(_GRIN_VERTEX_T(v)); // TODO
76+
return _g->IsInnerVertex(_GRIN_VERTEX_T(v));
7877
}
7978

8079
bool grin_is_mirror_vertex(GRIN_GRAPH g, GRIN_VERTEX v) {
8180
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
82-
return _g->IsOuterVertex(_GRIN_VERTEX_T(v)); // TODO
81+
return _g->IsOuterVertex(_GRIN_VERTEX_T(v));
8382
}
8483
#endif
8584

modules/graph/grin/src/predefine.cc

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ const void* _get_value_from_vertex_property_table(GRIN_GRAPH g, GRIN_VERTEX_PROP
7272
grin_error_code = GRIN_ERROR_CODE::NO_ERROR;
7373
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
7474
auto _vpt = static_cast<GRIN_VERTEX_PROPERTY_TABLE_T*>(vpt);
75-
if (v < _vpt->vbegin || v >= _vpt->vend) {
75+
unsigned vtype = _grin_get_type_from_property(vp);
76+
if (v < _vpt->vbegin || v >= _vpt->vend || vtype != _vpt->vtype) {
7677
grin_error_code = GRIN_ERROR_CODE::INVALID_VALUE;
7778
return NULL;
78-
}
79-
unsigned vtype = _grin_get_type_from_property(vp);
79+
}
8080
unsigned vprop = _grin_get_prop_from_property(vp);
8181
auto offset = v - _vpt->vbegin;
8282
auto array = _g->vertex_data_table(vtype)->column(vprop)->chunk(0);
@@ -153,3 +153,25 @@ unsigned _grin_get_type_from_property(unsigned long long int prop) {
153153
unsigned _grin_get_prop_from_property(unsigned long long int prop) {
154154
return (unsigned)(prop & 0xffffffff);
155155
}
156+
157+
void _prepare_cache(GRIN_GRAPH_T* g) {
158+
g->cache = new _GRAPH_CACHE();
159+
g->cache->id_parser = vineyard::IdParser<_GRIN_GRAPH_T::vid_t>();
160+
g->cache->id_parser.Init(g->g->fnum(), g->g->vertex_label_num());
161+
162+
for (int i = 0; i < g->g->vertex_label_num(); ++i) {
163+
g->cache->vtype_names.push_back(g->g->schema().GetVertexLabelName(i));
164+
g->cache->vprop_names.push_back(std::vector<std::string>());
165+
for (int j = 0; j < g->g->vertex_property_num(i); ++j) {
166+
g->cache->vprop_names[i].push_back(g->g->schema().GetVertexPropertyName(i, j));
167+
}
168+
}
169+
170+
for (int i = 0; i < g->g->edge_label_num(); ++i) {
171+
g->cache->etype_names.push_back(g->g->schema().GetEdgeLabelName(i));
172+
g->cache->eprop_names.push_back(std::vector<std::string>());
173+
for (int j = 0; j < g->g->edge_property_num(i); ++j) {
174+
g->cache->eprop_names[i].push_back(g->g->schema().GetEdgePropertyName(i, j));
175+
}
176+
}
177+
}

modules/graph/grin/src/predefine.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,26 @@ unsigned _grin_get_prop_from_property(unsigned long long int);
9696
#define GRIN_VID_T uint64_t
9797

9898
/* The following data types shall be defined through typedef. */
99-
typedef vineyard::ArrowFragment<GRIN_OID_T, GRIN_VID_T> _GRIN_GRAPH_T;
99+
typedef vineyard::ArrowFragment<GRIN_OID_T, GRIN_VID_T> _GRIN_GRAPH_T;
100+
struct _GRAPH_CACHE {
101+
vineyard::IdParser<_GRIN_GRAPH_T::vid_t> id_parser;
102+
std::vector<std::string> vtype_names;
103+
std::vector<std::string> etype_names;
104+
std::vector<std::vector<std::string>> vprop_names;
105+
std::vector<std::vector<std::string>> eprop_names;
106+
};
107+
100108
struct GRIN_GRAPH_T {
101109
vineyard::Client client;
102110
std::shared_ptr<_GRIN_GRAPH_T> _g;
103111
_GRIN_GRAPH_T* g;
112+
_GRAPH_CACHE* cache;
104113
};
105-
typedef _GRIN_GRAPH_T::vertex_t _GRIN_VERTEX_T;
114+
115+
void _prepare_cache(GRIN_GRAPH_T* g);
116+
117+
typedef _GRIN_GRAPH_T::vertex_t _GRIN_VERTEX_T;
118+
106119
struct GRIN_EDGE_T {
107120
_GRIN_GRAPH_T::vid_t src;
108121
_GRIN_GRAPH_T::vid_t dst;

modules/graph/grin/src/property/property.cc

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@ extern "C" {
1717

1818
#ifdef GRIN_WITH_VERTEX_PROPERTY_NAME
1919
const char* grin_get_vertex_property_name(GRIN_GRAPH g, GRIN_VERTEX_TYPE vtype, GRIN_VERTEX_PROPERTY vp) {
20-
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
21-
auto s = _g->schema().GetVertexPropertyName(_grin_get_type_from_property(vp), _grin_get_prop_from_property(vp));
22-
int len = s.length() + 1;
23-
char* out = new char[len];
24-
snprintf(out, len, "%s", s.c_str());
25-
return out; // TODO: optimize after rebase, put strings in g.
20+
auto bg = static_cast<GRIN_GRAPH_T*>(g);
21+
return bg->cache->vprop_names[_grin_get_type_from_property(vp)][_grin_get_prop_from_property(vp)].c_str();
2622
}
2723

2824
GRIN_VERTEX_PROPERTY grin_get_vertex_property_by_name(GRIN_GRAPH g, GRIN_VERTEX_TYPE vtype,
@@ -54,12 +50,8 @@ GRIN_VERTEX_PROPERTY_LIST grin_get_vertex_properties_by_name(GRIN_GRAPH g, const
5450

5551
#ifdef GRIN_WITH_EDGE_PROPERTY_NAME
5652
const char* grin_get_edge_property_name(GRIN_GRAPH g, GRIN_EDGE_TYPE etype, GRIN_EDGE_PROPERTY ep) {
57-
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
58-
auto s = _g->schema().GetEdgePropertyName(_grin_get_type_from_property(ep), _grin_get_prop_from_property(ep));
59-
int len = s.length() + 1;
60-
char* out = new char[len];
61-
snprintf(out, len, "%s", s.c_str());
62-
return out; // TODO
53+
auto bg = static_cast<GRIN_GRAPH_T*>(g);
54+
return bg->cache->eprop_names[_grin_get_type_from_property(ep)][_grin_get_prop_from_property(ep)].c_str();
6355
}
6456

6557
GRIN_EDGE_PROPERTY grin_get_edge_property_by_name(GRIN_GRAPH g, GRIN_EDGE_TYPE etype,

modules/graph/grin/src/property/type.cc

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ bool grin_equal_vertex_type(GRIN_GRAPH g, GRIN_VERTEX_TYPE vt1, GRIN_VERTEX_TYPE
2222

2323
GRIN_VERTEX_TYPE grin_get_vertex_type(GRIN_GRAPH g, GRIN_VERTEX v) {
2424
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
25-
return _g->vertex_label(_GRIN_VERTEX_T(v)); // TODO
25+
auto bg = static_cast<GRIN_GRAPH_T*>(g);
26+
return bg->cache->id_parser.GetLabelId(v);
2627
}
2728

2829
void grin_destroy_vertex_type(GRIN_GRAPH g, GRIN_VERTEX_TYPE vt) {}
@@ -66,11 +67,8 @@ GRIN_VERTEX_TYPE grin_get_vertex_type_from_list(GRIN_GRAPH g, GRIN_VERTEX_TYPE_L
6667
#ifdef GRIN_WITH_VERTEX_TYPE_NAME
6768
const char* grin_get_vertex_type_name(GRIN_GRAPH g, GRIN_VERTEX_TYPE vtype) {
6869
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
69-
auto s = _g->schema().GetVertexLabelName(vtype);
70-
int len = s.length() + 1;
71-
char* out = new char[len];
72-
snprintf(out, len, "%s", s.c_str());
73-
return out; // TODO
70+
auto bg = static_cast<GRIN_GRAPH_T*>(g);
71+
return bg->cache->vtype_names[vtype].c_str();
7472
}
7573

7674
GRIN_VERTEX_TYPE grin_get_vertex_type_by_name(GRIN_GRAPH g, const char* name) {
@@ -144,11 +142,8 @@ GRIN_EDGE_TYPE grin_get_edge_type_from_list(GRIN_GRAPH g, GRIN_EDGE_TYPE_LIST et
144142
#ifdef GRIN_WITH_EDGE_TYPE_NAME
145143
const char* grin_get_edge_type_name(GRIN_GRAPH g, GRIN_EDGE_TYPE etype) {
146144
auto _g = static_cast<GRIN_GRAPH_T*>(g)->g;
147-
auto s = _g->schema().GetEdgeLabelName(etype);
148-
int len = s.length() + 1;
149-
char* out = new char[len];
150-
snprintf(out, len, "%s", s.c_str());
151-
return out; // TODO
145+
auto bg = static_cast<GRIN_GRAPH_T*>(g);
146+
return bg->cache->etype_names[etype].c_str();
152147
}
153148

154149
GRIN_EDGE_TYPE grin_get_edge_type_by_name(GRIN_GRAPH g, const char* name) {

modules/graph/grin/src/topology/structure.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ GRIN_GRAPH grin_get_graph_from_storage(int argc, char** argv) {
3333

3434
g->_g = std::dynamic_pointer_cast<_GRIN_GRAPH_T>(g->client.GetObject(obj_id));
3535
g->g = g->_g.get();
36+
_prepare_cache(g);
3637
return g;
3738
}
3839

3940
void grin_destroy_graph(GRIN_GRAPH g) {
4041
auto _g = static_cast<GRIN_GRAPH_T*>(g);
42+
delete _g->cache;
4143
delete _g;
4244
}
4345

0 commit comments

Comments
 (0)