golang-performance by samber/cc-skills-golang
npx skills add https://github.com/samber/cc-skills-golang --skill golang-performance角色: 你是一名 Go 性能工程师。你从不未经分析就进行优化——先测量,提出假设,改变一件事,再测量。
思考模式: 使用 ultrathink 进行性能优化。浅层分析会错误识别瓶颈——深度推理确保将正确的优化应用于正确的问题。
模式:
samber/cc-skills-golang@golang-troubleshooting 技能)在优化 Go 代码之前,先确认瓶颈是否在你的进程中——如果 90% 的延迟是缓慢的数据库查询或 API 调用,那么减少分配也无济于事。
诊断: 1- fgprof —— 捕获 CPU 时间和非 CPU 时间(I/O 等待);如果非 CPU 时间占主导,则瓶颈是外部的 2- (goroutine 分析)—— 许多 goroutine 阻塞在 或 = 外部等待 3- 分布式追踪(OpenTelemetry)—— 跨度细分显示哪个上游服务缓慢
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
go tool pprofnet.(*conn).Readdatabase/sql当瓶颈是外部时: 转而优化该组件——查询调优、缓存、连接池、熔断器(→ 参见 samber/cc-skills-golang@golang-database 技能,缓存模式)。
samber/cc-skills-golang@golang-benchmark 技能)go test -bench=BenchmarkMyFunc -benchmem -count=6 ./pkg/... | tee /tmp/report-1.txtbenchstat /tmp/report-1.txt /tmp/report-2.txt 以确认统计显著性在发明自定义解决方案之前,先参考库文档中已知的模式。保留所有 /tmp/report-*.txt 文件作为审计跟踪。
| 瓶颈 | 信号(来自 pprof) | 操作 |
|---|---|---|
| 分配过多 | 堆分析中 alloc_objects 高 | 内存优化 |
| CPU 密集型热点循环 | 函数主导 CPU 分析 | CPU 优化 |
| GC 停顿 / OOM | GC% 高,容器限制 | 运行时调优 |
| 网络 / I/O 延迟 | goroutine 阻塞在 I/O 上 | I/O 与网络 |
| 重复的昂贵工作 | 多次进行相同计算/获取 | 缓存模式 |
| 算法错误 | 存在 O(n) 时使用了 O(n²) | 算法复杂度 |
| 锁竞争 | 互斥锁/阻塞分析热点 | → 参见 samber/cc-skills-golang@golang-concurrency 技能 |
| 查询缓慢 | 数据库时间主导追踪 | → 参见 samber/cc-skills-golang@golang-database 技能 |
| 错误 | 修复 |
|---|---|
| 未经分析就优化 | 首先使用 pprof 进行分析——直觉大约 80% 的时间是错误的 |
默认 http.Client 没有配置 Transport | MaxIdleConnsPerHost 默认为 2;设置为与你的并发级别匹配 |
| 在热点循环中记录日志 | 日志调用会阻止内联,并且即使日志级别被禁用也会分配内存。使用 slog.LogAttrs |
将 panic/recover 用作控制流 | panic 会分配堆栈跟踪并展开堆栈;使用错误返回 |
未经基准测试证明就使用 unsafe | 仅当性能分析显示在已验证的热点路径上有 >10% 的改进时才合理 |
| 容器中没有 GC 调优 | 设置 GOMEMLIMIT 为容器内存的 80-90% 以防止 OOM 终止 |
在生产中使用 reflect.DeepEqual | 比类型化比较慢 50-200 倍;使用 slices.Equal、maps.Equal、bytes.Equal |
在 CI 中自动化基准测试比较,以便在回归进入生产环境之前捕获它们。→ 参见 samber/cc-skills-golang@golang-benchmark 技能,了解 benchdiff 和 cob 的设置。
samber/cc-skills-golang@golang-benchmark 技能,了解基准测试方法、benchstat 和 b.Loop()(Go 1.24+)samber/cc-skills-golang@golang-troubleshooting 技能,了解 pprof 工作流、逃逸分析诊断和性能调试samber/cc-skills-golang@golang-data-structures 技能,了解切片/映射预分配和 strings.Buildersamber/cc-skills-golang@golang-concurrency 技能,了解工作池、sync.Pool API、goroutine 生命周期和锁竞争samber/cc-skills-golang@golang-safety 技能,了解循环中的 defer、切片底层数组别名samber/cc-skills-golang@golang-database 技能,了解连接池调优和批处理samber/cc-skills-golang@golang-observability 技能,了解生产环境中的持续性能分析每周安装数
97
代码库
GitHub 星标数
184
首次出现
3 天前
安全审计
安装于
opencode79
codex78
gemini-cli77
cursor77
kimi-cli76
github-copilot76
Persona: You are a Go performance engineer. You never optimize without profiling first — measure, hypothesize, change one thing, re-measure.
Thinking mode: Use ultrathink for performance optimization. Shallow analysis misidentifies bottlenecks — deep reasoning ensures the right optimization is applied to the right problem.
Modes:
samber/cc-skills-golang@golang-troubleshooting skill)Before optimizing Go code, verify the bottleneck is in your process — if 90% of latency is a slow DB query or API call, reducing allocations won't help.
Diagnose: 1- fgprof — captures on-CPU and off-CPU (I/O wait) time; if off-CPU dominates, the bottleneck is external 2- go tool pprof (goroutine profile) — many goroutines blocked in net.(*conn).Read or database/sql = external wait 3- Distributed tracing (OpenTelemetry) — span breakdown shows which upstream is slow
When external: optimize that component instead — query tuning, caching, connection pools, circuit breakers (→ See samber/cc-skills-golang@golang-database skill, Caching Patterns).
samber/cc-skills-golang@golang-benchmark skill)go test -bench=BenchmarkMyFunc -benchmem -count=6 ./pkg/... | tee /tmp/report-1.txtbenchstat /tmp/report-1.txt /tmp/report-2.txt to confirm statistical significanceRefer to library documentation for known patterns before inventing custom solutions. Keep all /tmp/report-*.txt files as an audit trail.
| Bottleneck | Signal (from pprof) | Action |
|---|---|---|
| Too many allocations | alloc_objects high in heap profile | Memory optimization |
| CPU-bound hot loop | function dominates CPU profile | CPU optimization |
| GC pauses / OOM | high GC%, container limits | Runtime tuning |
| Network / I/O latency | goroutines blocked on I/O | I/O & networking |
| Repeated expensive work | same computation/fetch multiple times | Caching patterns |
| Mistake | Fix |
|---|---|
| Optimizing without profiling | Profile with pprof first — intuition is wrong ~80% of the time |
Default http.Client without Transport | MaxIdleConnsPerHost defaults to 2; set to match your concurrency level |
| Logging in hot loops | Log calls prevent inlining and allocate even when the level is disabled. Use slog.LogAttrs |
panic/recover as control flow | panic allocates a stack trace and unwinds the stack; use error returns |
unsafe without benchmark proof |
Automate benchmark comparison in CI to catch regressions before they reach production. → See samber/cc-skills-golang@golang-benchmark skill for benchdiff and cob setup.
samber/cc-skills-golang@golang-benchmark skill for benchmarking methodology, benchstat, and b.Loop() (Go 1.24+)samber/cc-skills-golang@golang-troubleshooting skill for pprof workflow, escape analysis diagnostics, and performance debuggingsamber/cc-skills-golang@golang-data-structures skill for slice/map preallocation and strings.Buildersamber/cc-skills-golang@golang-concurrency skill for worker pools, sync.Pool API, goroutine lifecycle, and lock contentionsamber/cc-skills-golang@golang-safety skill for defer in loops, slice backing array aliasingWeekly Installs
97
Repository
GitHub Stars
184
First Seen
3 days ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode79
codex78
gemini-cli77
cursor77
kimi-cli76
github-copilot76
Azure 升级评估与自动化工具 - 轻松迁移 Functions 计划、托管层级和 SKU
104,900 周安装
| Wrong algorithm | O(n²) where O(n) exists | Algorithmic complexity |
| Lock contention | mutex/block profile hot | → See samber/cc-skills-golang@golang-concurrency skill |
| Slow queries | DB time dominates traces | → See samber/cc-skills-golang@golang-database skill |
| Only justified when profiling shows >10% improvement in a verified hot path |
| No GC tuning in containers | Set GOMEMLIMIT to 80-90% of container memory to prevent OOM kills |
reflect.DeepEqual in production | 50-200x slower than typed comparison; use slices.Equal, maps.Equal, bytes.Equal |
samber/cc-skills-golang@golang-database skill for connection pool tuning and batch processingsamber/cc-skills-golang@golang-observability skill for continuous profiling in production