Performance optimization dla MVP: nie optymalizuj za wcześnie, ale projektuj tak żeby dało się zoptymalizować. Measure first, optimize later.
Flutter Performance — kluczowe metryki
60 FPS (16ms per frame):
- Cel: smooth animations
- Jank = dropped frames (widoczne lagowanie)
- Measure: Flutter DevTools Performance tab
App size:
- Cel: < 20 MB (Android), < 50 MB (iOS)
- Większy = wolniejszy download, mniej instalacji
Startup time:
- Cel: < 2 sekundy do pierwszego ekranu
- Measure: Flutter DevTools Timeline
Typowe problemy performance
1. Rebuild całego drzewa widgetów
- Problem: setState() rebuilds cały widget tree
- Fix: Użyj const widgets, split na mniejsze widgets
- Fix: State management (Riverpod) — rebuild tylko co potrzebne
2. Expensive operations w build()
- Problem: Sorting, filtering w build() — wykonuje się każdym rebuild
- Fix: Przenieś do state, compute raz
- Fix: Użyj memo/cached values
3. Duże listy bez lazy loading
- Problem: ListView z 1000 items — wszystkie renderowane naraz
- Fix: ListView.builder — lazy loading
- Fix: Pagination — load 20 items at a time
4. Nieoptymalne images
- Problem: 4K image w 100x100 thumbnail
- Fix: Resize images server-side
- Fix: Use cached_network_image package
💡 Zasada optymalizacji
Nie optymalizuj bez mierzenia. Użyj Flutter DevTools Performance tab. Znajdź bottleneck. Optymalizuj. Zmierz ponownie. Repeat.
Firebase Performance
Firestore queries:
- Problem: Fetch 10k documents każdym razem
- Fix: Limit queries (limit(20))
- Fix: Pagination (startAfter)
- Fix: Indexes dla złożonych queries
Real-time listeners:
- Problem: 100 active listeners = 100 connections
- Fix: Unsubscribe gdy widget disposed
- Fix: Użyj single listener + local state
Cloud Functions cold start:
- Problem: Pierwsza invocation = 5-10 sekund
- Fix: Keep functions warm (scheduled ping)
- Fix: Minimize dependencies
App Size Optimization
- Remove unused code: Tree shaking (automatic w release build)
- Compress images: WebP format, optimize PNGs
- Split APKs: Per-ABI builds (arm64, x86)
- Lazy load features: Deferred loading
Network Optimization
- Cache API responses: dio_cache_interceptor
- Compress requests: gzip
- Batch requests: Jeden call zamiast 10
- Prefetch: Load data before user needs it
Tools do mierzenia
Flutter DevTools:
- Performance tab — frame rendering
- Memory tab — memory leaks
- Network tab — API calls
Firebase Performance Monitoring:
- App startup time
- Screen rendering time
- Network requests duration
📝 Zadanie
Otwórz Flutter DevTools Performance tab. Zrób scroll przez długą listę. Znajdź jank (dropped frames). Zoptymalizuj (ListView.builder, const widgets). Zmierz ponownie.