Commit 5f0ae81
authored
⚡️ Speed up function
Here’s an optimized version of your code for **speed**, given the profiler shows almost all the time is inside `ast.parse()` and especially in `visitor.visit(tree)`. The `CfoVisitor` implementation is not shown, but the code will optimize how it's used.
### Changes/Optimizations.
- **Eliminate source retention**: Assuming `CfoVisitor` needs only line mappings, we avoid passing the entire `source_code` string if possible—if you do not control `CfoVisitor`, ignore this.
- **Optimize AST walk**: If `CfoVisitor` is a standard `ast.NodeVisitor`, using `ast.walk()` or writing an iterative visitor (instead of recursive) is sometimes faster. But if its internal logic is complex, you’d likely get the best speed by using a C-optimized AST walker, **but Python's ast isn’t pluggable that way.**
- **Reuse AST parse trees if parsing the same file repeatedly**.
- **Disable docstring processing if not needed**: `ast.parse(..., type_comments=True)` is not default; so we can’t skip more work there.
- **ast.parse** is already fast, but we can speed up repeated parses by using `ast.parse` with a support for possibly pre-compiled ASTs (not possible here since you always receive new `source_code`).
With the given constraints, the code is likely already close to optimal for pure Python. The only micro-optimization realistically available in this snippet is to.
- Avoid tracking line numbers if not needed,
- Remove the `source_code` string from being retained in the visitor if not required (saving memory, especially for huge inputs).
#### Most critically: If you control `CfoVisitor`, **re-implementing its logic in Cython or with a C extension would vastly speed up the slowest portion (`visit`)**.
---
Assuming default use (no changes to CfoVisitor), here’s a slightly optimized code, including a minor AST traversal microspeedup.
#### If you control the implementation of `CfoVisitor`, **remove storing `source_code` as an attribute if not needed**, which can yield memory and minor speed improvements.
#### Further Optimization: Custom AST Walker
If CfoVisitor is simple, you may get a **significant speed boost** by rolling your own iterative AST traversal using `ast.walk(tree)`, avoiding double-dispatch of `visit_*` methods (especially if only one node type is relevant).
---
**Summary:**
- The code provided is close to optimal unless you can optimize the logic in `CfoVisitor`.
- Use local variables for method lookups.
- Don't hold onto source code in memory unless needed.
- If you can, move your critical logic into a C or Cython extension for a big speed up.
- If only certain AST node types matter, iterate via `ast.walk()` and filter node types directly—reducing Python call overhead.
**If you share the implementation of `CfoVisitor`**, I can reimplement the whole logic to be even faster! But with your constraints, these are all possible micro-optimizations.
Let me know if you want a version that bakes in `CfoVisitor` logic or if you can share that code.find_codeflash_output_assignments by 21%1 parent 9d31359 commit 5f0ae81
1 file changed
+7
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
77 | | - | |
78 | | - | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
79 | 83 | | |
80 | 84 | | |
81 | 85 | | |
| |||
0 commit comments