Skip to content

Latest commit

 

History

History
479 lines (374 loc) · 11.6 KB

File metadata and controls

479 lines (374 loc) · 11.6 KB

Flutter Performance Optimization Guide

Quick Start

This guide provides everything you need to build high-performance Flutter applications and plugins. All recommendations are based on modern Flutter best practices and real-world performance metrics.

Key Files

  • SKILL.md - Comprehensive 1,983-line performance optimization guide with 10 major topics
  • PERFORMANCE_GUIDE.md - This file (quick reference and roadmap)

Performance Topics Covered

1. BUILD PERFORMANCE OPTIMIZATION (Section 1.1-1.3)

Why It Matters: Reduces compile time and app startup latency.

Key Techniques:

  • AOT Compilation & Obfuscation
  • Incremental Build Optimization
  • Modular Architecture with Feature Modules
  • Gradle Performance Configuration
  • Split APK Building

Quick Start:

flutter build apk --split-per-abi --obfuscate --split-debug-info=build/app/outputs/mapping

Target: <2 second startup time


2. RENDERING PERFORMANCE & FRAME RATES (Section 2.1-2.4)

Why It Matters: Ensures smooth 60+ FPS for better user experience.

Key Techniques:

  • Frame Rate Consistency (60 FPS standard, 120 FPS premium)
  • RepaintBoundary for rendering isolation
  • Const Widget Constructors
  • State Management Optimization (Provider, Riverpod)
  • Scrolling Performance Optimization

Anti-Patterns to Avoid:

  • Heavy computations in build()
  • Unnecessary widget rebuilds
  • Unoptimized state listeners

Target: Consistent 60 FPS (16.67ms per frame)


3. MEMORY MANAGEMENT (Section 3.1-3.4)

Why It Matters: Prevents crashes on low-end devices and reduces battery drain.

Key Techniques:

  • Memory Profiling with DevTools
  • Image Cache Configuration
  • Network Image Caching
  • Resource Cleanup in dispose()
  • Memory Leak Prevention

Best Practices:

  • Always cancel StreamSubscriptions
  • Dispose Timers in dispose()
  • Use WeakReferences for callbacks
  • Monitor mounted flag before setState()

Target: <100 MB average memory usage


4. PACKAGE & ASSET OPTIMIZATION (Section 4.1-4.3)

Why It Matters: Smaller download size and faster app load times.

Key Techniques:

  • Dependency Auditing
  • Asset Bundle Optimization
  • Selective Font Loading
  • Lazy Asset Loading

Audit Commands:

flutter pub deps --compact
flutter pub outdated
flutter analyze --verbose

Target: <50 MB APK/IPA


5. CODE SPLITTING & LAZY LOADING (Section 5.1-5.4)

Why It Matters: Reduces initial app size for faster first launch.

Key Techniques:

  • Feature Module Architecture
  • Deferred Imports
  • Conditional Compilation
  • Progressive Feature Loading

Benefits:

  • Reduced initial APK size
  • On-demand feature loading
  • Better app distribution
  • Faster time-to-interactive

6. IMAGE OPTIMIZATION (Section 6.1-6.4)

Why It Matters: Images consume 50-80% of app size; optimization has massive impact.

Key Techniques:

  • Image Compression & Format Selection
  • WebP Conversion (30% smaller than JPEG)
  • Responsive Images with Resolution Variants
  • Network Image Caching
  • SVG for Icons (vector graphics)

Image Size Recommendations:

  • Icons: SVG format
  • Photos: WebP or HEIC
  • Thumbnails: Max 400x400px
  • High-res: Multiple variants (@1x, @2x, @3x)

Target: 50% size reduction through format optimization


7. NETWORK OPTIMIZATION (Section 7.1-7.4)

Why It Matters: 50% of app slowness is network-related.

Key Techniques:

  • HTTP/HTTPS Connection Pooling
  • Request Caching Strategies
  • Data Compression (gzip)
  • Offline Queue for Resilience
  • Connectivity Monitoring

Caching Strategy:

// Cache API responses for 1 hour
await fetchCached(url, cacheDuration: Duration(hours: 1));

Target: <500ms average latency, 95% cache hit rate


8. CPU & BATTERY EFFICIENCY (Section 8.1-8.4)

Why It Matters: Users judge apps by battery impact.

Key Techniques:

  • Compute Isolation for Heavy Operations
  • Stream Throttling & Debouncing
  • Background Task Scheduling
  • Battery-Aware UI Adaptation
  • Event Handler Optimization

Battery Monitoring:

final powerSaveMode = await BatteryAwareBehavior().shouldEnablePowerMode();
if (powerSaveMode) {
  // Reduce frame rate or disable animations
}

Target: <5% battery drain per hour


9. PROFILING TOOLS & DEBUGGING (Section 9.1-9.4)

Why It Matters: Can't optimize what you don't measure.

Key Tools:

  • Performance Profiler
  • FPS Monitor
  • Memory Profiler
  • DevTools Integration
  • Timeline Events

Essential Commands:

flutter run --profile
# Open DevTools in browser -> Performance tab

10. APK/IPA SIZE REDUCTION (Section 10.1-10.5)

Why It Matters: Every MB matters for adoption and install rates.

Key Techniques:

  • R8/ProGuard Configuration
  • Split APKs by Architecture
  • Native Library Optimization
  • Bitcode for iOS
  • Bundle Size Analysis

Size Reduction Checklist:

  • Enable code minification: minifyEnabled true
  • Enable resource shrinking: shrinkResources true
  • Use split APKs: --split-per-abi
  • Compress assets to WebP
  • Remove debug symbols: --split-debug-info

Performance Metrics Dashboard

Critical Metrics

Metric Target Critical Priority
App Startup <2s <3s P0
Time to Interactive <5s <7s P0
Frame Rate (avg) 60 FPS >45 FPS P0
Jank (frame drops) <5% <10% P0
Memory (avg) <100 MB <150 MB P1
Memory (peak) <200 MB <250 MB P1
APK/IPA Size <50 MB <100 MB P1
Network Latency <500ms <1000ms P1
Battery Impact <5%/hour <10%/hour P2
Cache Hit Rate >95% >80% P2

Implementation Roadmap

Phase 1: Foundation (Week 1)

  • Enable code obfuscation and minification
  • Configure image caching and compression
  • Implement memory leak prevention patterns
  • Add DevTools performance monitoring
  • Audit and optimize dependencies

Phase 2: Optimization (Week 2)

  • Implement state management best practices
  • Optimize rendering with RepaintBoundary
  • Add network request caching
  • Implement offline support queue
  • Set up battery-aware behavior

Phase 3: Advanced (Week 3)

  • Implement feature module architecture
  • Add deferred imports for large features
  • Configure background task scheduling
  • Implement custom profiling
  • Optimize APK/IPA for distribution

Phase 4: Monitoring (Ongoing)

  • Set up performance dashboards
  • Monitor real user metrics (RUM)
  • Establish performance budgets
  • Regular profiling and optimization cycles
  • Benchmark against competitors

Code Examples Quick Reference

Memory-Safe Widget Pattern

class SafeWidget extends StatefulWidget {
  @override
  State<SafeWidget> createState() => _SafeWidgetState();
}

class _SafeWidgetState extends State<SafeWidget> {
  late StreamSubscription _sub;
  
  @override
  void initState() {
    super.initState();
    _sub = stream.listen((_) {
      if (mounted) setState(() {}); // Check mounted
    });
  }
  
  @override
  void dispose() {
    _sub.cancel(); // Always cleanup
    super.dispose();
  }
  
  @override
  Widget build(BuildContext context) => Container();
}

Efficient List Rendering

ListView.builder(
  addAutomaticKeepAlives: true,
  cacheExtent: 250.0,
  itemCount: 1000,
  itemBuilder: (context, index) {
    return RepaintBoundary(
      child: ListTile(title: Text('Item $index')),
    );
  },
)

Debounced Network Request

final optimizedStream = ComputeOptimizer.debounce(
  searchStream,
  const Duration(milliseconds: 300),
);

Compressed HTTP Request

await CompressionManager.compressedPost(
  Uri.parse('https://api.example.com/data'),
  {'key': 'value'},
);

Testing & Validation

Local Performance Testing

# Profile mode (closest to release)
flutter run --profile

# Trace frame rendering
flutter run --profile --trace-startup

# Memory profiling
flutter run --profile
# Open DevTools -> Memory tab

Real Device Testing

# On low-end device
flutter run --release -d <device_id>

# Monitor frame drops
flutter run --profile
# DevTools -> Performance -> Frame Analysis

Continuous Integration

# .github/workflows/performance.yml
- name: Performance Tests
  run: |
    flutter test --profile
    flutter build apk --release
    # Check APK size
    du -h build/app/outputs/flutter-app.apk

Common Pitfalls & Solutions

Pitfall 1: Unnecessary Rebuilds

Problem: Provider/Consumer rebuilds entire subtree Solution: Use select() for fine-grained updates

// BAD: Full rebuild
Consumer<UserProvider>(
  builder: (_, provider, __) => Text(provider.name),
)

// GOOD: Only rebuild when name changes
Consumer<UserProvider>(
  builder: (_, provider, __) => Text(provider.name),
  child: Consumer<UserProvider>(
    builder: (_, counter, __) => Text('${counter.count}'),
  ),
)

Pitfall 2: Heavy Computations in UI Thread

Problem: Freezes UI during data processing Solution: Offload to isolate

// BAD
final result = heavyComputation(data);

// GOOD
final result = await compute(heavyComputation, data);

Pitfall 3: Uncancelled Streams

Problem: Memory leak, continuous processing Solution: Always cancel in dispose()

@override
void dispose() {
  _subscription.cancel();
  _timer.cancel();
  super.dispose();
}

Pitfall 4: Uncompressed Images

Problem: 80% of app size Solution: Use WebP, compress, provide variants

ffmpeg -i image.png -c:v libwebp -quality 85 image.webp

Performance Benchmarking

Baseline Metrics to Track

  1. App Startup Time - Time from launch to first UI render
  2. First Meaningful Paint - Time when core content is visible
  3. Time to Interactive - When user can interact with app
  4. Frame Rate - FPS during scrolling and animations
  5. Memory Usage - RAM consumption over time
  6. APK/IPA Size - Final distribution size
  7. Network Latency - API response times
  8. Battery Drain - mAh consumed per hour
  9. Crash Rate - OOM and other crashes
  10. User Complaints - App store ratings related to performance

Marketing-Focused Performance Claims

By implementing these best practices, you can claim:

  • Industry-Leading Performance: 60 FPS consistent, sub-2s startup
  • Lightweight: <50 MB APK/IPA for premium experience
  • Battery Efficient: <5% per hour on standby, <15% during active use
  • Network Smart: Works offline, syncs when connected
  • Responsive: <500ms latency for all operations
  • Smooth Experience: Zero jank on modern and legacy devices

Resources & Learning

Official Documentation

Community Resources

  • Flutter Performance Monitoring
  • Google Performance Best Practices
  • Platform-Specific Optimization Guides

Next Steps

  1. Review SKILL.md for detailed implementations
  2. Measure current performance using DevTools
  3. Identify bottlenecks with profiling
  4. Implement optimizations from high to low priority
  5. Validate improvements with benchmarks
  6. Monitor production with analytics
  7. Iterate based on user feedback

Support & Contributions

For detailed code examples and implementations, refer to SKILL.md which contains:

  • 50+ code examples
  • Implementation patterns
  • Configuration templates
  • Best practices checklist
  • Performance metrics reference

Last Updated: 2025-11-18 Version: 1.0.0 Status: Ready for Production