Backstory
I've come across a performance hit when using Box2D's raycasts. I've switched to using a MT version of Box2D which really helps the physics aspect and allows all 4 of my cores to be used but when implement raycasts into my entities (which each do 840 raycasts) most of the time is spent waiting for the main thread to finish it's job.
Main Point
So I've created a crude thread pool to offload the raycasts through a task-worker-queue structure. But when I tested it compared against ST I found MT to be ~twice as slow as ST. I'm not sure whether it's my thread pool structure (which I tested to be faster when the task at hand is CPU intensive, not simple tasks) or that Box2D's raycasts aren't thread supported (I have debugged the code step by step and kinda see that 2 threads never occupy the raycast function at the same time.
Code
I've come across a performance hit when using Box2D's raycasts. I've switched to using a MT version of Box2D which really helps the physics aspect and allows all 4 of my cores to be used but when implement raycasts into my entities (which each do 840 raycasts) most of the time is spent waiting for the main thread to finish it's job.
Main Point
So I've created a crude thread pool to offload the raycasts through a task-worker-queue structure. But when I tested it compared against ST I found MT to be ~twice as slow as ST. I'm not sure whether it's my thread pool structure (which I tested to be faster when the task at hand is CPU intensive, not simple tasks) or that Box2D's raycasts aren't thread supported (I have debugged the code step by step and kinda see that 2 threads never occupy the raycast function at the same time.
Code
Code: Select all
class Task : public b2RayCastCallback
{
void Box2DRaycast::execute()
{
m_world->RayCast(this, m_p1, m_p2);
}
float32 Box2DRaycast::ReportFixture(b2Fixture* fixture, const b2Vec2& point, const b2Vec2& normal, float32 fraction)
{
if (fraction < m_fraction)
{
m_hit = true;
m_fixture = fixture;
m_point = point;
m_normal = normal;
m_fraction = fraction;
}
return fraction;
}
}
// Test task list
std::vector<Task> tasks(840 * num_entites); // any number of entities
for (Task t : tasks)
threadpool.add_task(t); // This adds the task to a queue which any waiting thread grabs and executes