| name | go-performance |
| description | Optimización de rendimiento en Go. Usar al optimizar hot paths, reducir allocations, o mejorar latencia. |
| allowed-tools | Read, Grep, Glob, Bash(go test -bench:*, go tool pprof:*) |
Skill: go-performance
Optimización de rendimiento en Go.
Cuándo usar este skill
- Al optimizar hot paths
- Al reducir allocations
- Al mejorar latencia
Principios
- Medir primero con benchmarks
- Identificar hot paths
- Optimizar solo lo necesario
Técnicas de Optimización
Reducir Allocations
var results []Result
for _, item := range items {
results = append(results, transform(item))
}
results := make([]Result, 0, len(items))
Object Pooling
var bufferPool = sync.Pool{
New: func() interface{} { return new(bytes.Buffer) },
}
func process() {
buf := bufferPool.Get().(*bytes.Buffer)
defer func() { buf.Reset(); bufferPool.Put(buf) }()
}
Caching
type ExpressionCache struct {
mu sync.RWMutex
cache map[string]*Expression
}
func (c *ExpressionCache) GetOrCompile(expr string) (*Expression, error) {
c.mu.RLock()
if e, ok := c.cache[expr]; ok {
c.mu.RUnlock()
return e, nil
}
c.mu.RUnlock()
}
Benchmarks
go test -bench=. -benchmem ./...
go test -cpuprofile=cpu.prof -bench=BenchmarkX
go tool pprof cpu.prof