⚡️ Speed up function normalize_code
by 22% in PR #733 (deduplicate-better
)
#734
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.
⚡️ This pull request contains optimizations for PR #733
If you approve this dependent PR, these changes will be merged into the original PR branch
deduplicate-better
.📄 22% (0.22x) speedup for
normalize_code
incodeflash/code_utils/deduplicate_code.py
⏱️ Runtime :
96.6 milliseconds
→79.2 milliseconds
(best of72
runs)📝 Explanation and details
The optimization replaces the
remove_docstrings_from_ast
function withfast_remove_docstrings_from_ast
that uses a more efficient traversal strategy.Key optimizations:
Eliminates
ast.walk()
overhead: The original code usesast.walk()
which visits every single node in the AST tree (21,611 hits in profiler). The optimized version uses a custom stack-based traversal that only visits nodes that can actually contain docstrings.Targeted traversal: Instead of examining all AST nodes, the optimized version only traverses
FunctionDef
,AsyncFunctionDef
,ClassDef
, andModule
nodes - the only node types that can contain docstrings in theirbody[0]
position.Reduced function call overhead: The stack-based approach eliminates the overhead of
ast.walk()
's generator-based iteration, reducing the number of Python function calls from 21,611 to just the nodes that matter.Performance impact: The docstring removal step drops from 131.4ms (25.5% of total time) to just 3.07ms (0.8% of total time) - a 97.7% reduction in that specific operation.
Test case effectiveness: The optimization shows consistent 10-25% speedups across all test cases, with the largest gains (23-24%) appearing in tests with many variables or docstrings (
test_large_many_variables_*
,test_large_docstring_removal_scaling
). Even simple cases benefit from the reduced AST traversal overhead.The optimization is particularly effective for code with deep nesting or many function/class definitions, as it avoids visiting irrelevant leaf nodes like literals, operators, and expressions that cannot contain docstrings.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
test_code_deduplication.py::test_deduplicate1
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr733-2025-09-13T23.50.11
and push.