Sho SHIMIZU
Committed by Gerrit Code Review

Avoid unnecessary allocations needed to convert List to Set

Change-Id: I80b0bcf31f625f8e151155880075c888a0cf1c1d
......@@ -48,6 +48,7 @@ import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
......@@ -333,14 +334,14 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
// it's assumed that the passed "values" is non-empty
// This is 2-pass scan. Nicer to have 1-pass scan
List<DiscreteResource> discreteValues = values.stream()
Set<DiscreteResource> discreteValues = values.stream()
.filter(x -> x instanceof DiscreteResource)
.map(x -> (DiscreteResource) x)
.collect(Collectors.toList());
List<ContinuousResource> continuousValues = values.stream()
.collect(Collectors.toCollection(LinkedHashSet::new));
Set<ContinuousResource> continuousValues = values.stream()
.filter(x -> x instanceof ContinuousResource)
.map(x -> (ContinuousResource) x)
.collect(Collectors.toList());
.collect(Collectors.toCollection(LinkedHashSet::new));
return discreteTxStore.register(key, discreteValues)
&& continuousTxStore.register(key, continuousValues);
......@@ -362,14 +363,14 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
// it's assumed that the passed "values" is non-empty
// This is 2-pass scan. Nicer to have 1-pass scan
List<DiscreteResource> discreteValues = values.stream()
Set<DiscreteResource> discreteValues = values.stream()
.filter(x -> x instanceof DiscreteResource)
.map(x -> (DiscreteResource) x)
.collect(Collectors.toList());
List<ContinuousResource> continuousValues = values.stream()
.collect(Collectors.toCollection(LinkedHashSet::new));
Set<ContinuousResource> continuousValues = values.stream()
.filter(x -> x instanceof ContinuousResource)
.map(x -> (ContinuousResource) x)
.collect(Collectors.toList());
.collect(Collectors.toCollection(LinkedHashSet::new));
return discreteTxStore.unregister(key, discreteValues)
&& continuousTxStore.unregister(key, continuousValues);
......
......@@ -18,7 +18,6 @@ package org.onosproject.store.resource.impl;
import org.onosproject.net.resource.DiscreteResource;
import org.onosproject.net.resource.DiscreteResourceId;
import java.util.List;
import java.util.Optional;
import java.util.Set;
......@@ -75,7 +74,7 @@ interface DiscreteResources {
* @return true this instance contains a resource included in the given resources,
* otherwise false.
*/
boolean containsAny(List<DiscreteResource> other);
boolean containsAny(Set<DiscreteResource> other);
/**
* Returns a union set of this instance and the given instance.
......@@ -93,7 +92,7 @@ interface DiscreteResources {
* @param removed resources
* @return a new DiscreteResources instance representing a difference set
*/
DiscreteResources remove(List<DiscreteResource> removed);
DiscreteResources remove(Set<DiscreteResource> removed);
/**
* Returns all of resources this instance holds.
......
......@@ -19,7 +19,6 @@ import com.google.common.collect.ImmutableSet;
import org.onosproject.net.resource.DiscreteResource;
import org.onosproject.net.resource.DiscreteResourceId;
import java.util.List;
import java.util.Optional;
import java.util.Set;
......@@ -48,7 +47,7 @@ final class EmptyDiscreteResources implements DiscreteResources {
}
@Override
public boolean containsAny(List<DiscreteResource> other) {
public boolean containsAny(Set<DiscreteResource> other) {
return false;
}
......@@ -58,7 +57,7 @@ final class EmptyDiscreteResources implements DiscreteResources {
}
@Override
public DiscreteResources remove(List<DiscreteResource> removed) {
public DiscreteResources remove(Set<DiscreteResource> removed) {
return this;
}
......
......@@ -23,7 +23,6 @@ import org.onosproject.net.resource.Resources;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
......@@ -97,7 +96,7 @@ final class EncodableDiscreteResources implements DiscreteResources {
}
@Override
public boolean containsAny(List<DiscreteResource> other) {
public boolean containsAny(Set<DiscreteResource> other) {
return other.stream()
.anyMatch(x -> values().contains(x));
}
......@@ -110,8 +109,8 @@ final class EncodableDiscreteResources implements DiscreteResources {
}
@Override
public DiscreteResources remove(List<DiscreteResource> removed) {
return of(parent, Sets.difference(values(), new LinkedHashSet<>(removed)));
public DiscreteResources remove(Set<DiscreteResource> removed) {
return of(parent, Sets.difference(values(), removed));
}
@Override
......
......@@ -22,7 +22,6 @@ import org.onosproject.net.resource.DiscreteResourceId;
import org.onosproject.net.resource.Resources;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
......@@ -68,7 +67,7 @@ final class GenericDiscreteResources implements DiscreteResources {
}
@Override
public boolean containsAny(List<DiscreteResource> other) {
public boolean containsAny(Set<DiscreteResource> other) {
return other.stream().anyMatch(values::contains);
}
......@@ -82,10 +81,8 @@ final class GenericDiscreteResources implements DiscreteResources {
// returns a new instance, not mutate the current instance
@Override
public DiscreteResources remove(List<DiscreteResource> removed) {
Set<DiscreteResource> newValues = new LinkedHashSet<>(this.values);
newValues.removeAll(removed);
return new GenericDiscreteResources(newValues);
public DiscreteResources remove(Set<DiscreteResource> removed) {
return of(Sets.difference(this.values, removed));
}
@Override
......
......@@ -28,7 +28,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
......@@ -61,13 +60,12 @@ class TransactionalContinuousResourceSubStore {
.findFirst();
}
boolean register(DiscreteResourceId key, List<ContinuousResource> values) {
boolean register(DiscreteResourceId key, Set<ContinuousResource> requested) {
// short-circuit: receiving empty resource is regarded as success
if (values.isEmpty()) {
if (requested.isEmpty()) {
return true;
}
Set<ContinuousResource> requested = new LinkedHashSet<>(values);
Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, requested);
if (oldValues == null) {
return true;
......@@ -94,7 +92,7 @@ class TransactionalContinuousResourceSubStore {
return childMap.replace(key, oldValues, newValues);
}
boolean unregister(DiscreteResourceId key, List<ContinuousResource> values) {
boolean unregister(DiscreteResourceId key, Set<ContinuousResource> values) {
// short-circuit: receiving empty resource is regarded as success
if (values.isEmpty()) {
return true;
......
......@@ -24,9 +24,8 @@ import org.onosproject.store.service.TransactionalMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
......@@ -54,13 +53,13 @@ class TransactionalDiscreteResourceSubStore {
return values.lookup(id);
}
boolean register(DiscreteResourceId key, List<DiscreteResource> values) {
boolean register(DiscreteResourceId key, Set<DiscreteResource> values) {
// short-circuit: receiving empty resource is regarded as success
if (values.isEmpty()) {
return true;
}
DiscreteResources requested = DiscreteResources.of(new LinkedHashSet<>(values));
DiscreteResources requested = DiscreteResources.of(values);
DiscreteResources oldValues = childMap.putIfAbsent(key, requested);
if (oldValues == null) {
return true;
......@@ -77,7 +76,7 @@ class TransactionalDiscreteResourceSubStore {
return childMap.replace(key, oldValues, newValues);
}
boolean unregister(DiscreteResourceId key, List<DiscreteResource> values) {
boolean unregister(DiscreteResourceId key, Set<DiscreteResource> values) {
// short-circuit: receiving empty resource is regarded as success
if (values.isEmpty()) {
return true;
......
......@@ -21,7 +21,6 @@ import org.onosproject.net.resource.DiscreteResourceId;
import org.onosproject.net.resource.Resources;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
......@@ -75,9 +74,9 @@ final class UnifiedDiscreteResources implements DiscreteResources {
}
@Override
public boolean containsAny(List<DiscreteResource> other) {
Map<Boolean, List<DiscreteResource>> partitioned = other.stream()
.collect(Collectors.partitioningBy(CODECS::isEncodable));
public boolean containsAny(Set<DiscreteResource> other) {
Map<Boolean, Set<DiscreteResource>> partitioned = other.stream()
.collect(Collectors.partitioningBy(CODECS::isEncodable, Collectors.toCollection(LinkedHashSet::new)));
return generics.containsAny(partitioned.get(false)) || encodables.containsAny(partitioned.get(true));
}
......@@ -87,8 +86,8 @@ final class UnifiedDiscreteResources implements DiscreteResources {
}
@Override
public DiscreteResources remove(List<DiscreteResource> removed) {
return of(Sets.difference(values(), new LinkedHashSet<>(removed)));
public DiscreteResources remove(Set<DiscreteResource> removed) {
return of(Sets.difference(values(), removed));
}
@Override
......