Skip to content

Commit 6b7632e

Browse files
committed
Cache env and param pointers in InPlaceExecutionContext
Execute() called Simulation::GetActive()->GetEnvironment() and GetParam() for every agent every step. These pointers are stable within an agent-ops phase, so cache them once in SetupAgentOpsAll and reuse the cached values in Execute. Eliminates two pointer chases per agent per step.
1 parent 67c8d6f commit 6b7632e

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/core/execution_context/in_place_exec_ctxt.cc

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,26 @@ void InPlaceExecutionContext::TearDownIterationAll(
140140
}
141141

142142
void InPlaceExecutionContext::SetupAgentOpsAll(
143-
const std::vector<ExecutionContext*>& all_exec_ctxts) {}
143+
const std::vector<ExecutionContext*>& all_exec_ctxts) {
144+
auto* sim = Simulation::GetActive();
145+
auto* env = sim->GetEnvironment();
146+
auto* param = sim->GetParam();
147+
for (auto* ec : all_exec_ctxts) {
148+
auto* ctxt = bdm_static_cast<InPlaceExecutionContext*>(ec);
149+
ctxt->cached_env_ = env;
150+
ctxt->cached_param_ = param;
151+
}
152+
}
144153

145154
void InPlaceExecutionContext::TearDownAgentOpsAll(
146155
const std::vector<ExecutionContext*>& all_exec_ctxts) {}
147156

148157
void InPlaceExecutionContext::Execute(
149158
Agent* agent, AgentHandle ah, const std::vector<Operation*>& operations) {
150-
auto* env = Simulation::GetActive()->GetEnvironment();
151-
auto* param = Simulation::GetActive()->GetParam();
159+
auto* env =
160+
cached_env_ ? cached_env_ : Simulation::GetActive()->GetEnvironment();
161+
auto* param =
162+
cached_param_ ? cached_param_ : Simulation::GetActive()->GetParam();
152163

153164
if (param->thread_safety_mechanism ==
154165
Param::ThreadSafetyMechanism::kUserSpecified) {

src/core/execution_context/in_place_exec_ctxt.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434

3535
namespace bdm {
3636

37+
class Environment;
38+
struct Param;
39+
3740
namespace in_place_exec_ctxt_detail {
3841
class InPlaceExecutionContext_NeighborCacheValidity_Test;
3942
}
@@ -157,6 +160,11 @@ class InPlaceExecutionContext : public ExecutionContext {
157160
/// Cache the value of Param::cache_neighbors
158161
bool cache_neighbors_ = false;
159162

163+
/// Cached pointers refreshed once per agent-ops phase in SetupAgentOpsAll,
164+
/// so Execute avoids per-agent Simulation::GetActive() lookups.
165+
Environment* cached_env_ = nullptr;
166+
const Param* cached_param_ = nullptr;
167+
160168
/// Check whether or not the neighbors in `neighbor_cache_` were queried with
161169
/// the same squared radius (`cached_squared_search_radius_`) as currently
162170
/// being queried with (`query_squared_radius_`)

0 commit comments

Comments
 (0)