[cooja] skins/TrafficVisualizerSkin: Reduce array copy and iteration overhead

This commit is contained in:
Enrico Joerns 2014-06-11 00:49:25 +02:00
parent 468d533c41
commit 7c80213c3a

View file

@ -33,6 +33,7 @@ import java.awt.Color;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Point; import java.awt.Point;
import java.awt.Polygon; import java.awt.Polygon;
import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Observable; import java.util.Observable;
@ -71,7 +72,6 @@ public class TrafficVisualizerSkin implements VisualizerSkin {
private AbstractRadioMedium radioMedium = null; private AbstractRadioMedium radioMedium = null;
private List<RadioConnectionArrow> historyList = new LinkedList<>(); private List<RadioConnectionArrow> historyList = new LinkedList<>();
private RadioConnectionArrow[] history = null;
private Observer radioMediumObserver = new Observer() { private Observer radioMediumObserver = new Observer() {
@Override @Override
@ -79,7 +79,6 @@ public class TrafficVisualizerSkin implements VisualizerSkin {
RadioConnection last = radioMedium.getLastConnection(); RadioConnection last = radioMedium.getLastConnection();
if (last != null && historyList.size() < MAX_HISTORY_SIZE) { if (last != null && historyList.size() < MAX_HISTORY_SIZE) {
historyList.add(new RadioConnectionArrow(last)); historyList.add(new RadioConnectionArrow(last));
history = historyList.toArray(new RadioConnectionArrow[0]);
visualizer.repaint(500); visualizer.repaint(500);
} }
} }
@ -92,27 +91,17 @@ public class TrafficVisualizerSkin implements VisualizerSkin {
} }
if (historyList.size() > 0) { if (historyList.size() > 0) {
boolean hasOld = false;
/* Increase age */ /* Increase age and remove too old arrows */
for (RadioConnectionArrow connArrow : historyList) { Iterator<RadioConnectionArrow> iter = historyList.iterator();
connArrow.increaseAge(); while (iter.hasNext()) {
if(connArrow.getAge() >= connArrow.getMaxAge()) { RadioConnectionArrow rca = iter.next();
hasOld = true; rca.increaseAge();
if(rca.getAge() >= rca.getMaxAge()) {
iter.remove();
} }
} }
/* Remove too old arrows */
if (hasOld) {
RadioConnectionArrow[] historyArr = historyList.toArray(new RadioConnectionArrow[0]);
for (RadioConnectionArrow connArrow : historyArr) {
if(connArrow.getAge() >= connArrow.getMaxAge()) {
historyList.remove(connArrow);
}
}
historyArr = historyList.toArray(new RadioConnectionArrow[0]);
}
visualizer.repaint(500); visualizer.repaint(500);
} }
@ -132,7 +121,6 @@ public class TrafficVisualizerSkin implements VisualizerSkin {
@Override @Override
public void run() { public void run() {
historyList.clear(); historyList.clear();
history = null;
/* Start observing radio medium for transmissions */ /* Start observing radio medium for transmissions */
radioMedium.addRadioMediumObserver(radioMediumObserver); radioMedium.addRadioMediumObserver(radioMediumObserver);