⚡️ Speed up method JavaScriptSupport._find_and_extract_body by 11% in PR #1780 (fix/normalizer)#1781
Conversation
The hot loop replaced recursive AST traversal with an iterative stack-based DFS and eliminated per-node UTF-8 decoding by comparing byte slices via a single `memoryview` and pre-encoded target name. Line profiler shows the original recursive `find_function_node` consumed 90% of runtime (9.79 ms), while the optimized iterative loop distributes work across `node.type` checks (34.8%) and `stack.pop()` (4.1%), totaling lower overhead per node. The large-scale test with 1000 variable declarations improved 29.9% (683 µs → 526 µs), confirming the win scales with AST size. Smaller test cases regressed 8–17% due to loop setup cost exceeding recursion savings on trivial trees, a reasonable trade-off for the target workload.
|
Claude finished @codeflash-ai[bot]'s task in 18s —— View job PR Review SummaryPrek ChecksFixed (auto-committed): The PR added Code ReviewLogic issue — DFS traversal order change: The original recursive code traverses children left-to-right. The new iterative version uses: stack.extend(node.children) # push in order
node = stack.pop() # pop reverses the order → right-to-left DFSThis means the optimized code performs a right-to-left pre-order DFS instead of the original left-to-right. If two functions share the same name at different sibling positions in the AST (e.g., overloaded-style declarations), the new code could return a different node than the original. In practice, function names are unique within a scope, so this shouldn't cause real-world issues — but it's a subtle behavioral change worth being aware of. Observation — When processing a Duplicate DetectionMEDIUM confidence: Test CoverageNo existing unit tests cover Summary: The optimization is sound. One lint bug was auto-fixed (redundant |
|
Closing stale optimization PR. |
⚡️ This pull request contains optimizations for PR #1780
If you approve this dependent PR, these changes will be merged into the original PR branch
fix/normalizer.📄 11% (0.11x) speedup for
JavaScriptSupport._find_and_extract_bodyincodeflash/languages/javascript/support.py⏱️ Runtime :
1.91 milliseconds→1.72 milliseconds(best of30runs)📝 Explanation and details
The hot loop replaced recursive AST traversal with an iterative stack-based DFS and eliminated per-node UTF-8 decoding by comparing byte slices via a single
memoryviewand pre-encoded target name. Line profiler shows the original recursivefind_function_nodeconsumed 90% of runtime (9.79 ms), while the optimized iterative loop distributes work acrossnode.typechecks (34.8%) andstack.pop()(4.1%), totaling lower overhead per node. The large-scale test with 1000 variable declarations improved 29.9% (683 µs → 526 µs), confirming the win scales with AST size. Smaller test cases regressed 8–17% due to loop setup cost exceeding recursion savings on trivial trees, a reasonable trade-off for the target workload.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1780-2026-03-06T16.16.57and push.