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 { ...@@ -66,7 +66,7 @@ final class EncodableDiscreteResources implements DiscreteResources {
66 return new EncodableDiscreteResources(parent, values); 66 return new EncodableDiscreteResources(parent, values);
67 } 67 }
68 68
69 - private EncodableDiscreteResources(DiscreteResource parent, Map<Class<?>, EncodedDiscreteResources> map) { 69 + EncodableDiscreteResources(DiscreteResource parent, Map<Class<?>, EncodedDiscreteResources> map) {
70 this.parent = parent; 70 this.parent = parent;
71 this.map = map; 71 this.map = map;
72 } 72 }
......
...@@ -21,8 +21,10 @@ import com.esotericsoftware.kryo.io.Input; ...@@ -21,8 +21,10 @@ import com.esotericsoftware.kryo.io.Input;
21 import com.esotericsoftware.kryo.io.Output; 21 import com.esotericsoftware.kryo.io.Output;
22 import org.onosproject.net.resource.DiscreteResource; 22 import org.onosproject.net.resource.DiscreteResource;
23 23
24 +import java.util.LinkedHashMap;
24 import java.util.LinkedHashSet; 25 import java.util.LinkedHashSet;
25 import java.util.Set; 26 import java.util.Set;
27 +import java.util.function.Function;
26 import java.util.stream.Collectors; 28 import java.util.stream.Collectors;
27 29
28 /** 30 /**
...@@ -41,9 +43,11 @@ class EncodableDiscreteResourcesSerializer extends Serializer<EncodableDiscreteR ...@@ -41,9 +43,11 @@ class EncodableDiscreteResourcesSerializer extends Serializer<EncodableDiscreteR
41 @SuppressWarnings("unchecked") 43 @SuppressWarnings("unchecked")
42 Set<EncodedDiscreteResources> resources = kryo.readObject(input, LinkedHashSet.class); 44 Set<EncodedDiscreteResources> resources = kryo.readObject(input, LinkedHashSet.class);
43 45
44 - return EncodableDiscreteResources.of(parent, 46 + return new EncodableDiscreteResources(parent, resources.stream()
45 - resources.stream() 47 + .collect(Collectors.toMap(
46 - .flatMap(x -> x.values(parent.id()).stream()) 48 + EncodedDiscreteResources::encodedClass,
47 - .collect(Collectors.toCollection(LinkedHashSet::new))); 49 + Function.identity(),
50 + (v1, v2) -> v1,
51 + LinkedHashMap::new)));
48 } 52 }
49 } 53 }
......
...@@ -76,6 +76,11 @@ final class EncodedDiscreteResources { ...@@ -76,6 +76,11 @@ final class EncodedDiscreteResources {
76 .collect(Collectors.toCollection(LinkedHashSet::new)); 76 .collect(Collectors.toCollection(LinkedHashSet::new));
77 } 77 }
78 78
79 + Class<?> encodedClass() {
80 + Range<Integer> firstRange = rangeSet.asRanges().iterator().next();
81 + return codec.decode(firstRange.lowerEndpoint()).getClass();
82 + }
83 +
79 @SuppressWarnings("unchecked") 84 @SuppressWarnings("unchecked")
80 boolean contains(DiscreteResource resource) { 85 boolean contains(DiscreteResource resource) {
81 return resource.valueAs(Object.class) 86 return resource.valueAs(Object.class)
......