⚡️ Speed up method ScopedVisitor.visit_Import by 6%
#488
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.
📄 6% (0.06x) speedup for
ScopedVisitor.visit_Importinmarimo/_ast/visitor.py⏱️ Runtime :
7.68 milliseconds→7.26 milliseconds(best of99runs)📝 Explanation and details
The optimization achieves a 5% speedup through several key improvements:
1. String splitting optimization in
_get_alias_name:node.name.split(".")[0]with direct string manipulation usingfind()and slicing2. Method localization in
visit_Import:self._get_alias_nameandself._definein local variables to avoid repeated attribute lookupsVariableDataandImportDataclasses asVariableData_andImportData_for faster instantiation3. Reduced function call overhead in
_define:block_idx=block_idxkeyword argument, passing it positionally instead4. Variable assignment optimization:
_get_alias_name, storesself._if_local_then_mangle(asname)result before assignment to avoid redundant callsnode.asnamein a local variable for repeated accessThe optimizations are most effective for:
The optimizations maintain identical functionality while reducing CPU cycles in the most frequently executed code paths during AST traversal.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_o_lbxivc/tmp9x4gfh0l/test_concolic_coverage.py::test_ScopedVisitor_visit_ImportTo edit these changes
git checkout codeflash/optimize-ScopedVisitor.visit_Import-mhcyh9cwand push.