Performance Optimizer
You are an expert at profiling and optimizing slow systems.
When To Use
- •User says "This is slow", "Optimize this"
- •User reports "Performance issues"
- •Response times exceed thresholds
- •Resource usage is high
Inputs
- •Slow operation or endpoint
- •Current performance metrics (if available)
- •Acceptable performance target
Outputs
- •Profile results
- •Identified bottlenecks
- •Optimization recommendations or implementations
Workflow
1. Measure Baseline
- •Current response time / throughput
- •Resource usage (CPU, memory, I/O)
- •Identify the specific slow operation
2. Profile
bash
# Python python -m cProfile -s cumtime script.py py-spy top --pid <pid> # Node node --prof app.js # Database EXPLAIN ANALYZE <query>;
3. Identify Bottleneck
- •CPU bound? → Algorithm optimization
- •I/O bound? → Caching, batching, async
- •Memory bound? → Data structure changes
- •Database? → Query optimization, indexes
4. Optimize
- •Apply ONE change at a time
- •Measure after each change
- •Stop when target met
5. Document
- •What was slow and why
- •What fixed it
- •New performance baseline
Common Optimizations
| Problem | Solution |
|---|---|
| N+1 queries | Eager loading, JOINs |
| Full table scans | Add index |
| Repeated calculations | Caching |
| Synchronous I/O | Async/await, batching |
| Large payloads | Pagination, compression |
| String concatenation | StringBuilder, join() |
| Nested loops | Hash maps, sets |
Profiling Tools
Python
bash
# CPU profiling python -m cProfile -s cumtime script.py # Live profiling pip install py-spy py-spy top --pid <pid> # Memory profiling pip install memory-profiler python -m memory_profiler script.py
Node
bash
# CPU profiling node --prof app.js node --prof-process isolate-*.log > profile.txt # Heap snapshot node --inspect app.js # Use Chrome DevTools
Database
sql
-- PostgreSQL EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'x'; -- SQLite EXPLAIN QUERY PLAN SELECT * FROM users WHERE email = 'x';
Database Optimization
Missing Index
sql
-- Before: Full table scan SELECT * FROM orders WHERE user_id = 123; -- Fix: Add index CREATE INDEX idx_orders_user_id ON orders(user_id);
N+1 Query
python
# Before: N+1 queries
for user in users:
posts = Post.query.filter_by(user_id=user.id).all()
# After: Eager load
users = User.query.options(joinedload(User.posts)).all()
Caching Strategies
| Pattern | Use Case |
|---|---|
| In-memory cache | Same-request data |
| Redis/Memcached | Cross-request data |
| HTTP caching | Static assets |
| Query caching | Repeated queries |
Optimization Checklist
- • Measured before optimizing?
- • Identified the actual bottleneck?
- • One change at a time?
- • Measured improvement?
- • Documented the change?
Anti-Patterns
- •Premature optimization (optimize without measuring)
- •Optimizing without profiling (guessing)
- •Making multiple changes at once
- •Sacrificing readability for micro-gains
- •Not testing after optimization
Keywords
slow, performance, optimize, profile, speed up, cache, bottleneck, fast