optimization-techniques
Performance optimization strategies. Use ONLY after profiling identifies bottlenecks.
When & Why to Use This Skill
This Claude skill provides expert performance optimization strategies specifically tailored for Go (Golang) development. It focuses on data-driven code improvements, guiding developers to eliminate bottlenecks identified through profiling. By implementing high-efficiency patterns such as slice pre-allocation, optimized string manipulation, and object pooling, this skill helps minimize memory allocations and maximize application throughput while maintaining code readability.
Use Cases
- Scenario 1: Reducing Garbage Collection (GC) pressure in high-concurrency Go services by implementing sync.Pool for reusable objects.
- Scenario 2: Optimizing data processing pipelines by pre-allocating slice capacities to prevent expensive memory re-allocations during execution.
- Scenario 3: Enhancing the performance of string-heavy applications by replacing inefficient concatenation with strings.Builder for faster execution.
- Scenario 4: Conducting post-profiling refactoring sessions to address specific performance bottlenecks without sacrificing the KISS (Keep It Simple, Stupid) principle.
| name | optimization-techniques |
|---|---|
| description | Performance optimization strategies. Use ONLY after profiling identifies bottlenecks. |
Optimization Techniques Skill
Performance optimization strategies for Go. ONLY use after profiling!
When to Use
Use ONLY after profiling identifies actual bottlenecks. NEVER optimize prematurely (KISS principle).
Pre-allocate Slices
// Good - pre-allocate when size known
items := make([]Item, 0, expectedSize)
// Bad - repeated allocations
var items []Item
Use strings.Builder
// Good
var b strings.Builder
for _, s := range strings {
b.WriteString(s)
}
result := b.String()
// Bad - repeated allocations
result := ""
for _, s := range strings {
result += s
}
sync.Pool for Reusable Objects
var bufferPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
buf := bufferPool.Get().(*bytes.Buffer)
defer bufferPool.Put(buf)
buf.Reset()
// use buf
Minimize Allocations
// Good - reuse buffer
buf := make([]byte, 1024)
for {
n, _ := r.Read(buf)
process(buf[:n])
}
// Bad - allocate each time
for {
buf := make([]byte, 1024)
n, _ := r.Read(buf)
process(buf[:n])
}
Golden Rules
- Profile first - Don't guess
- Measure impact - Benchmark before/after
- KISS principle - Simple first, optimize later
- Maintain readability - Don't sacrifice clarity
- Focus on hot paths - 80/20 rule