Reduce the memory usage by RangeSet in backing data structure
- Typically resource query is more frequent than resource registration.
This patch optimizes memory usage for resource query rather than registration
Change-Id: Ic680c09c83b4299e80d4493d01e642081549fa24
(cherry picked from commit fd18ded046b9135a7fdc7e58c3fbafea9c835fe8)
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedDiscreteResources.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedDiscreteResources.java
index c79b23a..a4818e2 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedDiscreteResources.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedDiscreteResources.java
@@ -15,6 +15,10 @@
*/
package org.onosproject.store.resource.impl;
+import com.google.common.collect.DiscreteDomain;
+import com.google.common.collect.Range;
+import com.google.common.collect.RangeSet;
+import com.google.common.collect.TreeRangeSet;
import org.onlab.util.Tools;
import org.onosproject.net.resource.DiscreteResource;
import org.onosproject.net.resource.DiscreteResourceCodec;
@@ -24,39 +28,45 @@
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;
+import java.util.stream.IntStream;
/**
* Represents discrete resources encoded by a codec.
*/
final class EncodedDiscreteResources {
- private final Set<Integer> rawValues;
+ private final RangeSet<Integer> rangeSet;
private final DiscreteResourceCodec codec;
- EncodedDiscreteResources(Set<Integer> rawValues, DiscreteResourceCodec codec) {
- this.rawValues = rawValues;
+ EncodedDiscreteResources(RangeSet<Integer> rangeSet, DiscreteResourceCodec codec) {
+ this.rangeSet = rangeSet;
this.codec = codec;
}
static EncodedDiscreteResources of(Set<DiscreteResource> resources, DiscreteResourceCodec codec) {
- Set<Integer> rawValues = resources.stream()
+ RangeSet<Integer> rangeSet = TreeRangeSet.create();
+ resources.stream()
.map(x -> x.valueAs(Object.class))
.flatMap(Tools::stream)
.map(x -> codec.encode(x))
- .collect(Collectors.toCollection(LinkedHashSet::new));
+ .map(Range::singleton)
+ .map(x -> x.canonical(DiscreteDomain.integers()))
+ .forEach(rangeSet::add);
- return new EncodedDiscreteResources(rawValues, codec);
+ return new EncodedDiscreteResources(rangeSet, codec);
}
- Set<Integer> rawValues() {
- return rawValues;
+ RangeSet<Integer> rangeSet() {
+ return rangeSet;
}
DiscreteResourceCodec codec() {
return codec;
}
- Set<DiscreteResource> resources(DiscreteResourceId parent) {
- return rawValues.stream()
+ Set<DiscreteResource> values(DiscreteResourceId parent) {
+ return rangeSet.asRanges().stream()
+ .flatMapToInt(x1 -> IntStream.range(x1.lowerEndpoint(), x1.upperEndpoint()))
+ .boxed()
.map(x -> codec.decode(x))
.map(x -> Resources.discrete(parent, x).resource())
.collect(Collectors.toCollection(LinkedHashSet::new));
@@ -64,10 +74,10 @@
@SuppressWarnings("unchecked")
boolean contains(DiscreteResource resource) {
- return rawValues.contains(codec.encode(resource));
+ return rangeSet.contains(codec.encode(resource));
}
boolean isEmpty() {
- return rawValues.isEmpty();
+ return rangeSet.isEmpty();
}
}