Predator_prey_multiagent vs predator_prey_fast.jl example

Hello, in the predator_prey_multiagent.jl example I read the following comment: “This approach is less performant than the version in [Predator-prey dynamics]”.
Has someone more details on what makes it a less performant implementation than the predator_prey_fast.jl version?

Trying to put it in two sentences (and please correct me if I’m wrong here @Libbum or @Datseris):
The performance problem lies in dynamic dispatch during runtime because the compiler cannot optimise dispatch for the concrete agent types beforehand. The faster version deals with this by simply introducing if/else clauses for the different agent types inside the model_step! function instead of relying on dispatching to the correct version of agent_step! for each agent type.

Yes, this is exactly the issue.

For more information, you can take a look at the discussion in this open issue.

We’d like to try Catwalk.jl as a method to auto-optimise this problem, or there are design discussions in the works that will try to give us a cleaner interface for multi-agent model types without dynamic dispatch performance losses.

Luckily, for a few agent types, the performance drop is quite small. But when you’re looking in the 10s or 100s of types, things are noticeable.

That helps a lot. Thanks!

It could also be that the performance cost of dynamic dispatch is negligible for simulations that either run for a longer time or contain computationally expensive calculations/functions. However, this is still mostly unexplored and we will have to devote some time on this topic in the future. See issue #547.

1 Like