Commit 986bc2f
perf: Implement Phase 2 optimizations - caching, indexing, profiling infrastructure
Implement key optimizations from Phase 2 advanced optimization plan:
profiling infrastructure, intelligent caching, and data structure optimizations.
TRACK 1: PROFILING INFRASTRUCTURE
- scripts/profile_utils.py: Comprehensive profiling utilities
- @profile_function decorator with cProfile integration
- @time_function for quick timing
- @profile_memory for memory profiling
- PerformanceMonitor context manager
- benchmark_comparison() for A/B testing
- benchmarks/profile_suite.py: Profiling test suite
- Profiles 5 key areas: scanner, patterns, cost tracker, feedback loops, file I/O
- Saves .prof files for snakeviz visualization
- Measures baseline performance for future optimizations
TRACK 4: INTELLIGENT CACHING (HIGH PRIORITY)
File: src/empathy_os/project_index/scanner.py
1. File Hash Caching (@lru_cache, maxsize=1000)
- _hash_file(): Cache SHA256 hashes for change detection
- Memory: ~64KB (64 bytes × 1000 entries)
- Expected hit rate: 80%+ for incremental scans
- Benefit: Avoid re-hashing unchanged files
2. AST Parsing Cache (@lru_cache, maxsize=500)
- _parse_python_cached(): Cache parsed ASTs with file hash
- Memory: ~5MB (10KB per AST × 500 entries)
- Expected hit rate: 90%+ for incremental operations
- Benefit: Skip expensive ast.parse() for unchanged files
- Invalidation: Automatic via file_hash parameter
3. Updated _analyze_code_metrics():
- Uses cached AST parsing instead of ast.parse() directly
- Significant speedup for repeated scans
- No functional changes - drop-in optimization
TRACK 3: DATA STRUCTURE OPTIMIZATION (MEDIUM PRIORITY)
File: src/empathy_os/pattern_library.py
1. Index Structures (O(1) lookups):
- _patterns_by_type: Dict[pattern_type -> List[pattern_ids]]
- _patterns_by_tag: Dict[tag -> List[pattern_ids]]
- Reduces query_patterns from O(n) to O(k) where k = matching patterns
2. Optimized query_patterns():
- When pattern_type specified: O(1) index lookup instead of O(n) scan
- Expected speedup: 50%+ for type-filtered queries
- Backward compatible - same API, better performance
3. New Helper Methods:
- get_patterns_by_tag(tag): O(1) tag-based lookup
- get_patterns_by_type(type): O(1) type-based lookup
- Enable efficient bulk retrieval
TESTING:
✅ All scanner tests passing (73 tests)
✅ All pattern tests passing (63 tests)
✅ No regressions detected
✅ Optimizations are transparent to callers
PERFORMANCE EXPECTATIONS:
Based on Phase 2 plan targets:
- Scanner caching: 30-50% faster for incremental scans
- Pattern lookup: 50%+ faster for type-filtered queries
- AST parsing: 90%+ cache hit rate eliminates redundant parsing
INFRASTRUCTURE ADDITIONS:
- Profiling utilities ready for identifying bottlenecks
- Benchmark comparison tools for validating optimizations
- Foundation for Track 2 (generators) and remaining optimizations
FILES MODIFIED: 2 core files + 2 new infrastructure files
LINES ADDED: ~350 lines of optimization code + profiling tools
Next Steps (from Phase 2 plan):
- Run profiling suite to identify additional bottlenecks
- Convert high-value list comprehensions to generators
- Add cache statistics monitoring
- Measure actual performance improvements
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>1 parent e136536 commit 986bc2f
File tree
4 files changed
+546
-14
lines changed- benchmarks
- scripts
- src/empathy_os
- project_index
4 files changed
+546
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
0 commit comments