Performance Optimization System
Overview
The advanced performance optimization system introduced in WeaveDI v3.2.1 achieves 0% overhead in production environments while providing powerful monitoring capabilities in development environments. It leverages environment flags and conditional compilation to ensure optimal performance.
🚀 Core Optimization Features
- ✅ Conditional Performance Tracking: Complete elimination of Task creation in production
- ✅ Compile-time Optimization: Leverages Swift conditional compilation
- ✅ Intelligent Caching: Automatic optimization of frequently used dependencies
- ✅ Memory Efficiency: Elimination of unnecessary tracking data
Environment-specific Performance Strategies
Production Environment (Release)
// Tracking code is completely removed in production
public static func resolve<T>(_ type: T.Type) -> T? where T: Sendable {
let resolved = WeaveDI.Container.live.resolve(type)
// The following code is completely removed by conditional compilation
#if DEBUG && DI_MONITORING_ENABLED
Task { @DIActor in
AutoDIOptimizer.shared.trackResolution(type)
}
#endif
return resolved
}Production Characteristics:
- 0% Tracking Overhead: No Task creation
- Minimal Memory Usage: No tracking data storage
- Optimized Resolution Speed: Only pure resolution logic executes
Development Environment (Debug)
// Rich tracking features in development environment
#if DEBUG && DI_MONITORING_ENABLED
// Detailed performance tracking enabled
let stats = await DIAdvanced.Performance.getStats()
print("📊 Dependency Resolution Statistics:")
print(" - Total Resolutions: \(stats["totalResolutions"] ?? 0)")
print(" - Average Resolution Time: \(stats["averageTime"] ?? 0)ms")
print(" - Cache Hit Rate: \(stats["cacheHitRate"] ?? 0)%")
#endifDevelopment Characteristics:
- Real-time Monitoring: All resolutions tracked
- Performance Analysis: Detailed metrics collection
- Bottleneck Detection: Automatic optimization suggestions
Performance Optimization APIs
DIAdvanced.Performance Class
public enum Performance {
/// Resolve dependencies with performance tracking
public static func resolveWithTracking<T>(_ type: T.Type) -> T? where T: Sendable
/// Mark as frequently used type
@MainActor
public static func markAsFrequentlyUsed<T>(_ type: T.Type)
/// Enable performance optimization
@MainActor
public static func enableOptimization()
/// Return current performance statistics
@MainActor
public static func getStats() async -> [String: Int]
}Real Usage Examples
import WeaveDI
class AppPerformanceManager {
static func initializePerformanceOptimizations() {
#if DEBUG && DI_MONITORING_ENABLED
// Optimization settings executed only in development environment
Task { @MainActor in
DIAdvanced.Performance.enableOptimization()
// Mark core services as frequently used
DIAdvanced.Performance.markAsFrequentlyUsed(UserService.self)
DIAdvanced.Performance.markAsFrequentlyUsed(NetworkService.self)
DIAdvanced.Performance.markAsFrequentlyUsed(CacheService.self)
print("🎯 Performance optimization activated!")
}
#endif
// Nothing executes in production
}
@MainActor
static func printPerformanceReport() async {
#if DEBUG && DI_MONITORING_ENABLED
let stats = await DIAdvanced.Performance.getStats()
print("📈 Performance Report:")
for (key, value) in stats {
print(" \(key): \(value)")
}
#endif
}
}Automatic Optimization System
AutoDIOptimizer Integration
// Automatic optimization system runs in background
@DIActor
public final class AutoDIOptimizer {
/// Conditional resolution tracking
public func trackResolution<T>(_ type: T.Type) {
#if DEBUG && DI_MONITORING_ENABLED
// Analyze resolution patterns
updateResolutionStats(for: type)
// Identify optimization opportunities
if shouldOptimize(type) {
Log.info("🚀 Optimization recommended for \(type) type")
}
#endif
}
/// Control optimization activation
public func setOptimizationEnabled(_ enabled: Bool) {
#if DEBUG && DI_MONITORING_ENABLED
isOptimizationEnabled = enabled
Log.info("⚙️ Auto-optimization \(enabled ? "enabled" : "disabled")")
#endif
}
}Performance Benchmarks
Real Performance Measurements
class PerformanceBenchmark {
static func measureResolutionPerformance() async {
let iterations = 10000
// Production performance measurement
let startTime = CFAbsoluteTimeGetCurrent()
for _ in 0..<iterations {
_ = UnifiedDI.resolve(UserService.self)
}
let productionTime = CFAbsoluteTimeGetCurrent() - startTime
print("🏎️ Production Performance:")
print(" \(iterations) resolutions: \(productionTime * 1000)ms")
print(" Average resolution time: \((productionTime * 1000) / Double(iterations))ms")
#if DEBUG && DI_MONITORING_ENABLED
let stats = await DIAdvanced.Performance.getStats()
print("📊 Development Environment Additional Info:")
print(" Tracked resolutions: \(stats["trackedResolutions"] ?? 0)")
print(" Cache utilization: \(stats["cacheUtilization"] ?? 0)%")
#endif
}
}Performance Comparison Results
| Environment | Task Creation | Memory Usage | Resolution Time |
|---|---|---|---|
| Production | 0 | Minimal | 100% |
| Development (Tracking OFF) | 0 | Minimal | 100% |
| Development (Tracking ON) | Every call | +15% | +5% |
Memory Optimization
Conditional Memory Usage
// Memory-efficient tracking system
#if DEBUG && DI_MONITORING_ENABLED
private var resolutionStats: [String: ResolutionMetrics] = [:]
private var optimizationHints: Set<String> = []
#endif
public func trackResolution<T>(_ type: T.Type) {
#if DEBUG && DI_MONITORING_ENABLED
let typeName = String(describing: type)
resolutionStats[typeName, default: ResolutionMetrics()].increment()
#endif
// 0 memory usage in production
}Memory Usage Patterns
- Production: 0 bytes of tracking data
- Development: ~64 bytes per type (optimized struct)
- Auto Cleanup: Automatic memory release on app termination
Practical Usage Guide
App Startup Configuration
@main
struct MyApp: App {
init() {
setupPerformanceOptimizations()
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
private func setupPerformanceOptimizations() {
#if DEBUG && DI_MONITORING_ENABLED
Task { @MainActor in
DIAdvanced.Performance.enableOptimization()
print("🔧 Development mode: Performance tracking enabled")
}
#else
print("🚀 Production mode: Optimized performance")
#endif
}
}CI/CD Pipeline Verification
#!/bin/bash
# Performance test script
echo "🧪 Production build performance test..."
swift build -c release
echo "🔍 Verify tracking code removal in release binary..."
if nm MyApp | grep -q "trackResolution"; then
echo "❌ Tracking code found in release build!"
exit 1
else
echo "✅ Tracking code removed from release build"
fi
echo "📊 Running performance benchmark..."
./MyApp --performance-testTroubleshooting
Q: Need statistics in production?
A: Implement a separate lightweight metrics system or enable DI_MONITORING_ENABLED only in specific builds.
Q: Slow performance in development?
A: Temporarily disable the DI_MONITORING_ENABLED flag to test production-level performance.
Q: Increasing memory usage?
A: Tracking data is stored only in development environment, so production is unaffected. Reset statistics periodically if needed.
Advanced Optimization Techniques
Custom Performance Metrics
extension DIAdvanced.Performance {
/// Add custom metric
@MainActor
public static func addCustomMetric(_ name: String, value: Int) {
#if DEBUG && DI_MONITORING_ENABLED
customMetrics[name] = value
#endif
}
/// Log performance event
public static func logPerformanceEvent(_ event: String) {
#if DEBUG && DI_MONITORING_ENABLED
Log.performance("📈 \(event)")
#endif
}
}Related APIs
AutoDIOptimizer- Automatic optimization engineEnvironment Flags- Compile-time optimizationUnifiedDI- Unified DI system
This optimization system was introduced in WeaveDI v3.2.1. It's an innovative system providing the perfect balance between production performance and development convenience.