# 🏎️ Performance Standards (The "Turbo" Protocol) **Audience:** Linus Torvalds (The Kernel) & The Architect. **Objective:** Speed is a Feature. Latency is a Bug. > [!CRITICAL] > **The Linus Mandate:** > "I don't care if it's 'clean code'. If it takes 2 seconds to load a dropdown, it's garbage. Optimize it or delete it." ## 1. ⏱️ Latency Budgets (Hard Limits) Every endpoint/function falls into a Tier: * **Tier 1 (Interactive):** `< 100ms` (UI interactions, Auto-complete). * **Tier 2 (Standard):** `< 500ms` (Form submissions, Page loads). * **Tier 3 (Batch):** `< 5s` (Reports, Complex calculations). * *Rule:* If it takes > 5s, it MUST be Async (Background Job). ## 2. 🗄️ Database & IO (The N+1 Killer) * **No Loop Queries:** NEVER execute a SQL query inside a `for` loop. * **❌ BAD:** `for user in users: db.get_profile(user.id)` * **✅ GOOD:** `db.get_profiles(user_ids)` (Batch fetching). * **Select *:** Explicitly select columns. Do not drag 5MB of JSONB if you only need the `id`. ## 3. 💾 Memory & Resources * **Generators > Lists:** Use `yield` for processing large datasets. Do not load 1GB CSVs into RAM. * **Container Limits:** Respect the `512MB` limit set in `business_standards.md`. * *Leak Detection:* If memory grows linearly over 1 hour, the container must be killed and investigated. ## 4. ⚡ Caching Strategy * **Read-Heavy Data:** Must be cached (Redis/Memcached) with a TTL. * **Cache Stampede:** Implement "Stale-While-Revalidate" patterns. Do not let 1000 users hit the DB simultaneously when cache expires. ## 5. 🐧 The Kernel's Optimization Checklist Before merging, run the profiler: - [ ] **Complexity:** Is this algorithm O(n) or O(n^2)? (Nested loops over large data = Reject). - [ ] **IO:** Count the DB calls. Is it 1 or 100? - [ ] **Payload:** Check the JSON response size. Is it > 100KB? (Gzip it or paginate it). - [ ] **Async:** Are we blocking the Event Loop (Node/Python Asyncio) with CPU work?