Sho SHIMIZU

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
......@@ -117,7 +117,7 @@ final class EncodableDiscreteResources implements DiscreteResources {
@Override
public Set<DiscreteResource> values() {
return values.values().stream()
.flatMap(x -> x.resources(parent.id()).stream())
.flatMap(x -> x.values(parent.id()).stream())
.collect(Collectors.toCollection(LinkedHashSet::new));
}
......
......@@ -43,7 +43,7 @@ class EncodableDiscreteResourcesSerializer extends Serializer<EncodableDiscreteR
return EncodableDiscreteResources.of(parent,
resources.stream()
.flatMap(x -> x.resources(parent.id()).stream())
.flatMap(x -> x.values(parent.id()).stream())
.collect(Collectors.toCollection(LinkedHashSet::new)));
}
}
......
......@@ -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 org.onosproject.net.resource.Resources;
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 @@ final class EncodedDiscreteResources {
@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();
}
}
......
......@@ -19,18 +19,15 @@ import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
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.ClosedOpenRange;
import org.onosproject.net.resource.DiscreteResourceCodec;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* Kryo Serializer for {@link EncodedDiscreteResources}.
......@@ -38,12 +35,7 @@ import java.util.stream.IntStream;
final class EncodedResourcesSerializer extends Serializer<EncodedDiscreteResources> {
@Override
public void write(Kryo kryo, Output output, EncodedDiscreteResources object) {
TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
object.rawValues().stream()
.map(Range::singleton)
.map(x -> x.canonical(DiscreteDomain.integers()))
.forEach(rangeSet::add);
List<ClosedOpenRange> ranges = rangeSet.asRanges().stream()
List<ClosedOpenRange> ranges = object.rangeSet().asRanges().stream()
.map(ClosedOpenRange::of)
.collect(Collectors.toList());
kryo.writeObject(output, ranges);
......@@ -56,10 +48,10 @@ final class EncodedResourcesSerializer extends Serializer<EncodedDiscreteResourc
List<ClosedOpenRange> ranges = kryo.readObject(input, ArrayList.class);
DiscreteResourceCodec codec = (DiscreteResourceCodec) kryo.readClassAndObject(input);
HashSet<Integer> rawValues = ranges.stream()
.flatMapToInt(x -> IntStream.range(x.lowerBound(), x.upperBound()))
.boxed()
.collect(Collectors.toCollection(LinkedHashSet::new));
return new EncodedDiscreteResources(rawValues, codec);
RangeSet<Integer> rangeSet = TreeRangeSet.create();
ranges.stream()
.map(x -> Range.closedOpen(x.lowerBound(), x.upperBound()))
.forEach(rangeSet::add);
return new EncodedDiscreteResources(rangeSet, codec);
}
}
......