Commit 2e5d728
authored
Optimize AiServiceClient._get_valid_candidates
The optimization achieves a **45% speedup** by restructuring how Pydantic model instances are created during markdown parsing.
**Key Change**: Instead of creating an empty `CodeStringsMarkdown()` object and repeatedly appending to its `code_strings` list (which triggers Pydantic field validation on each append), the optimized version collects all code blocks into a plain Python list first, then creates the Pydantic model once with the complete list.
**Why This is Faster**:
- **Reduced Pydantic overhead**: The original code performed O(n) Pydantic field validations as each `CodeString` was appended. The optimization reduces this to O(1) by doing a single model instantiation.
- **Fewer object mutations**: Plain list operations (`code_string_list.append()`) are significantly faster than mutating Pydantic model fields.
- **Profiler evidence**: The line creating `CodeStringsMarkdown()` dropped from 89.6% of function time (18.05ms) to 81% (8.45ms) - nearly a 2x improvement on the bottleneck line.
**Impact on Workloads**: This optimization is particularly effective for scenarios processing multiple markdown code blocks (as shown in test results where larger datasets see 46-47% improvements). Since `parse_markdown_code` is called in a tight loop within `_get_valid_candidates`, the per-call savings compound significantly when processing batches of optimization candidates.
**Test Case Performance**: The optimization shows consistent 25-47% improvements across various test scenarios, with the largest gains on tests with multiple candidates or code blocks, confirming the batching approach scales well.1 parent 6c8be65 commit 2e5d728
1 file changed
+5
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
272 | 272 | | |
273 | 273 | | |
274 | 274 | | |
275 | | - | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
276 | 279 | | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
| 280 | + | |
281 | 281 | | |
282 | 282 | | |
283 | 283 | | |
| |||
0 commit comments