Commit 30063aa
authored
⚡️ Speed up function
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 with `libcst`.
However, gathering `target_functions` can be optimized using a list comprehension with tuple unpacking, while avoiding multiple attribute lookups.
Also, the main time is spent in `module.visit(transformer)` and `cst.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, using `visit_`/`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.
- Combined the logic of extracting `(class_name, function_name)` into a set comprehension for fewer attribute accesses and tighter bytecode.
- Added a check: if `target_functions` is empty, we just return the original code immediately (this prevents any parsing/visiting if there's nothing to decorate).
- Comments left untouched unless relevant code was modified.
- Retained function signature and interface.
- Provided some minor micro-optimizations (generator expressions, less branching).
- Eliminated redundant variable assignments.
### Notes.
- As per your profile, most time is spent in parsing and visiting. Optimize the `AddDecoratorTransformer` for early exits and to do as little as possible, in its own definition (not here), for further improvement.
- Return early for the empty case: saves any parse/visit calls at all.
- If you do ever get code objects, you could use `isinstance(module, cst.Module)` to skip reparsing, but as per the signature we always expect `str`.
If you want even more speed, next steps are:
- Minimize the work that `AddDecoratorTransformer` does (match by qualified name to avoid visiting subtrees needlessly).
- Use native AST parsing/writing if you have full control over decorator syntax constraints.
Let me know if you want to see transformer optimizations as well!add_codeflash_decorator_to_code by 17% in PR #294 (add-timing-info-to-generated-tests)1 parent 5115295 commit 30063aa
1 file changed
+11
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
| |||
85 | 87 | | |
86 | 88 | | |
87 | 89 | | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
94 | 95 | | |
95 | 96 | | |
96 | | - | |
| 97 | + | |
97 | 98 | | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
98 | 102 | | |
99 | 103 | | |
100 | 104 | | |
| |||
0 commit comments