Skip to content

Commit

Permalink
Optimize Entities capacity to avoid reallocation (#145)
Browse files Browse the repository at this point in the history
* Optimize Entities capacity to avoid reallocation
  • Loading branch information
sazzad16 authored Aug 14, 2022
1 parent 81302a1 commit c1297f7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
13 changes: 12 additions & 1 deletion src/main/java/com/redislabs/redisgraph/graph_entities/Edge.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@
public class Edge extends GraphEntity {

//members
private String relationshipType;
private String relationshipType;
private long source;
private long destination;

public Edge() {
super();
}

/**
* Use this constructor to reduce memory allocations
* when properties are added to the edge
* @param propertiesCapacity preallocate the capacity for the properties
*/
public Edge(int propertiesCapacity) {
super(propertiesCapacity);
}
//getters & setters

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,20 @@
public abstract class GraphEntity {
//members
protected long id;
protected final Map<String, Property<?>> propertyMap = new HashMap<>();
protected final Map<String, Property<?>> propertyMap;

public GraphEntity() {
propertyMap = new HashMap<>();
}

/**
* Use this constructor to reduce memory allocations
* when properties are added to the edge
* @param propertiesCapacity preallocate the capacity for the properties
*/
public GraphEntity(int propertiesCapacity) {
propertyMap = new HashMap<>(propertiesCapacity);
}

//setters & getters

Expand Down
19 changes: 18 additions & 1 deletion src/main/java/com/redislabs/redisgraph/graph_entities/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,24 @@
public class Node extends GraphEntity {

//members
private final List<String> labels = new ArrayList<>();
private final List<String> labels;

public Node() {
super();
labels = new ArrayList<>();
}

/**
* Use this constructor to reduce memory allocations
* when labels or properties are added to the node
* @param labelsCapacity preallocate the capacity for the node labels
* @param propertiesCapacity preallocate the capacity for the properties
*/
public Node(int labelsCapacity, int propertiesCapacity) {
super(propertiesCapacity);
this.labels = new ArrayList<>(labelsCapacity);
}


/**
* @param label - a label to be add
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,26 +140,28 @@ public Header getHeader() {
*/
@SuppressWarnings("unchecked")
private Node deserializeNode(List<Object> rawNodeData) {
Node node = new Node();
deserializeGraphEntityId(node, rawNodeData.get(0));

List<Long> labelsIndices = (List<Long>) rawNodeData.get(1);
List<List<Object>> rawProperties = (List<List<Object>>) rawNodeData.get(2);

Node node = new Node(labelsIndices.size(), rawProperties.size());
deserializeGraphEntityId(node, (Long) rawNodeData.get(0));

for (Long labelIndex : labelsIndices) {
String label = cache.getLabel(labelIndex.intValue(), redisGraph);
node.addLabel(label);
}
deserializeGraphEntityProperties(node, (List<List<Object>>) rawNodeData.get(2));

return node;
deserializeGraphEntityProperties(node, rawProperties);

return node;
}

/**
* @param graphEntity graph entity
* @param rawEntityId raw representation of entity id to be set to the graph
* entity
* @param id entity id to be set to the graph entity
*/
private void deserializeGraphEntityId(GraphEntity graphEntity, Object rawEntityId) {
long id = (Long) rawEntityId;
private void deserializeGraphEntityId(GraphEntity graphEntity, long id) {
graphEntity.setId(id);
}

Expand All @@ -172,16 +174,19 @@ private void deserializeGraphEntityId(GraphEntity graphEntity, Object rawEntityI
*/
@SuppressWarnings("unchecked")
private Edge deserializeEdge(List<Object> rawEdgeData) {
Edge edge = new Edge();
deserializeGraphEntityId(edge, rawEdgeData.get(0));

List<List<Object>> rawProperties = (List<List<Object>>) rawEdgeData.get(4);

Edge edge = new Edge(rawProperties.size());
deserializeGraphEntityId(edge, (Long) rawEdgeData.get(0));

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

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

deserializeGraphEntityProperties(edge, (List<List<Object>>) rawEdgeData.get(4));
deserializeGraphEntityProperties(edge, rawProperties);

return edge;
}
Expand Down Expand Up @@ -256,8 +261,11 @@ private Object deserializePoint(Object rawScalarData) {
@SuppressWarnings("unchecked")
private Map<String, Object> deserializeMap(Object rawScalarData) {
List<Object> keyTypeValueEntries = (List<Object>) rawScalarData;
Map<String, Object> map = new HashMap<>();
for (int i = 0; i < keyTypeValueEntries.size(); i += 2) {

int size = keyTypeValueEntries.size();
Map<String, Object> map = new HashMap<>(size >> 1); // set the capacity to half of the list

for (int i = 0; i < size; i += 2) {
String key = SafeEncoder.encode((byte[]) keyTypeValueEntries.get(i));
Object value = deserializeScalar((List<Object>) keyTypeValueEntries.get(i + 1));
map.put(key, value);
Expand Down

0 comments on commit c1297f7

Please sign in to comment.