Skip to content

Commit c1297f7

Browse files
authored
Optimize Entities capacity to avoid reallocation (#145)
* Optimize Entities capacity to avoid reallocation
1 parent 81302a1 commit c1297f7

File tree

4 files changed

+64
-16
lines changed

4 files changed

+64
-16
lines changed

src/main/java/com/redislabs/redisgraph/graph_entities/Edge.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,22 @@
99
public class Edge extends GraphEntity {
1010

1111
//members
12-
private String relationshipType;
12+
private String relationshipType;
1313
private long source;
1414
private long destination;
1515

16+
public Edge() {
17+
super();
18+
}
1619

20+
/**
21+
* Use this constructor to reduce memory allocations
22+
* when properties are added to the edge
23+
* @param propertiesCapacity preallocate the capacity for the properties
24+
*/
25+
public Edge(int propertiesCapacity) {
26+
super(propertiesCapacity);
27+
}
1728
//getters & setters
1829

1930
/**

src/main/java/com/redislabs/redisgraph/graph_entities/GraphEntity.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,20 @@
1010
public abstract class GraphEntity {
1111
//members
1212
protected long id;
13-
protected final Map<String, Property<?>> propertyMap = new HashMap<>();
13+
protected final Map<String, Property<?>> propertyMap;
1414

15+
public GraphEntity() {
16+
propertyMap = new HashMap<>();
17+
}
18+
19+
/**
20+
* Use this constructor to reduce memory allocations
21+
* when properties are added to the edge
22+
* @param propertiesCapacity preallocate the capacity for the properties
23+
*/
24+
public GraphEntity(int propertiesCapacity) {
25+
propertyMap = new HashMap<>(propertiesCapacity);
26+
}
1527

1628
//setters & getters
1729

src/main/java/com/redislabs/redisgraph/graph_entities/Node.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,24 @@
1010
public class Node extends GraphEntity {
1111

1212
//members
13-
private final List<String> labels = new ArrayList<>();
13+
private final List<String> labels;
14+
15+
public Node() {
16+
super();
17+
labels = new ArrayList<>();
18+
}
19+
20+
/**
21+
* Use this constructor to reduce memory allocations
22+
* when labels or properties are added to the node
23+
* @param labelsCapacity preallocate the capacity for the node labels
24+
* @param propertiesCapacity preallocate the capacity for the properties
25+
*/
26+
public Node(int labelsCapacity, int propertiesCapacity) {
27+
super(propertiesCapacity);
28+
this.labels = new ArrayList<>(labelsCapacity);
29+
}
30+
1431

1532
/**
1633
* @param label - a label to be add

src/main/java/com/redislabs/redisgraph/impl/resultset/ResultSetImpl.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,26 +140,28 @@ public Header getHeader() {
140140
*/
141141
@SuppressWarnings("unchecked")
142142
private Node deserializeNode(List<Object> rawNodeData) {
143-
Node node = new Node();
144-
deserializeGraphEntityId(node, rawNodeData.get(0));
143+
145144
List<Long> labelsIndices = (List<Long>) rawNodeData.get(1);
145+
List<List<Object>> rawProperties = (List<List<Object>>) rawNodeData.get(2);
146+
147+
Node node = new Node(labelsIndices.size(), rawProperties.size());
148+
deserializeGraphEntityId(node, (Long) rawNodeData.get(0));
149+
146150
for (Long labelIndex : labelsIndices) {
147151
String label = cache.getLabel(labelIndex.intValue(), redisGraph);
148152
node.addLabel(label);
149153
}
150-
deserializeGraphEntityProperties(node, (List<List<Object>>) rawNodeData.get(2));
151154

152-
return node;
155+
deserializeGraphEntityProperties(node, rawProperties);
153156

157+
return node;
154158
}
155159

156160
/**
157161
* @param graphEntity graph entity
158-
* @param rawEntityId raw representation of entity id to be set to the graph
159-
* entity
162+
* @param id entity id to be set to the graph entity
160163
*/
161-
private void deserializeGraphEntityId(GraphEntity graphEntity, Object rawEntityId) {
162-
long id = (Long) rawEntityId;
164+
private void deserializeGraphEntityId(GraphEntity graphEntity, long id) {
163165
graphEntity.setId(id);
164166
}
165167

@@ -172,16 +174,19 @@ private void deserializeGraphEntityId(GraphEntity graphEntity, Object rawEntityI
172174
*/
173175
@SuppressWarnings("unchecked")
174176
private Edge deserializeEdge(List<Object> rawEdgeData) {
175-
Edge edge = new Edge();
176-
deserializeGraphEntityId(edge, rawEdgeData.get(0));
177+
178+
List<List<Object>> rawProperties = (List<List<Object>>) rawEdgeData.get(4);
179+
180+
Edge edge = new Edge(rawProperties.size());
181+
deserializeGraphEntityId(edge, (Long) rawEdgeData.get(0));
177182

178183
String relationshipType = cache.getRelationshipType(((Long) rawEdgeData.get(1)).intValue(), redisGraph);
179184
edge.setRelationshipType(relationshipType);
180185

181186
edge.setSource((long) rawEdgeData.get(2));
182187
edge.setDestination((long) rawEdgeData.get(3));
183188

184-
deserializeGraphEntityProperties(edge, (List<List<Object>>) rawEdgeData.get(4));
189+
deserializeGraphEntityProperties(edge, rawProperties);
185190

186191
return edge;
187192
}
@@ -256,8 +261,11 @@ private Object deserializePoint(Object rawScalarData) {
256261
@SuppressWarnings("unchecked")
257262
private Map<String, Object> deserializeMap(Object rawScalarData) {
258263
List<Object> keyTypeValueEntries = (List<Object>) rawScalarData;
259-
Map<String, Object> map = new HashMap<>();
260-
for (int i = 0; i < keyTypeValueEntries.size(); i += 2) {
264+
265+
int size = keyTypeValueEntries.size();
266+
Map<String, Object> map = new HashMap<>(size >> 1); // set the capacity to half of the list
267+
268+
for (int i = 0; i < size; i += 2) {
261269
String key = SafeEncoder.encode((byte[]) keyTypeValueEntries.get(i));
262270
Object value = deserializeScalar((List<Object>) keyTypeValueEntries.get(i + 1));
263271
map.put(key, value);

0 commit comments

Comments
 (0)