Using a List.indexOf() followed by equality to 0 is ... inefficient
to say the least.
Kill isFirstBestPath() and use a simple check against the first path,
if the list is non-empty.
Also optimize list allocation and remove useless copying while we're
at it.
Change-Id: Ia1a48736ff363ab15776b9f188207720f23169e2
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
*/
package org.opendaylight.protocol.bgp.mode.impl.add;
*/
package org.opendaylight.protocol.bgp.mode.impl.add;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.primitives.UnsignedInteger;
import java.util.ArrayList;
import com.google.common.collect.Lists;
import com.google.common.primitives.UnsignedInteger;
import java.util.ArrayList;
if (this.newBestPathToBeAdvertised == null || this.newBestPathToBeAdvertised.isEmpty()) {
return Collections.emptyList();
}
if (this.newBestPathToBeAdvertised == null || this.newBestPathToBeAdvertised.isEmpty()) {
return Collections.emptyList();
}
- final List<AdvertizedRoute<C, S, R, I>> advertized = new ArrayList<>();
+ final List<AdvertizedRoute<C, S, R, I>> advertized = new ArrayList<>(newBestPathToBeAdvertised.size());
+ final AddPathBestPath firstBestPath = this.bestPath.isEmpty() ? null : this.bestPath.get(0);
for (final AddPathBestPath path : this.newBestPathToBeAdvertised) {
for (final AddPathBestPath path : this.newBestPathToBeAdvertised) {
- final boolean isFirstBestPath = isFirstBestPath(this.bestPath.indexOf(path));
final R routeAddPath = createRoute(ribSupport, routeKey, path.getPathId(), path);
final R routeAddPath = createRoute(ribSupport, routeKey, path.getPathId(), path);
+ // FIXME: can we use identity check here?
+ final boolean isFirstBestPath = firstBestPath != null && firstBestPath.equals(path);
final AdvertizedRoute<C, S, R, I> adv = new AdvertizedRoute<>(ribSupport, isFirstBestPath,
routeAddPath, path.getAttributes(), path.getPeerId());
advertized.add(adv);
final AdvertizedRoute<C, S, R, I> adv = new AdvertizedRoute<>(ribSupport, isFirstBestPath,
routeAddPath, path.getAttributes(), path.getPeerId());
advertized.add(adv);
return selector.result();
}
return selector.result();
}
- private static boolean isFirstBestPath(final int bestPathPosition) {
- return bestPathPosition == 0;
- }
-
- protected boolean isBestPathNew(final List<AddPathBestPath> newBestPathList) {
+ protected boolean isBestPathNew(final ImmutableList<AddPathBestPath> newBestPathList) {
this.isNonAddPathBestPathNew = !isNonAddPathBestPathTheSame(newBestPathList);
filterRemovedPaths(newBestPathList);
if (this.bestPathRemoved != null && !this.bestPathRemoved.isEmpty()
|| newBestPathList != null
&& !newBestPathList.equals(this.bestPath)) {
this.isNonAddPathBestPathNew = !isNonAddPathBestPathTheSame(newBestPathList);
filterRemovedPaths(newBestPathList);
if (this.bestPathRemoved != null && !this.bestPathRemoved.isEmpty()
|| newBestPathList != null
&& !newBestPathList.equals(this.bestPath)) {
- this.newBestPathToBeAdvertised = new ArrayList<>(newBestPathList);
if (this.bestPath != null) {
if (this.bestPath != null) {
+ this.newBestPathToBeAdvertised = new ArrayList<>(newBestPathList);
this.newBestPathToBeAdvertised.removeAll(this.bestPath);
this.newBestPathToBeAdvertised.removeAll(this.bestPath);
+ } else {
+ this.newBestPathToBeAdvertised = newBestPathList;
}
this.bestPath = newBestPathList;
LOG.trace("Actual Best {}, removed best {}", this.bestPath, this.bestPathRemoved);
}
this.bestPath = newBestPathList;
LOG.trace("Actual Best {}, removed best {}", this.bestPath, this.bestPathRemoved);