⚡️ Speed up method DefaultExecutor.execute_cell_async by 15%
#415
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.
📄 15% (0.15x) speedup for
DefaultExecutor.execute_cell_asyncinmarimo/_runtime/executor.py⏱️ Runtime :
431 microseconds→375 microseconds(best of44runs)📝 Explanation and details
The optimized code achieves a 15% runtime improvement through two key micro-optimizations that reduce overhead in frequently called functions:
1. Cached Module Constant in
_is_coroutine:inspect.CO_COROUTINEto module-level constant_CO_COROUTINEinspectmodule (from 414.6μs to 326.9μs per hit)2. Optimized Regex Pattern in
_raise_name_error:_NAME_ERROR_REGEXat module level instead of recompiling on each callre.findall()with fasterre.search().group(1)since only one match is neededThe optimizations target hot paths in async cell execution where
_is_coroutineis called multiple times per cell (for both body and last_expr evaluation). While the regex optimization affects error cases less frequently, it provides dramatic improvements when NameErrors occur.Test Case Performance:
_is_coroutineoptimizationThe throughput remains unchanged at 23,540 ops/sec as these are micro-optimizations that reduce per-operation overhead rather than changing the fundamental execution model.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-DefaultExecutor.execute_cell_async-mh6a03psand push.