⚡️ Speed up method Event.statistics by 11%
#7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 11% (0.11x) speedup for
Event.statisticsinsrc/anyio/_backends/_trio.py⏱️ Runtime :
678 microseconds→611 microseconds(best of35runs)📝 Explanation and details
The optimization removes the keyword argument
tasks_waiting=when constructing theEventStatisticsobject, passingorig_statistics.tasks_waitingas a positional argument instead.Key Change:
EventStatistics(tasks_waiting=orig_statistics.tasks_waiting)toEventStatistics(orig_statistics.tasks_waiting)Why this is faster:
In Python, positional arguments are faster than keyword arguments because:
Performance Impact:
The line profiler shows the optimized version reduces the time spent in the return statement from 926,309ns to 870,294ns (about 6% improvement on that line). This micro-optimization is particularly effective when the method is called frequently, as evidenced by the test results.
Best suited for:
This optimization provides consistent 3-11% speedups across most test cases, with the largest gains (11.5%) appearing in high-frequency scenarios like the test with 1000 Event instances. The optimization is most beneficial when
statistics()is called repeatedly in performance-critical code paths.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-Event.statistics-mhl34gu1and push.