agentleFS
els0rels0r/skills/skills/go-performance/SKILL.md

go-performance

Skill1 starsChanged 5 months agoMITView on GitHub

Go performance analysis: memory allocation, CPU bottlenecks, concurrency contention, struct alignment, escape analysis, and benchmarking patterns. Use this skill whenever writing performance-sensitive Go code, reviewing Go code for efficiency, discussing allocations or GC pressure, benchmarking, profiling with pprof, or optimizing hot paths. Also trigger when the user mentions sync.Pool, atomic operations, pre-allocation, or escape analysis — even without saying "performance."

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 2 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Always ask the user to validate results — if they look too good, they probably are.

    line 23
  2. Very specific:

    Every service must expose pprof.

    line 33
  3. Very specific:

    Use bytes.Clone(buf.Bytes()) when you need to retain data after the buffer is returned to the pool.

    line 66
  4. Very specific:

    Use the fieldalignment tool to detect suboptimal ordering.

    line 124
  5. Distinctive:

    Don't return pointers to small, short-lived structs.

    line 132

≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Go Performance

Performance is cost in cloud, experience on user-facing paths. Never optimize without measurement.

Workflow

  1. Establish a benchmark baseline (see go-testing for benchmark patterns).
  2. Implement fixes.
  3. Compare with benchstat baseline.txt optimized.txt.
  4. Always ask the user to validate results — if they look too good, they probably are.

Prioritize: hot paths > allocation-heavy code > concurrency contention > stylistic.


Rules

1. Expose pprof

Every service must expose pprof. Import _ "net/http/pprof" and run go http.ListenAndServe(":6060", nil) alongside the app.

EndpointShows
/debug/pprof/profileCPU profile
/debug/pprof/heapLive heap allocations
/debug/pprof/allocsCumulative allocation counts
/debug/pprof/goroutineGoroutine stacks
/debug/pprof/mutexMutex contention
/debug/pprof/blockBlocking operations
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
go tool pprof http://localhost:6060/debug/pprof/heap

2. sync.Pool — Reuse Temporary Buffers

Reuse buffers on high-frequency paths to relieve GC.

var bufPool = sync.Pool{New: func() any { return new(bytes.Buffer) }}

func handleRequest(w http.ResponseWriter, r *http.Request) {
    buf := bufPool.Get().(*bytes.Buffer)
    defer func() { buf.Reset(); bufPool.Put(buf) }()
    buf.ReadFrom(r.Body)
    process(buf.Bytes())
}

NEVER pool strings.Builder when returning b.String() — String() shares the builder's internal buffer (no copy). Resetting the builder corrupts the string the previous caller still holds. Use bytes.Buffer and consume buf.Bytes() before reset.

Use bytes.Clone(buf.Bytes()) when you need to retain data after the buffer is returned to the pool.

3. Pre-allocate Slices and Maps

Known size → pre-allocate. Avoids runtime reallocation and memory copying.

// ❌
var result []int
for _, v := range input {
    result = append(result, v*2)
}

// ✅
result := make([]int, 0, len(input))

4. strings.Builder for Concatenation

Strings are immutable. + in a loop creates a new string per iteration.

var b strings.Builder
b.Grow(expectedTotalLength)
for _, word := range words {
    b.WriteString(word)
}

5. strconv over fmt

fmt uses reflection. For basic type conversions, use strconv.

s := strconv.Itoa(42)       // not fmt.Sprintf("%d", 42)
s := strconv.FormatFloat(f, 'f', 2, 64) // not fmt.Sprintf("%.2f", f)

6. Struct Field Alignment

Order fields largest → smallest to minimize padding.

// ❌ 24 bytes (padding waste)
type User struct {
    IsAdmin bool   // 1 + 7 padding
    ID      int64  // 8
    Age     int8   // 1 + 7 padding
}

// ✅ 16 bytes
type User struct {
    ID      int64  // 8
    IsAdmin bool   // 1
    Age     int8   // 1 + 6 padding
}

Use the fieldalignment tool to detect suboptimal ordering.

7. Prefer strings/bytes over regexp

regexp is safe but slow. Use strings functions for simple matching. If regex is unavoidable, compile once at package level with regexp.MustCompile.

8. Escape Analysis — Keep Allocations on the Stack

Don't return pointers to small, short-lived structs. For slices, use the append pattern — pass a destination slice so the caller manages memory.

// ❌ forces heap allocation every call
func (m *IntMap) Convert() []int {
    result := make([]int, 0, len(m.data))
    for _, v := range m.data {
        result = append(result, v)
    }
    return result
}

// ✅ caller controls allocation
func (m *IntMap) Convert(dst []int) []int {
    for _, v := range m.data {
        dst = append(dst, v)
    }
    return dst
}

9. Concrete Returns over Interface Returns

Accept interfaces, return concrete types. Returning an interface forces a wrapping allocation. (See also go-style interface design rules.)

10. Atomics and RWMutex for Contention

sync.Mutex serializes goroutines. Use sync/atomic for counters, sync.RWMutex for read-heavy data.

// ❌ mutex for a counter
type Counter struct {
    mu sync.Mutex
    v  int64
}

// ✅ atomic
type Counter struct {
    v atomic.Int64
}
func (c *Counter) Add() { c.v.Add(1) }

11. defer on Hot Paths

Go 1.22+ significantly reduced defer overhead. In most code, use defer freely (as go-style recommends). Only consider manual unlock/close on benchmarked hot paths where profiling shows defer as a measurable contributor — this is rare post-1.22.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.