Perf: Optimize Network.run loop via unswitching and int tracking#1752
Perf: Optimize Network.run loop via unswitching and int tracking#1752ayush4874 wants to merge 1 commit intobrian-team:masterfrom
Conversation
|
@mstimberg can you also have a look here? |
|
Hi @ayush4874, thanks for the PR and apologies that I did not reply earlier. I appreciate the reasoning behind the PR, but I am hesitant accepting it in its current form. I agree that checking if single_clock:
advance = clock.advance
else:
def advance():
for c in clocks:
c.advance()
...
while ...:
...
advance()Again, not quite sure about the best approach here, but generally speaking I'd prefer a clean/readable solution over an optimized one (if it is only about a few percentage points in edge cases). |
1b26c1d to
a7c367d
Compare
a7c367d to
0aacbf2
Compare
|
Hi @mstimberg, I've updated the PR. I switched to the function dispatch approach you suggested (for both To keep performance up, I pre-bound |
|
Thanks again @ayush4874 – same comment as for #1766 :-) Looks good at first sight, but I will do a more thorough review beginning of next week. |
I noticed the main simulation loop in
Network.runhad some overhead from repeated checks that don't change during a run (likesingle_clockanddevice).I refactored it to split the "fast path" (single clock) from the multi-clock logic.
Also,
timestep[0]is a numpy scalar, which is surprisingly slow to access in a hot loop. I replaced it with a local integer counter just for the loop condition.Benchmarks (1M steps, pure Python):
Verified that
stop()andNetworkOperationstill trigger correctly with the new logic.