Commit 7f4c01e
authored
Here is an optimized rewrite of your `FunctionRanker` class.
**Key speed optimizations applied:**
1. **Avoid repeated loading of function stats:**
The original code reloads function stats for each function during ranking (`get_function_ttx_score()` is called per function and loads/returns). We prefetch stats once in `rank_functions()` and reuse them for all lookups.
2. **Inline and batch lookups:**
We use a helper to batch compute scores directly via a pre-fetched `stats` dict. This removes per-call overhead from attribute access and creation of possible keys inside the hot loop.
3. **Minimal string operations:**
We precompute the two possible key formats needed for lookup (file:qualified and file:function) for all items only ONCE, instead of per invocation.
4. **Skip list-comprehension in favor of tuple-unpacking:**
Use generator expressions for lower overhead when building output.
5. **Fast path with `dict.get()` lookup:**
Avoid redundant `if key in dict` by just trying `dict.get(key)`.
6. **Do not change signatures or behavior.
Do not rename any classes or functions.
All logging, ordering, functionality is preserved.**
**Summary of performance impact:**
- The stats are loaded only once, not per function.
- String concatenations for keys are only performed twice per function (and not redundantly in both `rank_functions` and `get_function_ttx_score`).
- All lookup and sorting logic remains as in the original so results will match, but runtime (especially for large lists) will be significantly better.
- If you want, you could further optimize by memoizing scores with LRU cache, but with this design, dictionary operations are already the bottleneck, and this is the lowest-overhead idiomatic Python approach.
- No imports, function names, or signatures are changed.
Let me know if you need further GPU-based or numpy/pandas-style speedups!
1 parent 059b4dc commit 7f4c01e
1 file changed
+21
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| 7 | + | |
6 | 8 | | |
7 | 9 | | |
8 | 10 | | |
| |||
105 | 107 | | |
106 | 108 | | |
107 | 109 | | |
108 | | - | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
109 | 114 | | |
| 115 | + | |
| 116 | + | |
110 | 117 | | |
111 | | - | |
112 | | - | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
113 | 131 | | |
114 | 132 | | |
115 | 133 | | |
116 | 134 | | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | 135 | | |
122 | 136 | | |
123 | 137 | | |
| |||
0 commit comments