Commit e945280
authored
⚡️ Speed up function
The optimized code achieves a 16% speedup through several key algorithmic and structural improvements:
**Key Optimizations Applied:**
1. **Eliminated Redundant List Operations**: The original code used `layer.remove(vertex_id)` which is O(n) for each removal, requiring list shifting. The optimized version builds new layers using list comprehensions `[vid for vid in layer if "ChatInput" not in vid]`, avoiding expensive in-place mutations.
2. **Reduced Dependency Checking**: The original code checked dependencies with `"ChatInput" in vertex_id and self.get_predecessors(...)` in a single condition, causing short-circuit evaluation issues. The optimized version separates the string check from dependency checking, only calling expensive graph operations when necessary.
3. **Streamlined Data Flow**: Instead of first collecting ChatInputs in `chat_inputs_first`, then extending it, and finally removing from original layers, the optimized version processes everything in a single pass - collecting ChatInputs while immediately checking dependencies, then rebuilding layers without ChatInputs.
4. **Eliminated Intermediate Collections**: The original code created `layer_chat_inputs_first` for each layer and used `extend()` operations. The optimized version directly appends to `chatinputs_ids` and builds the final result structure more efficiently.
**Why These Changes Improve Performance:**
- **List.remove() elimination**: Each `remove()` call is O(n) and requires shifting elements. With multiple ChatInputs per layer, this becomes expensive. List comprehensions are more cache-efficient and avoid memory moves.
- **Better short-circuiting**: Early return on first dependency found prevents unnecessary processing of remaining ChatInputs.
- **Reduced function call overhead**: Fewer intermediate list operations and method calls reduce the per-operation overhead.
**Test Case Performance Patterns:**
The optimization performs best on:
- **Large datasets with no ChatInputs** (117% faster): Avoids expensive string checking and graph operations entirely
- **Scenarios with many ChatInputs but no dependencies** (18-26% faster): Benefits from elimination of list.remove() operations
- **Empty or sparse layers** (18-20% faster): Reduced overhead in layer processing
The optimization performs worse on small test cases with dependencies because the additional upfront setup (creating collections) has overhead that isn't amortized over enough work, but the algorithmic improvements shine on larger inputs where the O(n) operations in the original become bottlenecks.sort_chat_inputs_first by 16%1 parent 9b951ff commit e945280
1 file changed
+25
-20
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
64 | | - | |
65 | | - | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
66 | 72 | | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
84 | 83 | | |
85 | 84 | | |
86 | | - | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
87 | 92 | | |
88 | 93 | | |
89 | 94 | | |
| |||
0 commit comments