python.io_in_hot_path
Performance
Medium
Detects I/O operations (file access, network calls, database queries) in hot code paths like loops or frequently-called functions.
Why It Matters
Section titled “Why It Matters”I/O operations in hot paths cause:
- Latency spikes — Each I/O call adds milliseconds of delay
- Throughput reduction — Blocking I/O prevents concurrent processing
- Resource exhaustion — Too many concurrent I/O operations overwhelm systems
- Unpredictable performance — I/O latency varies significantly
Example
Section titled “Example”# ❌ Before (I/O in loop)def process_users(user_ids): results = [] for user_id in user_ids: # I/O call in every iteration user = fetch_user_from_db(user_id) results.append(transform(user)) return results# ✅ After (batch I/O)def process_users(user_ids): # Single I/O call for all users users = fetch_users_batch(user_ids) return [transform(user) for user in users]What Unfault Detects
Section titled “What Unfault Detects”- Database queries inside loops
- HTTP requests inside loops
- File reads/writes inside loops
- Network operations in frequently-called functions
- I/O in list comprehensions or generator expressions
Auto-Fix
Section titled “Auto-Fix”Unfault generates patches that suggest batching strategies:
# Batch database queriesusers = User.query.filter(User.id.in_(user_ids)).all()user_map = {u.id: u for u in users}
# Batch HTTP requestsimport asyncioresults = await asyncio.gather(*[fetch(url) for url in urls])Performance Comparison
Section titled “Performance Comparison”| Pattern | 100 items | 1000 items |
|---|---|---|
| I/O in loop | ~500ms | ~5000ms |
| Batched I/O | ~10ms | ~50ms |