I was looking at some of the code in https://github.com/EsotericSoftware/kryonet/blob/master/src/com/esotericsoftware/kryonet/Server.java and noticed the functions (which are seemingly called quite frequently) iterate through the entire list of connections… in O(n) time instead of O(1) time by indexing with connectionID.
public void sendToTCP (int connectionID, Object object) {
Connection[] connections = this.connections;
for (int i = 0, n = connections.length; i < n; i++) {
Connection connection = connections[i];
if (connection.id == connectionID) {
connection.sendTCP(object);
break;
}
}
}
public void sendToUDP (int connectionID, Object object) {
Connection[] connections = this.connections;
for (int i = 0, n = connections.length; i < n; i++) {
Connection connection = connections[i];
if (connection.id == connectionID) {
connection.sendUDP(object);
break;
}
}
}
Why is that?