get_event_loop() and _get_running_loop() can be faster.
Case Time Mean Improve
No Change 7.323 +- 0.172 7.323 0.00%
Remove class _RunningLoop 6.513 +- 0.115 6.513 -11.06%
Expand _get_running_loop() inside get_event_loop() 5.851 +- 0.160 5.851 -20.10%
Use Tuple instead of two attributes 6.179 +- 0.099 6.179 -15.62%
Tuple + Remove _RunningLoop 6.026 +- 0.123 6.026 -17.71%
Tuple + return ternary + Remove _RunningLoop 6.060 +- 0.111 6.06 -17.25%
Combine all four optimizations 4.735 +- 0.111 4.735 -35.34%
Remove class _RunningLoop + Use Tuple instead of two attributes 6.241 +- 0.097 6.241 -14.78%
Experimenting with different techniques to optimize get_event_loop and _get_running_loop.
After discuss with Yuri, decide not to expand _get_running_loop inside get_event_loop.
Combine tuple in _running_loop and Remove _RunningLoop (just use threading.local) can achieve the best improvement: 17.71% faster.
I'm not convinced that the PR is worth it. 3% is not interesting on a micro benchmark.
I found a small issue in the PR (left a comment in the PR).
I think using a tuple is still a good idea (even if the speedup is tiny) because logically, both attributes on that threading.local() object are always set and read at the same time. Essentially, it's a pair of (loop, pid), so using a tuple here makes the code easier to reason about.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
The text was updated successfully, but these errors were encountered: