name: performance description: Identify and improve performance and scalability.
Performance Skill
Use this skill to evaluate and enhance the performance and scalability of the codebase. Follow these guidelines:
Measurement
- Profile first – Use profiling tools to measure CPU, memory and I/O usage. Benchmark with realistic workloads to identify hot spots.
- Establish baselines – Record current performance metrics (latency, throughput, resource usage) before optimising. Compare against targets.
Complexity & Algorithms
- Assess the algorithmic complexity of functions. Replace inefficient algorithms (e.g. O(n²) loops) with more scalable alternatives.
- Choose appropriate data structures for the task (e.g. hash maps for lookups, queues for FIFO processing).
Concurrency & Asynchrony
- Utilise concurrency primitives (threads, async/await, goroutines) where they improve throughput. Avoid blocking calls on the main thread.
- Ensure shared state is protected with locks or other synchronisation mechanisms. Prevent race conditions and deadlocks.
Resource Management
- Manage memory, file handles and network connections carefully. Allocate and release resources promptly (e.g. use context managers in Python or
usingstatements in C#). - Avoid unnecessary memory copies and large intermediate allocations.
Caching & Memoization
- Cache expensive computations or remote calls where appropriate. Use proper invalidation strategies (TTL, LRU) to avoid stale data.
- Prefer immutable data where possible to simplify caching and concurrency.
I/O Efficiency
- Batch operations to reduce overhead (e.g. bulk database writes, batch API requests). Use streaming for large files or payloads.
- Minimise network round trips. Use connection pooling for database and HTTP clients.
Scalability
- Design services to scale horizontally: keep them stateless where possible and externalise state to databases or caches.
- Implement back‑pressure mechanisms (queues, circuit breakers, rate limiting) to handle overload gracefully.
Observability
- Collect metrics, traces and logs to monitor performance in production. Dashboards and alerts help detect regressions early.
- Add instrumentation at key points (e.g. request start/end, database queries) to measure latency and throughput.
When suggesting optimisations, balance performance gains against code readability and maintainability. Document any trade‑offs or additional complexity introduced by optimisations.