Documentation
¶
Index ¶
- func CalcFieldSize(typ types.Type) (size, align int64)
- func CalcOptimizedSize(fields []FieldInfo) int64
- func CalcStructSize(st *types.Struct) int64
- func CalcStructSizeFromFields(fields []FieldInfo) int64
- func EstimateFieldSizeWithLookup(expr ast.Expr, pkgDir string) (size, align int64)
- type Config
- type FieldAnalyzer
- type FieldInfo
- type MethodIndex
- type Optimizer
- type Report
- type SkipCategory
- type StructInfo
- type StructReport
- type StructTask
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CalcFieldSize ¶
CalcFieldSize calculates field size using platform-aware sizes
func CalcOptimizedSize ¶
CalcOptimizedSize calculates the size after optimization (uses FieldInfo sizes)
func CalcStructSize ¶
CalcStructSize calculates struct size (uses types.Sizes to simulate unsafe.Sizeof)
func CalcStructSizeFromFields ¶
CalcStructSizeFromFields calculates struct size from field information
Types ¶
type Config ¶
type Config struct {
TargetDir string
StructName string
Package string
SourceFile string
Write bool
Backup bool
SkipDirs []string
SkipFiles []string
SkipByMethods []string
SkipByNames []string
Verbose int
SortSameSize bool
Output string
ProjectType string
GOPATH string
MaxDepth int
Timeout int
PkgScope string
PkgWorkerLimit int // package-level concurrency limit (default 4, prevents OOM)
ReservedFields []string // reserved field names (always placed last)
Recursive bool // recursively scan sub-packages (-package mode)
AllowExternalPkgs bool // allow scanning cross-package structs (including vendor directory)
}
Config holds the optimizer configuration
type FieldAnalyzer ¶
type FieldAnalyzer struct {
// contains filtered or unexported fields
}
FieldAnalyzer analyzes struct fields
func NewFieldAnalyzer ¶
func NewFieldAnalyzer(info *types.Info, fset *token.FileSet) *FieldAnalyzer
NewFieldAnalyzer creates a new field analyzer
func (*FieldAnalyzer) AnalyzeStruct ¶
func (fa *FieldAnalyzer) AnalyzeStruct(st *types.Struct, structName, pkgPath, filePath string) *StructInfo
AnalyzeStruct analyzes the fields of a struct
type FieldInfo ¶
type FieldInfo struct {
Name string
Type types.Type
Size int64
Align int64
IsEmbed bool
IsInterface bool
IsStdLib bool
IsThirdParty bool
PkgPath string
TypeName string
Tag string
}
FieldInfo holds field information
func ReorderFields ¶
ReorderFields reorders fields to optimize memory alignment. Returns the sorted order; the caller decides whether to adopt it (only if it saves memory). Fields in reservedFields are always placed last.
type MethodIndex ¶ added in v1.5.0
type MethodIndex struct {
// contains filtered or unexported fields
}
MethodIndex caches method sets for packages. Structure: map[package_path]map[struct_name]map[method_name]bool
func NewMethodIndex ¶ added in v1.5.0
func NewMethodIndex() *MethodIndex
NewMethodIndex creates a new method indexer
func (*MethodIndex) HasMethod ¶ added in v1.5.0
func (mi *MethodIndex) HasMethod(pkgPath, structName, methodPattern string) bool
HasMethod checks if a struct has the specified method (supports wildcards)
type Optimizer ¶
type Optimizer struct {
// contains filtered or unexported fields
}
Optimizer is the struct optimizer
func NewOptimizer ¶
NewOptimizer creates a new optimizer
func (*Optimizer) GetOptimized ¶
func (o *Optimizer) GetOptimized() map[string]*StructInfo
GetOptimized returns the optimized struct info map (thread-safe)
type Report ¶
type Report struct {
TotalStructs int
OptimizedCount int
SkippedCount int
TotalSaved int64
StructReports []*StructReport
RootStruct string // root struct name (-struct mode)
RootStructSize int64 // root struct original size (root struct only)
RootStructOptSize int64 // root struct optimized size (root struct only)
TotalOrigSize int64 // total original size of all structs
TotalOptSize int64 // total optimized size of all structs
}
Report is the optimization report
type SkipCategory ¶ added in v1.9.0
type SkipCategory int
SkipCategory classifies why a struct was skipped
const ( SkipNone SkipCategory = iota SkipEmpty // empty struct SkipSingleField // single-field struct SkipByMethod // skipped by method pattern SkipByName // skipped by name pattern SkipVendor // vendor/third-party package SkipStdLib // standard library SkipNonProject // non-project internal package SkipCircular // circular reference SkipMaxDepth // exceeded max recursion depth SkipLoadFailed // failed to load package SkipLookupFailed // failed to find struct in package SkipPanic // panic during processing )
type StructInfo ¶
type StructInfo struct {
Name string
PkgPath string
File string
Fields []FieldInfo
OrigSize int64
OptSize int64
Optimized bool
Skipped bool
SkipReason string
OrigOrder []string
OptOrder []string
}
StructInfo holds struct information
type StructReport ¶
type StructReport struct {
Name string
PkgPath string
File string
OrigSize int64
OptSize int64
Saved int64
OrigFields []string
OptFields []string
FieldTypes map[string]string // field_name -> type_name
FieldSizes map[string]int64 // field_name -> size (bytes)
Skipped bool
SkipReason string
SkipCategory SkipCategory // enum-based skip classification
Depth int
ParentKey string // "pkg.StructName" of parent, empty for root
HasEmbed bool // whether it contains embedded fields
}
StructReport represents a struct optimization report