# Optimization.... can't make up my mind.

This topic is 4609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hum... ... de dum.... de dum... So I've got my C -> C# port of my 2D MMORPG underway (and I've made quite the progress). I'm currently working on the most CPU intensive parts of the server; the buffer flusher. For each player, the server stores a reference of all the objects the client can see, so that when an object goes out of the client's vision, the server knows to stop sending the movement packets to the client. The reference array (used by the server) has a size of 1024, and the server is designed to be able to hold 1000 players (laugh). Now as you can imagine, doing something like this...
//on each frame of the game loop
for(each player)
for(each reference)
if(reference!=null)
if(outofvision(reference))
remove(reference);
... sounds like a bottleneck (its defenently slower than the method I'm going to present next, but the thing is, this method is a sure bet that it won't miss which objects have to be removed). In my C code, I had designed a system where as any event that could remove an object from the players vision, would trigger it to be removed. The problem was, my design was buggy; in very very rare instances (too rare for me to properly debug it), in object would not get removed and this led to some very suspicious phenomenas (that and, some players told me I was a newbie programmer [depressed]). I'm trying to figure out what the performance penality is for going with the first method. Rarely will a player have more than 30 objects referenced, and although I don't want to shutdoors, I doubt I'll have more than a 100 players online for quite a while (on account of the fact, I'm "borrowing" graphics from a few dozen games [rolleyes]). So to get a little more technical, about 80% of those million equality statements will be checking if an object is null or not; this is fast right? I've considered using ArrayLists, but I hear they are slower than arrays (especially when its resizing the ArrayList), and can be a memory management hazard. If anyone has any experience with these, I'd like to hear it first hand. Lastly, if anyone has done something like this before, and found a better solution to this, I'd like to hear it.

##### Share on other sites
"sounds like a bottleneck" <-- sounds like premature optimization

##### Share on other sites
Well, look through and find the bug...

Or, you can do a hacky approach and simply run that code once every x frames as opposed to once every frame AND do your currently buggy removal. This way the buggy graphic glitch (or whatever glitch you are getting) will only be with your user for a little while before it's rectified and reset to proper.

I can't say much else because I don't really want to trudge through and debug your code for you.

*edit: I wouldn't call it premature optimization, that could be a huge problem if there are 1000 players and 1000 references in a level, for example. Who wants to cycle through 1000000 checks against positions EVERY frame? That's a million loop iterations which is not good to do without good reason.

You can do this in chunks too, just do a little bit at a time in your main loop, or thread the operation. That way you don't have a stall every time you do run the check.

##### Share on other sites
Hold up. Are you checking the entire reference array? like, checking ~30 visibilities, then making sure that the other 970 references are null, one at a time? I suggest you simply keep your active references coalesced. (when an object goes invisible, swap it and the last element, then drop the size of the array by 1.) That brings it down to 100*30=3000 visibility tests, easily doable. And if you want, you can perform the culling for only a subset of the players every frame. It won't really matter if an object is reported for an extra 2-3 frames, and will quarter the time you spend on these tests.

##### Share on other sites
Quote:
 Original post by RDragon1"sounds like a bottleneck" <-- sounds like premature optimization

I really don't believe it is, considering the following.

1) This is the most CPU intensive part of the server; if there is any bottle necks in the server, it'll be in the buffer handling.
2) I'm checking every reference, for every player, on every frame of the game loop; for the good majority of the time, this will mean approx. 1,000,000 null reference checks on each frame of the game loop, 997,000 of which shouldn't need to be checked.

Quote:
 Original post by M2tMOr, you can do a hacky approach and simply run that code once every x frames as opposed to once every frame AND do your currently buggy removal. This way the buggy graphic glitch (or whatever glitch you are getting) will only be with your user for a little while before it's rectified and reset to proper.

A phenomena was that it existed on the client, yet no longer on the server; thus, the server for some reason "forgot" about the client and it removed it from itself. Since the client has no such flusher built into it, it existed on the client until he exited the game.

Quote:
 Original post by Sneftel...when an object goes invisible, swap it and the last element, then drop the size of the array by 1.) That brings it down to 100*30=3000 visibility tests, easily doable. And if you want, you can perform the culling for only a subset of the players every frame. It won't really matter if an object is reported for an extra 2-3 frames, and will quarter the time you spend on these tests.

Now why didn't I think of that; lol?
(and yes to your first question)

Thanks again!

1. 1
2. 2
3. 3
4. 4
frob
13
5. 5

• 16
• 13
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632169
• Total Posts
3004545

×