diff --git a/src/core/execution_context/in_place_exec_ctxt.cc b/src/core/execution_context/in_place_exec_ctxt.cc index 0032961ff..bd284a62b 100644 --- a/src/core/execution_context/in_place_exec_ctxt.cc +++ b/src/core/execution_context/in_place_exec_ctxt.cc @@ -140,15 +140,26 @@ void InPlaceExecutionContext::TearDownIterationAll( } void InPlaceExecutionContext::SetupAgentOpsAll( - const std::vector& all_exec_ctxts) {} + const std::vector& all_exec_ctxts) { + auto* sim = Simulation::GetActive(); + auto* env = sim->GetEnvironment(); + auto* param = sim->GetParam(); + for (auto* ec : all_exec_ctxts) { + auto* ctxt = bdm_static_cast(ec); + ctxt->cached_env_ = env; + ctxt->cached_param_ = param; + } +} void InPlaceExecutionContext::TearDownAgentOpsAll( const std::vector& all_exec_ctxts) {} void InPlaceExecutionContext::Execute( Agent* agent, AgentHandle ah, const std::vector& operations) { - auto* env = Simulation::GetActive()->GetEnvironment(); - auto* param = Simulation::GetActive()->GetParam(); + auto* env = + cached_env_ ? cached_env_ : Simulation::GetActive()->GetEnvironment(); + auto* param = + cached_param_ ? cached_param_ : Simulation::GetActive()->GetParam(); if (param->thread_safety_mechanism == Param::ThreadSafetyMechanism::kUserSpecified) { diff --git a/src/core/execution_context/in_place_exec_ctxt.h b/src/core/execution_context/in_place_exec_ctxt.h index 7413d373b..55e07a3d4 100644 --- a/src/core/execution_context/in_place_exec_ctxt.h +++ b/src/core/execution_context/in_place_exec_ctxt.h @@ -34,6 +34,9 @@ namespace bdm { +class Environment; +struct Param; + namespace in_place_exec_ctxt_detail { class InPlaceExecutionContext_NeighborCacheValidity_Test; } @@ -157,6 +160,11 @@ class InPlaceExecutionContext : public ExecutionContext { /// Cache the value of Param::cache_neighbors bool cache_neighbors_ = false; + /// Cached pointers refreshed once per agent-ops phase in SetupAgentOpsAll, + /// so Execute avoids per-agent Simulation::GetActive() lookups. + Environment* cached_env_ = nullptr; + const Param* cached_param_ = nullptr; + /// Check whether or not the neighbors in `neighbor_cache_` were queried with /// the same squared radius (`cached_squared_search_radius_`) as currently /// being queried with (`query_squared_radius_`)