Commit c9f6483
authored
Optimize _compare_hypothesis_tests_semantic
The optimized code achieves a **32% speedup** by eliminating redundant data structures and reducing iteration overhead through two key optimizations:
**1. Single-pass aggregation instead of list accumulation:**
- **Original**: Uses `defaultdict(list)` to collect all `FunctionTestInvocation` objects per test function, then later iterates through these lists to compute failure flags with `any(not ex.did_pass for ex in orig_examples)`
- **Optimized**: Uses plain dicts with 2-element lists `[count, had_failure]` to track both example count and failure status in a single pass, eliminating the need to store individual test objects or re-scan them
**2. Reduced memory allocation and access patterns:**
- **Original**: Creates and stores complete lists of test objects (up to 9,458 objects in large test cases), then performs expensive `any()` operations over these lists
- **Optimized**: Uses compact 2-item lists per test function, avoiding object accumulation and expensive linear scans
The line profiler shows the key performance gains:
- Lines with `any(not ex.did_pass...)` in original (10.1% and 10.2% of total time) are completely eliminated
- The `setdefault()` operations replace the more expensive `defaultdict(list).append()` calls
- Overall reduction from storing ~9,458 objects to just tracking summary statistics
**Best performance gains** occur in test cases with:
- **Large numbers of examples per test function** (up to 105% faster for `test_large_scale_all_fail`)
- **Many distinct test functions** (up to 75% faster for `test_large_scale_some_failures`)
- **Mixed pass/fail scenarios** where the original's `any()` operations were most expensive
The optimization maintains identical behavior while dramatically reducing both memory usage and computational complexity from O(examples) to O(1) per test function group.1 parent 6968ab3 commit c9f6483
1 file changed
+22
-23
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
| |||
138 | 137 | | |
139 | 138 | | |
140 | 139 | | |
141 | | - | |
142 | 140 | | |
143 | 141 | | |
144 | 142 | | |
| |||
148 | 146 | | |
149 | 147 | | |
150 | 148 | | |
151 | | - | |
152 | | - | |
| 149 | + | |
| 150 | + | |
153 | 151 | | |
154 | | - | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
155 | 157 | | |
156 | | - | |
157 | | - | |
| 158 | + | |
158 | 159 | | |
159 | | - | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
160 | 165 | | |
161 | | - | |
162 | | - | |
163 | | - | |
| 166 | + | |
| 167 | + | |
164 | 168 | | |
165 | 169 | | |
166 | | - | |
167 | | - | |
| 170 | + | |
| 171 | + | |
168 | 172 | | |
169 | 173 | | |
170 | | - | |
171 | | - | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
172 | 178 | | |
173 | 179 | | |
174 | | - | |
175 | | - | |
| 180 | + | |
176 | 181 | | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | 182 | | |
184 | 183 | | |
185 | 184 | | |
| |||
0 commit comments