Measure first. Optimize the bottleneck that matters. Keep correctness tests around every performance change.
For advanced patterns such as NumPy vectorization, caching, memory management,
parallelism, async I/O, database optimization, and benchmarking tools, read
references/advanced-patterns.md.
Intake
Collect:
- workload and input size;
- target metric: latency, throughput, memory, CPU, I/O;
- current measurement and desired target;
- environment and hardware;
- correctness tests or golden outputs.
Profiling Order
- Reproduce the slow path with a realistic input.
- Add a simple wall-clock measurement.
- Use the right profiler:
cProfile/py-spyfor CPU;line_profilerfor line-level hot spots;memory_profiler/tracemallocfor memory;- application metrics for production latency.
- Identify the smallest hot path worth changing.
- Optimize one bottleneck.
- Re-measure and verify correctness.
Common Fix Classes
- Better algorithm or data structure.
- Avoid repeated work with caching.
- Batch I/O or database calls.
- Use generators/streaming for memory.
- Vectorize numerical work with NumPy/Pandas/JAX/PyTorch when appropriate.
- Move true CPU-bound parallelism to multiprocessing/native libraries.
Rules
- Do not optimize without a baseline measurement.
- Do not trade correctness for speed silently.
- Do not optimize cold code.
- Prefer algorithmic wins over micro-optimizations.
- Record before/after numbers and input sizes.
Output
Return:
- performance target;
- measurement method;
- bottleneck evidence;
- optimization plan ranked by expected impact;
- correctness guard;
- before/after reporting template.