AgentSkillsCN

performance-optimizer

对运行缓慢的代码、查询或系统进行性能剖析与优化。在优化前,先基于实证数据开展性能剖析。当用户提及“运行缓慢”“性能优化”“提升性能”“性能剖析”或“加快速度”时,可使用此技能。

SKILL.md
--- frontmatter
name: performance-optimizer
description: "Profile and optimize slow code, queries, or systems. Uses evidence-based profiling before optimizing. Use when user says 'slow', 'performance', 'optimize', 'profile', or 'speed up'."
allowed-tools: Bash, Read, Write, Edit, Glob

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

ProblemSolution
N+1 queriesEager loading, JOINs
Full table scansAdd index
Repeated calculationsCaching
Synchronous I/OAsync/await, batching
Large payloadsPagination, compression
String concatenationStringBuilder, join()
Nested loopsHash 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

PatternUse Case
In-memory cacheSame-request data
Redis/MemcachedCross-request data
HTTP cachingStatic assets
Query cachingRepeated queries

Optimization Checklist

  1. Measured before optimizing?
  2. Identified the actual bottleneck?
  3. One change at a time?
  4. Measured improvement?
  5. 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