Sho SHIMIZU
Committed by Gerrit Code Review

Performance improvement in deserialization of EncodableDiscreteResources

Over 1500x faster. This is for ONOS-4698

Before this patch
Benchmark                                   Mode  Cnt     Score   Error  Units
VlanSetSerialize.deserialize_compact_vlans  avgt  200  1681.625 ± 8.937  us/op
VlanSetSerialize.serialize_compact_vlans    avgt  200     1.532 ± 0.017  us/op

After this patch
Benchmark                                   Mode  Cnt  Score   Error  Units
VlanSetSerialize.deserialize_compact_vlans  avgt  200  1.070 ± 0.012  us/op
VlanSetSerialize.serialize_compact_vlans    avgt  200  1.613 ± 0.014  us/op

Change-Id: I2f00f7fbf25ee36555751e0bcff95d06b701f8c8
......@@ -66,7 +66,7 @@ final class EncodableDiscreteResources implements DiscreteResources {
return new EncodableDiscreteResources(parent, values);
}
private EncodableDiscreteResources(DiscreteResource parent, Map<Class<?>, EncodedDiscreteResources> map) {
EncodableDiscreteResources(DiscreteResource parent, Map<Class<?>, EncodedDiscreteResources> map) {
this.parent = parent;
this.map = map;
}
......
......@@ -21,8 +21,10 @@ import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import org.onosproject.net.resource.DiscreteResource;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
......@@ -41,9 +43,11 @@ class EncodableDiscreteResourcesSerializer extends Serializer<EncodableDiscreteR
@SuppressWarnings("unchecked")
Set<EncodedDiscreteResources> resources = kryo.readObject(input, LinkedHashSet.class);
return EncodableDiscreteResources.of(parent,
resources.stream()
.flatMap(x -> x.values(parent.id()).stream())
.collect(Collectors.toCollection(LinkedHashSet::new)));
return new EncodableDiscreteResources(parent, resources.stream()
.collect(Collectors.toMap(
EncodedDiscreteResources::encodedClass,
Function.identity(),
(v1, v2) -> v1,
LinkedHashMap::new)));
}
}
......
......@@ -76,6 +76,11 @@ final class EncodedDiscreteResources {
.collect(Collectors.toCollection(LinkedHashSet::new));
}
Class<?> encodedClass() {
Range<Integer> firstRange = rangeSet.asRanges().iterator().next();
return codec.decode(firstRange.lowerEndpoint()).getClass();
}
@SuppressWarnings("unchecked")
boolean contains(DiscreteResource resource) {
return resource.valueAs(Object.class)
......