一键导入
go-struct-optimizer
Analyze and optimize Go struct memory layouts — padding, alignment, GC scan range. Use when reviewing struct field order or optimizing memory.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Analyze and optimize Go struct memory layouts — padding, alignment, GC scan range. Use when reviewing struct field order or optimizing memory.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
Universal agent environment initializer. Creates junctions/symlinks and hardlinks so any supported AI agent can discover workflows, skills, and rules from the canonical .agents/ directory.
Generate high-performance GoNN neural network code following library best practices. Covers Builder and Options API styles, deep network construction, optimizer/scheduler configuration, and AI-Meta doc-comment annotations.
基于 SOC 职业分类
| name | go-struct-optimizer |
| description | Analyze and optimize Go struct memory layouts — padding, alignment, GC scan range. Use when reviewing struct field order or optimizing memory. |
Analyze and optimize Go struct memory layouts: padding, alignment, GC scan range.
Logic inspired by padiazg/go-struct-analyzer and fieldalignment.
| Type | Size | Align | Pointer |
|---|---|---|---|
bool, int8, uint8, byte | 1 | 1 | no |
int16, uint16 | 2 | 2 | no |
int32, uint32, rune, float32 | 4 | 4 | no |
int64, uint64, float64 | 8 | 8 | no |
complex64 | 8 | 4 | no |
complex128 | 16 | 8 | no |
int, uint, uintptr | 8 | 8 | no |
*T, map[K]V, chan T, func(...) | 8 | 8 | pure |
interface{} / any | 16 | 8 | pure |
string | 16 | 8 | mixed (ptr+len) |
[]T | 24 | 8 | mixed (ptr+len+cap) |
*T, map, chan, func, interface{})string, []T)bool)Sort fields by alignment descending, then by size descending, then by name:
alignment DESC → size DESC → name ASC
Sort fields by alignment descending, then by pointer class (pure → mixed → none), with mixed sorted by size ascending (fewer trailing non-ptr words):
alignment DESC → ptr_class (pure < mixed < none) → size (ASC for mixed, DESC otherwise) → name ASC
For most cases, GC-optimal order also reduces padding. Prefer GC-optimal unless struct has no pointers at all (then use size-optimal).
The skill provides a Python analysis script at scripts/analyze.py.
No dependencies required — stdlib only.
# Analyze a single file
python <skill_dir>/scripts/analyze.py <file.go>
# Analyze a directory tree
python <skill_dir>/scripts/analyze.py ./internal/ecs/
# Go-style recursive scan from the current dir (skips *_test.go, vendor,
# testdata, and dot/underscore dirs — same exclusions as `go ./...`)
python <skill_dir>/scripts/analyze.py ./...
# Recursive scan rooted at a subtree
python <skill_dir>/scripts/analyze.py ./internal/...
| Flag | Effect |
|---|---|
--include-tests | Also analyze *_test.go files. In Go, tests and benchmarks both live in *_test.go; there is no separate benchmark-file convention, so this one flag covers both. By default they are skipped because structs in test files are fixtures, not production hot-path data. |
--arch {amd64,arm64,386,arm} | Target pointer width (default amd64). amd64/arm64 → 8-byte pointers; 386/arm → 4-byte. Note: custom_types.json sizes are authored for 64-bit. |
Unresolved named types (cross-package types not in custom_types.json) are
assumed 8B / align 8 / mixed and listed on stderr so you can add precise
entries — ignore generic type parameters like T/K/V in that list.
For each struct the script prints:
=== StructName ===
Current: 48 bytes (align 8), GC scan: 48 bytes
Size-opt: 40 bytes (align 8), GC scan: 40 bytes [saves 8B]
GC-opt: 40 bytes (align 8), GC scan: 24 bytes [scan -24B]
Current layout:
offset size pad ptr field
0 8 0 pure Entities []Entity
8 8 0 none ID uint64
16 24 0 mix Components []ComponentID
40 4 0 none Count uint32
44 4 4 none — padding —
GC-optimal layout:
offset size pad ptr field
0 8 0 pure Entities []Entity
8 24 0 mix Components []ComponentID
32 8 0 none ID uint64
40 4 0 none Count uint32
44 4 4 none — padding —
After analysis, reorder struct fields manually following the GC-optimal layout. Verify with:
go vet ./...
go test -race ./...