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; ...@@ -48,6 +48,7 @@ import org.slf4j.LoggerFactory;
48 48
49 import java.util.Collection; 49 import java.util.Collection;
50 import java.util.LinkedHashMap; 50 import java.util.LinkedHashMap;
51 +import java.util.LinkedHashSet;
51 import java.util.List; 52 import java.util.List;
52 import java.util.Map; 53 import java.util.Map;
53 import java.util.Optional; 54 import java.util.Optional;
...@@ -333,14 +334,14 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour ...@@ -333,14 +334,14 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
333 // it's assumed that the passed "values" is non-empty 334 // it's assumed that the passed "values" is non-empty
334 335
335 // This is 2-pass scan. Nicer to have 1-pass scan 336 // This is 2-pass scan. Nicer to have 1-pass scan
336 - List<DiscreteResource> discreteValues = values.stream() 337 + Set<DiscreteResource> discreteValues = values.stream()
337 .filter(x -> x instanceof DiscreteResource) 338 .filter(x -> x instanceof DiscreteResource)
338 .map(x -> (DiscreteResource) x) 339 .map(x -> (DiscreteResource) x)
339 - .collect(Collectors.toList()); 340 + .collect(Collectors.toCollection(LinkedHashSet::new));
340 - List<ContinuousResource> continuousValues = values.stream() 341 + Set<ContinuousResource> continuousValues = values.stream()
341 .filter(x -> x instanceof ContinuousResource) 342 .filter(x -> x instanceof ContinuousResource)
342 .map(x -> (ContinuousResource) x) 343 .map(x -> (ContinuousResource) x)
343 - .collect(Collectors.toList()); 344 + .collect(Collectors.toCollection(LinkedHashSet::new));
344 345
345 return discreteTxStore.register(key, discreteValues) 346 return discreteTxStore.register(key, discreteValues)
346 && continuousTxStore.register(key, continuousValues); 347 && continuousTxStore.register(key, continuousValues);
...@@ -362,14 +363,14 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour ...@@ -362,14 +363,14 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
362 // it's assumed that the passed "values" is non-empty 363 // it's assumed that the passed "values" is non-empty
363 364
364 // This is 2-pass scan. Nicer to have 1-pass scan 365 // This is 2-pass scan. Nicer to have 1-pass scan
365 - List<DiscreteResource> discreteValues = values.stream() 366 + Set<DiscreteResource> discreteValues = values.stream()
366 .filter(x -> x instanceof DiscreteResource) 367 .filter(x -> x instanceof DiscreteResource)
367 .map(x -> (DiscreteResource) x) 368 .map(x -> (DiscreteResource) x)
368 - .collect(Collectors.toList()); 369 + .collect(Collectors.toCollection(LinkedHashSet::new));
369 - List<ContinuousResource> continuousValues = values.stream() 370 + Set<ContinuousResource> continuousValues = values.stream()
370 .filter(x -> x instanceof ContinuousResource) 371 .filter(x -> x instanceof ContinuousResource)
371 .map(x -> (ContinuousResource) x) 372 .map(x -> (ContinuousResource) x)
372 - .collect(Collectors.toList()); 373 + .collect(Collectors.toCollection(LinkedHashSet::new));
373 374
374 return discreteTxStore.unregister(key, discreteValues) 375 return discreteTxStore.unregister(key, discreteValues)
375 && continuousTxStore.unregister(key, continuousValues); 376 && continuousTxStore.unregister(key, continuousValues);
......
...@@ -18,7 +18,6 @@ package org.onosproject.store.resource.impl; ...@@ -18,7 +18,6 @@ package org.onosproject.store.resource.impl;
18 import org.onosproject.net.resource.DiscreteResource; 18 import org.onosproject.net.resource.DiscreteResource;
19 import org.onosproject.net.resource.DiscreteResourceId; 19 import org.onosproject.net.resource.DiscreteResourceId;
20 20
21 -import java.util.List;
22 import java.util.Optional; 21 import java.util.Optional;
23 import java.util.Set; 22 import java.util.Set;
24 23
...@@ -75,7 +74,7 @@ interface DiscreteResources { ...@@ -75,7 +74,7 @@ interface DiscreteResources {
75 * @return true this instance contains a resource included in the given resources, 74 * @return true this instance contains a resource included in the given resources,
76 * otherwise false. 75 * otherwise false.
77 */ 76 */
78 - boolean containsAny(List<DiscreteResource> other); 77 + boolean containsAny(Set<DiscreteResource> other);
79 78
80 /** 79 /**
81 * Returns a union set of this instance and the given instance. 80 * Returns a union set of this instance and the given instance.
...@@ -93,7 +92,7 @@ interface DiscreteResources { ...@@ -93,7 +92,7 @@ interface DiscreteResources {
93 * @param removed resources 92 * @param removed resources
94 * @return a new DiscreteResources instance representing a difference set 93 * @return a new DiscreteResources instance representing a difference set
95 */ 94 */
96 - DiscreteResources remove(List<DiscreteResource> removed); 95 + DiscreteResources remove(Set<DiscreteResource> removed);
97 96
98 /** 97 /**
99 * Returns all of resources this instance holds. 98 * Returns all of resources this instance holds.
......
...@@ -19,7 +19,6 @@ import com.google.common.collect.ImmutableSet; ...@@ -19,7 +19,6 @@ import com.google.common.collect.ImmutableSet;
19 import org.onosproject.net.resource.DiscreteResource; 19 import org.onosproject.net.resource.DiscreteResource;
20 import org.onosproject.net.resource.DiscreteResourceId; 20 import org.onosproject.net.resource.DiscreteResourceId;
21 21
22 -import java.util.List;
23 import java.util.Optional; 22 import java.util.Optional;
24 import java.util.Set; 23 import java.util.Set;
25 24
...@@ -48,7 +47,7 @@ final class EmptyDiscreteResources implements DiscreteResources { ...@@ -48,7 +47,7 @@ final class EmptyDiscreteResources implements DiscreteResources {
48 } 47 }
49 48
50 @Override 49 @Override
51 - public boolean containsAny(List<DiscreteResource> other) { 50 + public boolean containsAny(Set<DiscreteResource> other) {
52 return false; 51 return false;
53 } 52 }
54 53
...@@ -58,7 +57,7 @@ final class EmptyDiscreteResources implements DiscreteResources { ...@@ -58,7 +57,7 @@ final class EmptyDiscreteResources implements DiscreteResources {
58 } 57 }
59 58
60 @Override 59 @Override
61 - public DiscreteResources remove(List<DiscreteResource> removed) { 60 + public DiscreteResources remove(Set<DiscreteResource> removed) {
62 return this; 61 return this;
63 } 62 }
64 63
......
...@@ -23,7 +23,6 @@ import org.onosproject.net.resource.Resources; ...@@ -23,7 +23,6 @@ import org.onosproject.net.resource.Resources;
23 23
24 import java.util.LinkedHashMap; 24 import java.util.LinkedHashMap;
25 import java.util.LinkedHashSet; 25 import java.util.LinkedHashSet;
26 -import java.util.List;
27 import java.util.Map; 26 import java.util.Map;
28 import java.util.Optional; 27 import java.util.Optional;
29 import java.util.Set; 28 import java.util.Set;
...@@ -97,7 +96,7 @@ final class EncodableDiscreteResources implements DiscreteResources { ...@@ -97,7 +96,7 @@ final class EncodableDiscreteResources implements DiscreteResources {
97 } 96 }
98 97
99 @Override 98 @Override
100 - public boolean containsAny(List<DiscreteResource> other) { 99 + public boolean containsAny(Set<DiscreteResource> other) {
101 return other.stream() 100 return other.stream()
102 .anyMatch(x -> values().contains(x)); 101 .anyMatch(x -> values().contains(x));
103 } 102 }
...@@ -110,8 +109,8 @@ final class EncodableDiscreteResources implements DiscreteResources { ...@@ -110,8 +109,8 @@ final class EncodableDiscreteResources implements DiscreteResources {
110 } 109 }
111 110
112 @Override 111 @Override
113 - public DiscreteResources remove(List<DiscreteResource> removed) { 112 + public DiscreteResources remove(Set<DiscreteResource> removed) {
114 - return of(parent, Sets.difference(values(), new LinkedHashSet<>(removed))); 113 + return of(parent, Sets.difference(values(), removed));
115 } 114 }
116 115
117 @Override 116 @Override
......
...@@ -22,7 +22,6 @@ import org.onosproject.net.resource.DiscreteResourceId; ...@@ -22,7 +22,6 @@ import org.onosproject.net.resource.DiscreteResourceId;
22 import org.onosproject.net.resource.Resources; 22 import org.onosproject.net.resource.Resources;
23 23
24 import java.util.LinkedHashSet; 24 import java.util.LinkedHashSet;
25 -import java.util.List;
26 import java.util.Objects; 25 import java.util.Objects;
27 import java.util.Optional; 26 import java.util.Optional;
28 import java.util.Set; 27 import java.util.Set;
...@@ -68,7 +67,7 @@ final class GenericDiscreteResources implements DiscreteResources { ...@@ -68,7 +67,7 @@ final class GenericDiscreteResources implements DiscreteResources {
68 } 67 }
69 68
70 @Override 69 @Override
71 - public boolean containsAny(List<DiscreteResource> other) { 70 + public boolean containsAny(Set<DiscreteResource> other) {
72 return other.stream().anyMatch(values::contains); 71 return other.stream().anyMatch(values::contains);
73 } 72 }
74 73
...@@ -82,10 +81,8 @@ final class GenericDiscreteResources implements DiscreteResources { ...@@ -82,10 +81,8 @@ final class GenericDiscreteResources implements DiscreteResources {
82 81
83 // returns a new instance, not mutate the current instance 82 // returns a new instance, not mutate the current instance
84 @Override 83 @Override
85 - public DiscreteResources remove(List<DiscreteResource> removed) { 84 + public DiscreteResources remove(Set<DiscreteResource> removed) {
86 - Set<DiscreteResource> newValues = new LinkedHashSet<>(this.values); 85 + return of(Sets.difference(this.values, removed));
87 - newValues.removeAll(removed);
88 - return new GenericDiscreteResources(newValues);
89 } 86 }
90 87
91 @Override 88 @Override
......
...@@ -28,7 +28,6 @@ import org.slf4j.Logger; ...@@ -28,7 +28,6 @@ import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory; 28 import org.slf4j.LoggerFactory;
29 29
30 import java.util.LinkedHashSet; 30 import java.util.LinkedHashSet;
31 -import java.util.List;
32 import java.util.Optional; 31 import java.util.Optional;
33 import java.util.Set; 32 import java.util.Set;
34 import java.util.stream.Collectors; 33 import java.util.stream.Collectors;
...@@ -61,13 +60,12 @@ class TransactionalContinuousResourceSubStore { ...@@ -61,13 +60,12 @@ class TransactionalContinuousResourceSubStore {
61 .findFirst(); 60 .findFirst();
62 } 61 }
63 62
64 - boolean register(DiscreteResourceId key, List<ContinuousResource> values) { 63 + boolean register(DiscreteResourceId key, Set<ContinuousResource> requested) {
65 // short-circuit: receiving empty resource is regarded as success 64 // short-circuit: receiving empty resource is regarded as success
66 - if (values.isEmpty()) { 65 + if (requested.isEmpty()) {
67 return true; 66 return true;
68 } 67 }
69 68
70 - Set<ContinuousResource> requested = new LinkedHashSet<>(values);
71 Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, requested); 69 Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, requested);
72 if (oldValues == null) { 70 if (oldValues == null) {
73 return true; 71 return true;
...@@ -94,7 +92,7 @@ class TransactionalContinuousResourceSubStore { ...@@ -94,7 +92,7 @@ class TransactionalContinuousResourceSubStore {
94 return childMap.replace(key, oldValues, newValues); 92 return childMap.replace(key, oldValues, newValues);
95 } 93 }
96 94
97 - boolean unregister(DiscreteResourceId key, List<ContinuousResource> values) { 95 + boolean unregister(DiscreteResourceId key, Set<ContinuousResource> values) {
98 // short-circuit: receiving empty resource is regarded as success 96 // short-circuit: receiving empty resource is regarded as success
99 if (values.isEmpty()) { 97 if (values.isEmpty()) {
100 return true; 98 return true;
......
...@@ -24,9 +24,8 @@ import org.onosproject.store.service.TransactionalMap; ...@@ -24,9 +24,8 @@ import org.onosproject.store.service.TransactionalMap;
24 import org.slf4j.Logger; 24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory; 25 import org.slf4j.LoggerFactory;
26 26
27 -import java.util.LinkedHashSet;
28 -import java.util.List;
29 import java.util.Optional; 27 import java.util.Optional;
28 +import java.util.Set;
30 29
31 import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER; 30 import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
32 31
...@@ -54,13 +53,13 @@ class TransactionalDiscreteResourceSubStore { ...@@ -54,13 +53,13 @@ class TransactionalDiscreteResourceSubStore {
54 return values.lookup(id); 53 return values.lookup(id);
55 } 54 }
56 55
57 - boolean register(DiscreteResourceId key, List<DiscreteResource> values) { 56 + boolean register(DiscreteResourceId key, Set<DiscreteResource> values) {
58 // short-circuit: receiving empty resource is regarded as success 57 // short-circuit: receiving empty resource is regarded as success
59 if (values.isEmpty()) { 58 if (values.isEmpty()) {
60 return true; 59 return true;
61 } 60 }
62 61
63 - DiscreteResources requested = DiscreteResources.of(new LinkedHashSet<>(values)); 62 + DiscreteResources requested = DiscreteResources.of(values);
64 DiscreteResources oldValues = childMap.putIfAbsent(key, requested); 63 DiscreteResources oldValues = childMap.putIfAbsent(key, requested);
65 if (oldValues == null) { 64 if (oldValues == null) {
66 return true; 65 return true;
...@@ -77,7 +76,7 @@ class TransactionalDiscreteResourceSubStore { ...@@ -77,7 +76,7 @@ class TransactionalDiscreteResourceSubStore {
77 return childMap.replace(key, oldValues, newValues); 76 return childMap.replace(key, oldValues, newValues);
78 } 77 }
79 78
80 - boolean unregister(DiscreteResourceId key, List<DiscreteResource> values) { 79 + boolean unregister(DiscreteResourceId key, Set<DiscreteResource> values) {
81 // short-circuit: receiving empty resource is regarded as success 80 // short-circuit: receiving empty resource is regarded as success
82 if (values.isEmpty()) { 81 if (values.isEmpty()) {
83 return true; 82 return true;
......
...@@ -21,7 +21,6 @@ import org.onosproject.net.resource.DiscreteResourceId; ...@@ -21,7 +21,6 @@ import org.onosproject.net.resource.DiscreteResourceId;
21 import org.onosproject.net.resource.Resources; 21 import org.onosproject.net.resource.Resources;
22 22
23 import java.util.LinkedHashSet; 23 import java.util.LinkedHashSet;
24 -import java.util.List;
25 import java.util.Map; 24 import java.util.Map;
26 import java.util.Optional; 25 import java.util.Optional;
27 import java.util.Set; 26 import java.util.Set;
...@@ -75,9 +74,9 @@ final class UnifiedDiscreteResources implements DiscreteResources { ...@@ -75,9 +74,9 @@ final class UnifiedDiscreteResources implements DiscreteResources {
75 } 74 }
76 75
77 @Override 76 @Override
78 - public boolean containsAny(List<DiscreteResource> other) { 77 + public boolean containsAny(Set<DiscreteResource> other) {
79 - Map<Boolean, List<DiscreteResource>> partitioned = other.stream() 78 + Map<Boolean, Set<DiscreteResource>> partitioned = other.stream()
80 - .collect(Collectors.partitioningBy(CODECS::isEncodable)); 79 + .collect(Collectors.partitioningBy(CODECS::isEncodable, Collectors.toCollection(LinkedHashSet::new)));
81 return generics.containsAny(partitioned.get(false)) || encodables.containsAny(partitioned.get(true)); 80 return generics.containsAny(partitioned.get(false)) || encodables.containsAny(partitioned.get(true));
82 } 81 }
83 82
...@@ -87,8 +86,8 @@ final class UnifiedDiscreteResources implements DiscreteResources { ...@@ -87,8 +86,8 @@ final class UnifiedDiscreteResources implements DiscreteResources {
87 } 86 }
88 87
89 @Override 88 @Override
90 - public DiscreteResources remove(List<DiscreteResource> removed) { 89 + public DiscreteResources remove(Set<DiscreteResource> removed) {
91 - return of(Sets.difference(values(), new LinkedHashSet<>(removed))); 90 + return of(Sets.difference(values(), removed));
92 } 91 }
93 92
94 @Override 93 @Override
......