⚡️ Speed up method Lock.statistics by 24%
#9
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.
📄 24% (0.24x) speedup for
Lock.statisticsinsrc/anyio/_backends/_asyncio.py⏱️ Runtime :
32.5 microseconds→26.2 microseconds(best of6runs)📝 Explanation and details
The optimization achieves a 24% speedup by eliminating redundant attribute lookups in the
statistics()method.Key Changes:
owner_task = self._owner_taskto store the attribute value locallyself.locked()call with directowner_task is not Nonecomparisonowner_taskvariable instead of accessingself._owner_taskmultiple timesWhy This Works:
self._owner_task) involves dictionary lookups and method dispatch overheadself._owner_tasktwice: once in the conditional and again when callingself.locked()self.locked()method call added function call overhead for a simpleis not NonecheckPerformance Impact:
The line profiler shows the
returnstatement time dropped from 3.51ms to 1.48ms (58% reduction), while the overall method time improved from 3.86ms to 2.03ms. Test cases consistently show 15-32% speedups across different scenarios, with the optimization being most effective for frequently called code paths where every microsecond matters in async applications.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_ce_a9iww/tmptsx7ijih/test_concolic_coverage.py::test_Lock_statisticsTo edit these changes
git checkout codeflash/optimize-Lock.statistics-mhl6z6ptand push.