Skip to content

Commit 9a367e0

Browse files
committed
When checing for a new packet, check the last index first
This change should lower the execution time of this functuion.
1 parent 8fe4cd0 commit 9a367e0

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

javasdk/NRSDK/src/com/neuronrobotics/sdk/common/BowlerDatagramFactory.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class BowlerDatagramFactory {
2828

2929
private static BowlerDatagram pool [];
3030
private static int failed=0;
31+
private static int lastIndex = 0;
3132
private static int poolDefaultSize = 1500;
3233
private static long packetTimeout = 2000;
3334

@@ -57,11 +58,20 @@ private static synchronized BowlerDatagram getNextPacket(){
5758
BowlerDatagram ref = null;
5859

5960
//Find the most recent free packet from the pool
60-
for(int i=0;(i<pool.length && ref==null);i++){
61+
for(int i=lastIndex;(i<pool.length && ref==null);i++){
6162
//Log.warning("Checking pool packet "+i);
6263
if(pool[i].isFree()){
64+
lastIndex=i;
6365
ref=pool[i];
6466
}
67+
if(i==pool.length-1){
68+
//loop around since we started at the last index
69+
i=0;
70+
}
71+
if(i==lastIndex-1){
72+
//looped around, bail
73+
i=pool.length;
74+
}
6575
}
6676
if(ref == null){
6777
//The whole list was search and no free packets were found

0 commit comments

Comments
 (0)