Multiple Worlds (Using JBox2d in multiple threads)

Huh, I've been using HashMaps (actually HashTable, the Evil Synchronized Sibling of HashMap) for this sort of thing for years, even naming them things like "threadLocalPool." Despite all the hassles of synchronization and releasing objects at thread death, it never occurred to me to check whether there was a built in class that does it for you.

Apparently the first implementations were real bad performance-wise, so it never took off, but it looks like the recent ones are just fine. Good to know...

Looks like the thing to do would be to subclass ThreadLocal for each type that we need to grab local versions of, i.e.:
Code:
public class ThreadLocalVec2 extends ThreadLocal<Vec2> {
  protected Vec2 initialValue() {
    return new Vec2();
  }
}
public class ThreadLocalMat22 extends ThreadLocal<Mat22> {
  protected Mat22 initialValue() {
    return new Mat22();
  }
}

...etc. (sadly, we can't do it generically because type erasure makes returning a new instance of the generic type impossible, so we actually need to write a version for each type that we want to use 🙁 )

Then we'd pop one in place of each private static member currently being used. Refactoring-wise, probably easiest to start with "Encapsulate field" on each private static, then just edit the getter/setter to access the ThreadLocal instead. Come to think of it, depending on the performance hit, we might even be able to keep the two options side by side (i.e. use the ThreadLocal for possible multithreaded environments, but the private static variable for single threaded cases) just by switching a global flag, but we can worry about that later if the performance is an issue.
ewjordan wrote:
Then we'd pop one in place of each private static member currently being used. Refactoring-wise, probably easiest to start with "Encapsulate field" on each private static, then just edit the getter/setter to access the ThreadLocal instead. Come to think of it, depending on the performance hit, we might even be able to keep the two options side by side (i.e. use the ThreadLocal for possible multithreaded environments, but the private static variable for single threaded cases) just by switching a global flag, but we can worry about that later if the performance is an issue.


I'd have to say I'm somewhat against this, I think the objectpool is much cleaner than the static pool objects, where there would be one threadlocal that references an object with all the stacks on it (or one threadlocal per stack, which might be better multithreading-wise). Also because I've just spent some time already doing this hehe :/

That said, this method would be faster (no need to deal with stacks). I just don't think it's as clean. Plus I do still want to run some benchmarks doing the hashmap(table?) vs threadlocal, and i guess also versus a the static threadlocal you're suggesting.


Edit: but what I think could be large improvement areas are in contactsolver and island where a whole lot of array creation and instantiation is happening. If we can make that stuff more reusable, I think it could give the engine another boost.

On that note, what object could we keep around, and what object need to be instantiated every time?
I'm happy to say, pooled objects are now completely thread safe. I'm pretty sure I got them all, if you find any more problems then definitely let me know.

They are all pooled using the new org.jbox2d.pooling library, which also has nice array and stack support.
That sounds great! I will check it out and start testing today.
Hmm, it basically seems to work great, I had 30 worlds running in parallel and no multithreading issues.

However the contact listener behaviour seems to have changed: Before updating
-when hitting a sensor I got an "add"
-while the two objects were touching probably a few "add"s and "remove"s were produced (but always in a way that in total there were more adds then removes)
-when loosing contact with the sensor a "remove" was called resulting in an equal number of total add and remove calls
So for deciding if I have contact with a sensor I just counted the adds and removes and if I counted more adds there was contact.
Currently I seem to get an awful lot of "adds" and no(!) removes.

I don't know if this has actually to do with the multithreading issue / performance optimization, if not I can start a new topic.
What do you think?
since this is probably not a multiple worlds issue I started a new thread:
viewtopic.php?f=9&t=3526

Thanks a lot for your effort to enable multiple worlds again, it is greatly appreciated!

Original thread: https://box2d.org/forum/viewtopic.php?t=3441