# Performance Optimization

Apply systematic performance optimization techniques when writing or reviewing code.

- Repository: https://github.com/ynulihao/AgentSkillOS (618 stars)
- Category: Writing
- License: see repository
- Last updated: 2026-03-07
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add ynulihao/AgentSkillOS --skill performance-optimization -g -a claude-code -y`
Codex: `npx skills add ynulihao/AgentSkillOS --skill performance-optimization -g -a codex -y`
Cursor: `npx skills add ynulihao/AgentSkillOS --skill performance-optimization -g -a cursor -y`
Gemini CLI: `npx skills add ynulihao/AgentSkillOS --skill performance-optimization -g -a gemini-cli -y`

## Description

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.

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# Performance Optimization Skill

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.

## Core Philosophy

1. **Measure First**: Never optimize without profiling data
2. **Estimate Costs**: Back-of-envelope calculations before implementation
3. **Avoid Work**: The fastest code is code that doesn't run
4. **Reduce Allocations**: Memory allocation is often the hidden bottleneck
5. **Cache Locality**: Memory access patterns dominate modern performance

## Reference Latency Numbers

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 |

## Optimization Techniques (Priority Order)

### 1. Algorithmic Improvements (Highest Impact)

**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:**
- Replace nested loops with hash lookups
- Use sorted-list intersection → hash table lookups
- Process in reverse post-order to eliminate per-element checks
- Replace interval trees (O(log N)) with hash maps (O(1)) when ranges aren't needed

### 2. Reduce Memory Allocations

**Allocation is expensive (~25-100ns + GC pressure)**

```python
# 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
```

The rest: https://github.com/ynulihao/AgentSkillOS/blob/HEAD/data/skill_seeds/performance-optimization/SKILL.md
