|
87 | 87 |
|
88 | 88 | --- |
89 | 89 |
|
90 | | -### 6. Cached Eccentricity (commit `92edee0`) ⭐ |
| 90 | +### 7. Vectorized Phase Operations (commit `a0940fe`) ⭐ |
| 91 | + |
| 92 | +**Problem**: Python loops in phase gradient/curvature (nested neighbor iterations) |
| 93 | +**Solution**: NumPy vectorization with broadcasting + pre-extracted phases |
| 94 | +**Impact**: |
| 95 | +- **Additional 2% speedup** (1.707s → 1.670s) |
| 96 | +- Phase gradient: Vectorized wrapped differences via `(diffs + π) % 2π - π` |
| 97 | +- Phase curvature: Vectorized circular mean via `np.cos`/`np.sin` arrays |
| 98 | +- Eliminates nested Python loops over neighbors |
| 99 | + |
| 100 | +**TNFR Alignment**: |
| 101 | +- Batch operations = **coherent phase computations** (vs sequential) |
| 102 | +- Respects circular topology in phase space (wrapped differences) |
| 103 | +- Read-only, preserves all field semantics |
| 104 | + |
| 105 | +**Code Changes**: |
| 106 | +- Pre-extract phases dict: `{node: _get_phase(G, node) for node in nodes}` |
| 107 | +- Batch neighbor phases: `np.array([phases[j] for j in neighbors])` |
| 108 | +- Vectorized wrapping, mean, cos/sin operations |
| 109 | + |
| 110 | +**Files**: |
| 111 | +- `src/tnfr/physics/fields.py` (gradient + curvature functions) |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +### 8. Grammar Early Exit (commit `a0940fe`) |
| 116 | + |
| 117 | +**Problem**: Grammar validation checks all 8 rules even after first failure |
| 118 | +**Solution**: Optional `stop_on_first_error` parameter for early exit |
| 119 | +**Impact**: |
| 120 | +- **10-30% speedup** when sequences invalid (depends on error location) |
| 121 | +- Default: `False` (preserves comprehensive diagnostic reporting) |
| 122 | +- Use case: High-throughput validation where first error sufficient |
| 123 | + |
| 124 | +**TNFR Alignment**: |
| 125 | +- Optional optimization (respects need for complete diagnostics) |
| 126 | +- Does not weaken grammar - same validation logic |
| 127 | +- Trade-off: Performance vs diagnostic completeness |
| 128 | + |
| 129 | +**Code Changes**: |
| 130 | +```python |
| 131 | +def validate_sequence(..., stop_on_first_error: bool = False): |
| 132 | + # Check U1a |
| 133 | + if stop_on_first_error and not valid_init: |
| 134 | + return False, messages |
| 135 | + # ... repeat for U1b, U2, U3, U4a, U4b, U5 |
| 136 | +``` |
| 137 | + |
| 138 | +**Files**: |
| 139 | +- `src/tnfr/operators/grammar_core.py` (validate_sequence method) |
| 140 | + |
| 141 | +--- |
| 142 | + |
| 143 | +## 📊 Performance Summary |
| 144 | + |
| 145 | +### Validation Speedup Timeline (Updated) |
| 146 | + |
| 147 | +| Stage | Time (500 nodes, 10 runs) | Speedup vs Baseline | Cumulative | |
| 148 | +|-------|---------------------------|---------------------|------------| |
| 149 | +| **Baseline** | 6.138s | 1.0× | - | |
| 150 | +| + Fast diameter | 3.838s | 1.6× | **37.5% ↓** | |
| 151 | +| + Cached eccentricity | 1.707s | 3.6× | **72% ↓** | |
| 152 | +| + Vectorized phases | **1.670s** | **3.7×** | **73% ↓** | |
| 153 | + |
| 154 | +### Cumulative Improvements |
| 155 | + |
| 156 | +| Metric | Baseline | Current | Improvement | |
| 157 | +|--------|----------|---------|-------------| |
| 158 | +| **Total time** | 6.138s | 1.670s | **3.7× faster (73% ↓)** | |
| 159 | +| **Function calls** | 23.9M | 6.3M | **74% reduction** | |
| 160 | +| **Diameter** | ~50ms | ~1ms | **50× faster** | |
| 161 | +| **Eccentricity (1st)** | 2.3s | 0.2s | **10× faster** | |
| 162 | +| **Eccentricity (cached)** | 2.3s | 0.000s | **∞× speedup** | |
| 163 | +| **Phase ops** | ~5-10ms | ~2-4ms | **2-3× faster** | |
| 164 | + |
| 165 | +### Current Bottleneck: Φ_s (Expected) |
91 | 166 |
|
92 | 167 | **Problem**: Eccentricity O(N²) repeated 10× per validation (2.3s bottleneck) |
93 | 168 | **Solution**: Cache with `dependencies={'graph_topology'}` via TNFR paradigm |
|
0 commit comments