⚡️ Speed up function add_codeflash_decorator_to_code by 17% in PR #294 (add-timing-info-to-generated-tests)
#297
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 #294
If you approve this dependent PR, these changes will be merged into the original PR branch
add-timing-info-to-generated-tests.📄 17% (0.17x) speedup for
add_codeflash_decorator_to_codeincodeflash/benchmarking/instrument_codeflash_trace.py⏱️ Runtime :
596 milliseconds→510 milliseconds(best of18runs)📝 Explanation and details
Here’s an optimized version of your program. The main bottleneck is not in the construction of
target_functions(which is a tiny fraction of the runtime), but in the way you handle parsing and transformation withlibcst.However, gathering
target_functionscan be optimized using a list comprehension with tuple unpacking, while avoiding multiple attribute lookups.Also, the main time is spent in
module.visit(transformer)andcst.parse_module. If you have control over how the transformer (i.e.,AddDecoratorTransformer) is written, you should make it as restrictive and fast as possible, usingvisit_/leave_functions that early-exit on non-target nodes.Below, I’ll only optimize what’s asked–rewriting this function to minimize unnecessary slow steps and any wasted computations, while preserving the code logic and interface.
Changes.
(class_name, function_name)into a set comprehension for fewer attribute accesses and tighter bytecode.target_functionsis empty, we just return the original code immediately (this prevents any parsing/visiting if there's nothing to decorate).Notes.
AddDecoratorTransformerfor early exits and to do as little as possible, in its own definition (not here), for further improvement.isinstance(module, cst.Module)to skip reparsing, but as per the signature we always expectstr.If you want even more speed, next steps are:
AddDecoratorTransformerdoes (match by qualified name to avoid visiting subtrees needlessly).Let me know if you want to see transformer optimizations as well!
✅ Correctness verification report:
⚙️ Existing Unit Tests Details
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-pr294-2025-06-06T05.50.14and push.