arcanea-performance-tuning
2
总安装量
2
周安装量
#63746
全站排名
安装命令
npx skills add https://github.com/frankxai/arcanea --skill arcanea-performance-tuning
Agent 安装分布
codex
2
kilo
2
mcpjam
1
zencoder
1
crush
1
cline
1
Skill 文档
The Performance Tuning Codex
“Premature optimization is the root of all evil. But mature optimization is the root of all delight.”
The Performance Philosophy
The Golden Rules
RULE 1: MEASURE FIRST
Don't guess where the bottleneck is.
Profile. Measure. Prove.
RULE 2: OPTIMIZE THE RIGHT THING
80% of time is spent in 20% of code.
Find that 20%.
RULE 3: SET TARGETS
"Faster" is not a goal.
"Under 200ms" is a goal.
RULE 4: REGRESSION PREVENTION
Performance is easy to lose.
Benchmark continuously.
The Optimization Hierarchy
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â OPTIMIZATION HIERARCHY â
â (Optimize in this order) â
â ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ£
â â
â 1. ALGORITHM â O(n²) â O(n log n) = massive wins â
â 2. DATA STRUCTURE â Right structure for access pattern â
â 3. I/O â Network, disk, database calls â
â 4. MEMORY â Allocation, garbage collection â
â 5. CPU â Hot loops, cache efficiency â
â â
â (Don't optimize #5 if #1-4 are the problem) â
â â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Profiling
Types of Profiling
CPU PROFILING:
⢠What functions take the most time?
⢠Where are the hot paths?
⢠What's the call graph?
MEMORY PROFILING:
⢠Where is memory allocated?
⢠What's causing garbage collection?
⢠Are there memory leaks?
I/O PROFILING:
⢠What queries are slow?
⢠What network calls are made?
⢠What files are accessed?
TRACE PROFILING:
⢠What's the full request lifecycle?
⢠Where do requests spend time?
⢠What's the concurrency pattern?
The Profiling Process
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â THE PROFILING CYCLE â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â â
â 1. ESTABLISH BASELINE â
â Measure current performance â
â Record metrics: latency, throughput, resource usage â
â â
â 2. SET TARGET â
â Define acceptable performance â
â "P95 latency < 200ms" â
â â
â 3. PROFILE â
â Identify bottlenecks â
â Focus on top 3 issues â
â â
â 4. HYPOTHESIZE â
â Why is this slow? â
â What would make it faster? â
â â
â 5. OPTIMIZE â
â Make ONE change â
â Keep it isolated â
â â
â 6. MEASURE â
â Did it help? â
â Did it hurt anything else? â
â â
â 7. REPEAT â
â Until target reached â
â â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Common Performance Patterns
The N+1 Query Problem
BAD: N+1 queries
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â // Get all users (1 query) â
â users = db.query("SELECT * FROM users") â
â â
â // For each user, get their orders (N queries) â
â for user in users: â
â orders = db.query("SELECT * FROM orders WHERE user_id=?")â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
GOOD: Eager loading
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â // Single query with JOIN â
â SELECT users.*, orders.* â
â FROM users â
â LEFT JOIN orders ON orders.user_id = users.id â
â â
â // Or batch loading â
â SELECT * FROM orders WHERE user_id IN (1, 2, 3, 4, 5) â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Caching Strategies
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â CACHING STRATEGIES â
â ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ£
â â
â CACHE-ASIDE (Lazy Loading) â
â âââââââââââ â
â â Request ââââ¬âââ¶ Cache Hit âââ¶ Return â
â âââââââââââ â â
â ââââ¶ Cache Miss âââ¶ DB âââ¶ Cache âââ¶ Return â
â â
â WRITE-THROUGH â
â âââââââââââ â
â â Write ââââ¶ Cache âââ¶ DB âââ¶ Confirm â
â âââââââââââ â
â â
â WRITE-BEHIND (Async) â
â âââââââââââ â
â â Write ââââ¶ Cache âââ¶ Confirm â
â âââââââââââ â â
â ââââ¶ [Later] âââ¶ DB â
â â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
CACHE INVALIDATION:
⢠TTL (Time To Live) - Simple but may serve stale data
⢠Event-based - Invalidate on writes
⢠Tag-based - Group related items
Connection Pooling
WITHOUT POOLING:
ââââââââââââ ââââââââââââ
â Request ââââ¶âââ Connect ââââ¶ 50-100ms overhead
ââââââââââââ ââââââââââââ
WITH POOLING:
ââââââââââââ ââââââââââââââââ ââââââââââââ
â Request ââââ¶âââ Pool Manager ââââ¶âââ Reuse ââââ¶ ~0ms
ââââââââââââ ââââââââââââââââ ââââââââââââ
POOL CONFIGURATION:
⢠Min connections: Keep warm for base load
⢠Max connections: Limit to prevent exhaustion
⢠Idle timeout: Release unused connections
⢠Connection lifetime: Prevent stale connections
Lazy Loading
EAGER (Load everything):
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â class User: â
â def __init__(self, id): â
â self.profile = load_profile(id) # Always â
â self.orders = load_orders(id) # Always â
â self.preferences = load_prefs(id) # Always â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
LAZY (Load on demand):
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â class User: â
â def __init__(self, id): â
â self._id = id â
â self._orders = None â
â â
â @property â
â def orders(self): â
â if self._orders is None: â
â self._orders = load_orders(self._id) â
â return self._orders â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Database Optimization
Index Optimization
WHEN TO INDEX:
â Columns in WHERE clauses
â Columns in JOIN conditions
â Columns in ORDER BY
â Columns with high selectivity
WHEN NOT TO INDEX:
â Small tables (full scan is faster)
â Columns with low selectivity (gender, boolean)
â Tables with heavy writes (index maintenance cost)
â Columns rarely queried
COMPOSITE INDEX ORDER:
⢠Equality conditions first
⢠Range conditions last
⢠Most selective first
INDEX (status, created_at) -- status = 'active' AND created_at > ?
Query Optimization
EXPLAIN ANALYZE:
Always explain before optimizing.
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â EXPLAIN ANALYZE â
â SELECT * FROM orders â
â WHERE user_id = 123 AND status = 'pending' â
â ORDER BY created_at DESC â
â LIMIT 10; â
â â
â Look for: â
â ⢠Seq Scan (bad on large tables) â
â ⢠Index Scan (good) â
â ⢠Sort (expensive if not indexed) â
â ⢠Rows vs estimated rows (accuracy of stats) â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
COMMON FIXES:
⢠Add missing indexes
⢠Rewrite subqueries as JOINs
⢠Use LIMIT for pagination
⢠Avoid SELECT * in production
⢠Partition large tables
Frontend Performance
Critical Rendering Path
âââââââââââ âââââââââââ âââââââââââ âââââââââââ
â HTML ââââ¶âââ CSS ââââ¶âââ JS ââââ¶âââ Render â
â Parse â â Parse â â Execute â â Paint â
âââââââââââ âââââââââââ âââââââââââ âââââââââââ
â â â
â¼ â¼ â¼
DOM CSSOM Execute
Build Build & Modify
OPTIMIZATION:
1. Minimize critical resources
2. Minimize critical bytes
3. Minimize critical path length
Core Web Vitals
LCP (Largest Contentful Paint):
Target: < 2.5s
⢠Optimize images
⢠Preload critical resources
⢠Use CDN
FID (First Input Delay):
Target: < 100ms
⢠Break up long tasks
⢠Defer non-critical JS
⢠Use web workers
CLS (Cumulative Layout Shift):
Target: < 0.1
⢠Set image dimensions
⢠Reserve space for ads
⢠Avoid inserting content above fold
Bundle Optimization
CODE SPLITTING:
// Instead of one large bundle
import { everything } from 'huge-library';
// Load on demand
const HeavyComponent = lazy(() => import('./HeavyComponent'));
TREE SHAKING:
// Bad: imports everything
import _ from 'lodash';
// Good: imports only what's used
import { debounce } from 'lodash-es';
COMPRESSION:
⢠Gzip: 70-90% reduction
⢠Brotli: 15-20% better than Gzip
⢠Enable on server and CDN
Concurrency & Parallelism
Async Patterns
SEQUENTIAL (Slow):
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â result1 = await fetchUser() // 100ms â
â result2 = await fetchOrders() // 150ms â
â result3 = await fetchProducts() // 120ms â
â // Total: 370ms â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
PARALLEL (Fast):
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â [user, orders, products] = await Promise.all([ â
â fetchUser(), â
â fetchOrders(), â
â fetchProducts() â
â ]) â
â // Total: 150ms (slowest call) â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Rate Limiting & Backpressure
RATE LIMITING:
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â Token Bucket Algorithm: â
â â
â ⢠Bucket has capacity (e.g., 100 tokens) â
â ⢠Tokens added at fixed rate (e.g., 10/second) â
â ⢠Each request consumes a token â
â ⢠No tokens = request rejected â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
BACKPRESSURE:
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â When producer is faster than consumer: â
â â
â Options: â
â ⢠Drop: Discard excess (lossy) â
â ⢠Buffer: Queue until processed (memory risk) â
â ⢠Sample: Process every Nth item â
â ⢠Slow down: Signal producer to wait â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Monitoring & Metrics
Key Metrics
THE FOUR GOLDEN SIGNALS:
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â 1. LATENCY â Time to serve a request â
â 2. TRAFFIC â Requests per second â
â 3. ERRORS â Rate of failed requests â
â 4. SATURATION â How "full" the service is â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
PERCENTILES:
⢠P50 (median): Typical experience
⢠P95: Most users' worst experience
⢠P99: Tail latency (important!)
⢠Max: Absolute worst case
Note: Average is misleading.
A few slow requests hide in the average.
Benchmarking
MICRO-BENCHMARKS:
⢠Test specific functions
⢠Isolate from I/O
⢠Run many iterations
⢠Beware of JIT warmup
LOAD TESTING:
⢠Simulate realistic traffic
⢠Measure at various loads
⢠Find the breaking point
⢠Test failure scenarios
TOOLS:
⢠k6, Artillery, Locust (load testing)
⢠wrk, hey (HTTP benchmarking)
⢠hyperfine (CLI benchmarking)
Quick Reference
Performance Checklist
â¡ Profiled to find actual bottlenecks
â¡ Set measurable performance targets
â¡ Optimized hot paths first
â¡ Added appropriate caching
â¡ Minimized I/O operations
â¡ Used connection pooling
â¡ Indexed frequently queried columns
â¡ Implemented lazy loading where appropriate
â¡ Set up performance monitoring
â¡ Established performance regression tests
Common Performance Wins
| Problem | Solution |
|----------------------|-----------------------------|
| N+1 queries | Eager loading, batch |
| Slow queries | Add indexes, optimize SQL |
| Large payloads | Pagination, compression |
| Repeated computation | Caching, memoization |
| Synchronous waits | Async, parallel execution |
| Cold starts | Warmup, connection pools |
| Large bundles | Code splitting, tree shake |
| Slow images | Lazy load, WebP, CDN |
The Performance Mantras
"Measure first, optimize second"
"The fastest code is code that doesn't run"
"Cache invalidation is hard; TTL is your friend"
"Profile in production, not just development"
"Optimize for the common case"
“Performance is not about making things fast. It’s about removing what makes things slow.”