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

Memory optimization of StateDiff.bytes() to only use 1 new byte array instead of stateMap.size * 2 arrays #1656

Merged
merged 1 commit into from
Nov 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/main/java/com/iota/iri/model/StateDiff.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
* Creates a persistable State object, used to map addresses with values in the DB and snapshots.
Expand All @@ -22,11 +23,24 @@ public class StateDiff implements Persistable {
* a new empty byte array is returned instead.
*/
@Override
public byte[] bytes() {
return state.entrySet().parallelStream()
.map(entry -> ArrayUtils.addAll(entry.getKey().bytes(), Serializer.serialize(entry.getValue())))
.reduce(ArrayUtils::addAll)
.orElse(new byte[0]);
public byte[] bytes(){
int size = state.size();
if (size == 0) {
return new byte[0];
}

byte[] temp = new byte[size * (Hash.SIZE_IN_BYTES + Long.BYTES)];
int index = 0;
for (Entry<Hash,Long> entry : state.entrySet()){
byte[] key = entry.getKey().bytes();
System.arraycopy(key, 0, temp, index, key.length);
index += key.length;

byte[] value = Serializer.serialize(entry.getValue());
System.arraycopy(value, 0, temp, index, value.length);
index += value.length;
}
return temp;
}

/**
Expand Down