⚡️ Speed up function add_runtime_comments_to_generated_tests by 239% in PR #488 (fix-runtime-comments)
#489
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 #488
If you approve this dependent PR, these changes will be merged into the original PR branch
fix-runtime-comments.📄 239% (2.39x) speedup for
add_runtime_comments_to_generated_testsincodeflash/code_utils/edit_generated_tests.py⏱️ Runtime :
1.16 seconds→341 milliseconds(best of24runs)📝 Explanation and details
Here’s a heavily optimized rewrite of your function, focused on the main bottleneck: the
tree.visit(transformer)call inside the main loop (~95% of your runtime!). Across the entire function, the following optimizations (all applied without changing any functional output) are used.relative_topath gymnastics and conversions) are moved out of inner loops and stored as sensible lookups, since their results are almost invariant across tests.forloops per invocation inleave_SimpleStatementLineare merged into one, halving search cost.(rel_path, qualified_name, cfo_loc)to their runtimes. This makes runtime-access O(1) instead of requiring a full scan per statement.find_codeflash_output_assignmentsto operate on the CST or directly on the parsed AST rather than reparsing source code. (The code preserves your current approach but this is a further large opportunity.)leave_SimpleStatementLine’s_contains_myfunc_callis further micro-optimized by breaking as soon as a match is found (using exception for early escape), avoiding unnecessary traversal.GeneratedTestsobjects are only constructed once and appended.Below is the optimized code, with comments kept as close as possible to your original code (apart from changed logic).
Summary of key gains:
Still slow?:
The biggest remaining hit will be the
find_codeflash_output_assignments(which reparses source); move this to operate directly on CST if possible for further big wins.Let me know your measured speedup! 🚀
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
test_add_runtime_comments.py::TestAddRuntimeComments.test_add_runtime_comments_class_methodtest_add_runtime_comments.py::TestAddRuntimeComments.test_add_runtime_comments_multiple_assignmentstest_add_runtime_comments.py::TestAddRuntimeComments.test_add_runtime_comments_multiple_teststest_add_runtime_comments.py::TestAddRuntimeComments.test_add_runtime_comments_no_codeflash_outputtest_add_runtime_comments.py::TestAddRuntimeComments.test_add_runtime_comments_no_matching_runtimestest_add_runtime_comments.py::TestAddRuntimeComments.test_add_runtime_comments_performance_regressiontest_add_runtime_comments.py::TestAddRuntimeComments.test_add_runtime_comments_simple_functiontest_add_runtime_comments.py::TestAddRuntimeComments.test_basic_runtime_comment_additiontest_add_runtime_comments.py::TestAddRuntimeComments.test_different_time_formatstest_add_runtime_comments.py::TestAddRuntimeComments.test_invalid_python_code_handlingtest_add_runtime_comments.py::TestAddRuntimeComments.test_missing_test_resultstest_add_runtime_comments.py::TestAddRuntimeComments.test_multiple_generated_teststest_add_runtime_comments.py::TestAddRuntimeComments.test_multiple_runtimes_uses_minimumtest_add_runtime_comments.py::TestAddRuntimeComments.test_multiple_test_functionstest_add_runtime_comments.py::TestAddRuntimeComments.test_multistatement_line_handlingtest_add_runtime_comments.py::TestAddRuntimeComments.test_no_codeflash_output_assignmenttest_add_runtime_comments.py::TestAddRuntimeComments.test_partial_test_resultstest_add_runtime_comments.py::TestAddRuntimeComments.test_preserved_test_attributes🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr488-2025-07-02T23.50.27and push.