Skip to content
This repository was archived by the owner on Aug 23, 2020. It is now read-only.

Commit fc7dadb

Browse files
acha-billGal Rogozinski
authored and
Gal Rogozinski
committed
Memory optimization for serializing db objects (#1656)
1 parent 52b6330 commit fc7dadb

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/main/java/com/iota/iri/model/StateDiff.java

+19-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.Arrays;
99
import java.util.HashMap;
1010
import java.util.Map;
11+
import java.util.Map.Entry;
1112

1213
/**
1314
* Creates a persistable State object, used to map addresses with values in the DB and snapshots.
@@ -22,11 +23,24 @@ public class StateDiff implements Persistable {
2223
* a new empty byte array is returned instead.
2324
*/
2425
@Override
25-
public byte[] bytes() {
26-
return state.entrySet().parallelStream()
27-
.map(entry -> ArrayUtils.addAll(entry.getKey().bytes(), Serializer.serialize(entry.getValue())))
28-
.reduce(ArrayUtils::addAll)
29-
.orElse(new byte[0]);
26+
public byte[] bytes(){
27+
int size = state.size();
28+
if (size == 0) {
29+
return new byte[0];
30+
}
31+
32+
byte[] temp = new byte[size * (Hash.SIZE_IN_BYTES + Long.BYTES)];
33+
int index = 0;
34+
for (Entry<Hash,Long> entry : state.entrySet()){
35+
byte[] key = entry.getKey().bytes();
36+
System.arraycopy(key, 0, temp, index, key.length);
37+
index += key.length;
38+
39+
byte[] value = Serializer.serialize(entry.getValue());
40+
System.arraycopy(value, 0, temp, index, value.length);
41+
index += value.length;
42+
}
43+
return temp;
3044
}
3145

3246
/**

0 commit comments

Comments
 (0)