So I have a game where I put the player object into an array after them logging in, I don't have much trouble with it in regards of speeds since I also store the array index of the player but at some systems I do run into trouble, speed-wise.
E.G. One player wants to 'message' the other player, the client sends a packet with the name of the person like an unique long value which then loops over the whole player list array and compares the long value of the name and sends it to the found player afterwards.
My thought to speed this up was create a HashMap which uses Long value as the key to guarantee o(1) look-ups, although this was just a concept and I haven't tried it yet.
(I store the nameHash/long value of name upon login so calculating it wouldn't be a bottleneck)
Would this guarantee me o(1) lookups and is the long value of the string really unique?
The String to long(int64) method ->
public static long toLong(String s) {
long l = 0L;
for(int i = 0; i < s.length() && i < 12; i++) {
char c = s.charAt(i);
l *= 37L;
if(c >= 'A' && c <= 'Z') l += (1 + c) - 65;
else if(c >= 'a' && c <= 'z') l += (1 + c) - 97;
else if(c >= '0' && c <= '9') l += (27 + c) - 48;
}
while(l % 37L == 0L && l != 0L) l /= 37L;
return l;
}
The game ticks on 600ms and runs with 300/400 players so operations like this really are a bottleneck