⚡️ Speed up function run_async by 33%
#36
+14
−1
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.
📄 33% (0.33x) speedup for
run_asyncincognee/infrastructure/utils/run_async.py⏱️ Runtime :
2.76 seconds→2.07 seconds(best of90runs)📝 Explanation and details
The optimization introduces signature caching to eliminate the expensive
inspect.signature(func)calls that were consuming 37.4% of the original runtime.Key changes:
_signature_cachedictionary that maps function IDs to their cached signatures_has_loop_param()helper function that checks the cache first before callinginspect.signature()Why this is faster:
inspect.signature()is computationally expensive as it performs deep introspection of function metadata. The line profiler shows this call took 28ms per hit in the original code. By caching signatures based on functionid(), subsequent calls to the same function avoid this expensive operation entirely.The optimization reduced the signature check time from 37.4% to just 7% of total runtime, resulting in a 33% overall speedup (2.76s → 2.07s).
Best suited for:
The throughput improvement is modest (0.3%) because the executor overhead dominates in concurrent scenarios, but the runtime improvement is substantial for CPU-bound signature inspection bottlenecks.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-run_async-mh2k85jsand push.