• Create Account

## Immutable World State for Parallelization

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1sufficientreason  Members

201
Like
1Likes
Like

Posted 07 February 2012 - 03:00 PM

I'm designing a real-time top-down multiplayer 2D space shooter a la Subspace/Continuum (following the FPS model), and have a game state question.

I'm trying to make a scalable world state model for handling entity updates (ships, projectiles, explosions). Since the majority of the time spent in a state update will be on entity updates, I want to make them highly parallelizable for use in a C# Parallel.Foreach loop. In order to accomplish this, I've gone with an approach where each world state is completely immutable, and entities update by creating clones of themselves with changes applied, which are then placed into the next stored world state. This also makes all collisions order independent, since each entity must look at the current world state and independently decide how it should react to the collision.

External calls to modify the world are queued as actions to perform on the next Update(). Once Update() is called, a new mutable world state is created. All queued actions are retrieved and performed on that new world state, and each entity from the old state creates an updated clone to place in the new state. The update process can generate two kinds of events. Internal events are immediately queued for the next update, while external events are stored and frozen in the world state. The new world state, with the updated entities (also added/removed entities) and any external events that were generated that timestep, gets frozen and goes out to whoever wants to visualize that state (server broadcasts to clients, clients display).

The area highlighted in orange is where I want to parallelize. Here's the update process for each entity in detail:

Entities read the previous world state, themselves, and take any incoming events relevant to them (an explosion nearby, a change in their control state) and produce an immutable clone of themselves. Because nobody is writing any data here (and regular C# iterators are thread-safe for read access), each one of these entity updates could theoretically run on its own thread with no problem. So I want to batch their updates.

I guess my question is, does this seem reasonable? Cloning is expensive, but is it as expensive as the calculations needed for each update (collision detection, timer countdowns, control resolution, applying forces, etc.), so I think the tradeoff is worth it. Do any other games work like this? I couldn't find any FPS examples where immutable states are generated by cloning, but most of what we have to work with (Quake 3, Source SDK) didn't design servers with multicore in mind.

### #2swiftcoder  Senior Moderators

17805
Like
0Likes
Like

Posted 07 February 2012 - 04:12 PM

Functional programming languages tend to encourage (or even enforce) this sort of thing. Immutable state makes sharing data between threads a lot simpler (as you are aware).

I can't really speak as to how well a C# implementation will perform - it may be that you will be hindered by the language's own bias towards mutable state.

Tristam MacDonald - Software Engineer @ Amazon - [swiftcoding] [GitHub]

### #3ApochPiQ  Moderators

21387
Like
1Likes
Like

Posted 07 February 2012 - 04:17 PM

The main thing I'd be worried about is garbage collector pressure created by duplicating your state representations all the time. If possible, I'd recommend looking at double or triple buffering as a means to avoid that.
Wielder of the Sacred Wands

### #4sufficientreason  Members

201
Like
0Likes
Like

Posted 07 February 2012 - 04:21 PM

The main thing I'd be worried about is garbage collector pressure created by duplicating your state representations all the time. If possible, I'd recommend looking at double or triple buffering as a means to avoid that.

Could you elaborate a little bit or point me to a source for double buffering for model state? Everything I turn up is for buffering draws to the screen. Do you mean buffering entity state? I think that if I hold on to previous generated immutable world states I think I'm doing that anyway.

Updating a single entity is one step. You figure out all of the values for the data stored in that entity, and then instantiate that entity with those values in the constructor (using C# readonly variables for immutability).

### #5turch  Members

590
Like
1Likes
Like

Posted 07 February 2012 - 04:48 PM

The main thing I'd be worried about is garbage collector pressure created by duplicating your state representations all the time. If possible, I'd recommend looking at double or triple buffering as a means to avoid that.

Could you elaborate a little bit or point me to a source for double buffering for model state? Everything I turn up is for buffering draws to the screen.

Its pretty much analogous to screen buffering. You have two copies of whatever data structures store everything, and instead of creating a new version each update, you just change the data held in the "current" data. At the end of the update, just flip which one is current.

I've used that before on certain subsystems to prevent having to reallocate everything once per update.

### #6sufficientreason  Members

201
Like
0Likes
Like

Posted 07 February 2012 - 06:32 PM

Oh, that's a really good way to do it, thank you! I'll start with the naive approach of constant allocation and dscarding, and try that as an optimization after some profiling if it's necessary.

Aside from that, does this approach look alright? Event queues that only take effect after Update(), and so on? I'll report back with results after I implement it.

### #7slayemin  Members

5630
Like
1Likes
Like

Posted 11 February 2012 - 03:38 AM

I recently did a small project with multithreading. I had a problem with race conditions, so I implemented a binary semaphore to indicate if a shared resource was being used by another thread. It might be something you could find useful. Here is the general idea followed by pseudocode:

I have a static list of objects which need to be accessed by multiple threads. I also need to maintain the list of objects. Part of the list maintenance means that a thread may add or remove objects from the list. If two threads are modifying the size of the list, then a runtime error occurs. So, if a thread needs to access the list, I need to guarantee that the size of the list won't be changed by another thread. I create a static boolean variable to indicate whether or not the list is in use by another thread. When a thread wants to access the list, it checks to see if the list is unlocked before going forward.

static List<gameobject> m_objects = new list<gameobject>();
static bool m_listLocked = false;

while(m_listLocked);
m_listLocked = true;
foreach(item in the list)
do stuff;
m_listLocked = false;

while(m_listLocked);
m_listLocked = true;
foreach(item in the list)
do stuff;
m_listLocked = false;


I use an empty spin lock, but I could add a short sleep cycle inside of the while loop to save the CPU a bit.

Note that I don't do any thread synchronization with game frames.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.