Apply systematic performance optimization techniques when writing or reviewing code. Use when optimizing hot paths, reducing latency, improving throughput, fixing performance regressions, or when the user mentions performance, optimization, speed, latency, throughput, profiling, or benchmarking.
Apply these principles when optimizing code for performance. Focus on the critical 3% where performance truly matters - a 12% improvement is never marginal in engineering.
Use these for back-of-envelope calculations:
| Operation | Latency | |-----------|---------| | L1 cache reference | 0.5 ns | | Branch mispredict | 5 ns | | L2 cache reference | 7 ns | | Mutex lock/unlock | 25 ns | | Main memory reference | 100 ns | | Compress 1KB (Snappy) | 3 us | | SSD random read (4KB) | 20 us | | Round trip in datacenter | 50 us | | Disk seek | 5 ms |
Always check algorithm complexity first:
O(N^2) → O(N log N) = 1000x faster for N=1M
O(N) → O(1) = unbounded improvement
Common patterns:
Allocation is expensive (~25-100ns + GC pressure)
# BAD: Allocates on every call
def process(items):
result = [] # New allocation
for item in items:
result.append(transform(item))
return result
# GOOD: Pre-allocate or reuse
def process(items, out=None):
if out is None:
out = [None] * len(items)
for i, item in enumerate(items):
out[i] = transform(item)
return out
Techniques:
reserve() or known capacityMinimize memory footprint and cache lines touched:
// BAD: 24 bytes due to padding
struct Item {
flag: bool, // 1 byte + 7 padding
value: i64, // 8 bytes
count: i32, // 4 bytes + 4 padding
}
// GOOD: 16 bytes with reordering
struct Item {
value: i64, // 8 bytes
count: i32, // 4 bytes
flag: bool, // 1 byte + 3 padding
}
Techniques:
Map<A, Map<B, C>> → Map<(A,B), C>Optimize the common case without hurting rare cases:
# BAD: Always takes slow path
def parse_varint(data):
return generic_varint_parser(data)
# GOOD: Fast path for common 1-byte case
def parse_varint(data):
if data[0] < 128: # Single byte - 90% of cases
return data[0], 1
return generic_varint_parser(data) # Rare multi-byte
Techniques:
Trade memory for compute when beneficial:
# BAD: Recomputes on every access
def is_vowel(char):
return char.lower() in 'aeiou'
# GOOD: Lookup table
VOWEL_TABLE = [c.lower() in 'aeiou' for c in (chr(i) for i in range(256))]
def is_vowel(char):
return VOWEL_TABLE[ord(char)]
Techniques:
Amortize fixed costs across multiple operations:
# BAD: N round trips
for item in items:
result = db.lookup(item)
# GOOD: 1 round trip
results = db.lookup_many(items)
Design APIs that support:
# BAD: Always computes expensive value
def process(data, config):
expensive = compute_expensive(data) # Always runs
if config.needs_expensive:
use(expensive)
# GOOD: Defer until needed
def process(data, config):
if config.needs_expensive:
expensive = compute_expensive(data) # Only when needed
use(expensive)
Techniques:
Lower-level optimizations when profiling shows need:
// Avoid function call overhead in hot loops
#[inline(always)]
fn hot_function(x: i32) -> i32 { x * 2 }
// Copy to local variable for better alias analysis
fn process(data: &mut [i32], factor: &i32) {
let f = *factor; // Compiler knows this won't change
for x in data {
*x *= f;
}
}
Techniques:
Minimize lock contention and atomic operations:
Before optimizing, estimate:
Operation cost: ___ ns/us/ms
Frequency: ___ times per second/request
Total time: cost × frequency = ___
Improvement target: ___% reduction
Expected new time: ___
Is this worth it? [ ] Yes [ ] No
npx skills add ynulihao/performance-optimization下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer