All skills
cxuu avatar

/go-performance

@91f0c2e
by Charles Xucxuu/golang-skills165 stars
19

Use when optimizing Go code, investigating slow performance, or writing performance-critical sections. Also use when a user mentions slow Go code, string concatenation in loops, or asks about benchmarking, even if the user doesn't explicitly mention performance patterns. Does not cover concurrent performance patterns (see go-concurrency).

Use this Skill: https://skilld.dev/gh/cxuu/golang-skills/go-performance

This session only. Nothing lands on disk.

referencesSTRING-OPTIMIZATION.md

≈806 tokens on demand. Your agent reads this file only when SKILL.md points to it.

String Optimization Patterns

strconv vs fmt

When converting primitives to/from strings, strconv is faster than fmt because fmt uses reflection and handles arbitrary types.

Benchmark snippets use b.Loop(), available in Go 1.24 and newer. For older Go versions, use for i := 0; i < b.N; i++.

Bad:

for b.Loop() {
    s := fmt.Sprint(rand.Int())
}

Good:

for b.Loop() {
    s := strconv.Itoa(rand.Int())
}

Benchmark comparison:

Approach Speed Allocations
fmt.Sprint 143 ns/op 2 allocs/op
strconv.Itoa 64.2 ns/op 1 allocs/op

Common conversions:

Task fmt strconv
Int → string fmt.Sprint(n) strconv.Itoa(n)
Int64 → string fmt.Sprint(n) strconv.FormatInt(n, 10)
Float → string fmt.Sprint(f) strconv.FormatFloat(f, 'f', -1, 64)
String → int — strconv.Atoi(s)
Bool → string fmt.Sprint(b) strconv.FormatBool(b)

Repeated String-to-Byte Conversions

Do not create byte slices from a fixed string repeatedly. Instead, perform the conversion once and capture the result.

Bad:

for b.Loop() {
    w.Write([]byte("Hello world"))
}

Good:

data := []byte("Hello world")
for b.Loop() {
    w.Write(data)
}

Benchmark comparison:

Approach Speed
Repeated conversion 22.2 ns/op
Single conversion 3.25 ns/op

The good version is ~7x faster because it avoids allocating a new byte slice on each iteration.


String Concatenation

Choose the right string building strategy based on complexity.

Use + for Simple Cases

key := "projectid: " + p

The + operator is efficient for a small, fixed number of strings. The compiler can often optimize adjacent string literals.

Use fmt.Sprintf for Formatting

// Good: clear formatting
str := fmt.Sprintf("%s [%s:%d]-> %s", src, qos, mtu, dst)

// Bad: + with manual conversions
str := src.String() + " [" + qos.String() + ":" + strconv.Itoa(mtu) + "]-> " + dst.String()

When writing to an io.Writer, use fmt.Fprintf directly instead of building a temporary string with fmt.Sprintf.

Use strings.Builder for Piecemeal Construction

strings.Builder takes amortized linear time, whereas repeated + or fmt.Sprintf take quadratic time when building a large string:

b := new(strings.Builder)
for i, d := range digitsOfPi {
    fmt.Fprintf(b, "the %d digit of pi is: %d\n", i, d)
}
str := b.String()

Use Backticks for Constant Multi-line Strings

// Good: raw string literal
usage := `Usage:

custom_tool [args]`

// Bad: concatenation with escape sequences
usage := "" +
    "Usage:\n" +
    "\n" +
    "custom_tool [args]"

Strategy Summary

Method Best For Performance
+ Few strings, simple concat O(n) for small n
fmt.Sprintf Formatted output Slower, but clearer
strings.Builder Loop/piecemeal construction Amortized O(n)
strings.Join Joining a slice O(n)
Backtick literal Constant multi-line text Zero cost

Source: SKILL.md on GitHub

No alerts17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    The skill provides safe documentation and a utility helper script for Go performance optimization, profiling, and benchmark comparisons with no security issues detected.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer6mo

    1 file scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 91f0c2e. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 3 months ago
What it can do
Runs commands
All 1 allowed tools
Bash(bash:*)

README badge

README badge for cxuu/golang-skills/go-performance