optimizer

package
v1.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalcFieldSize

func CalcFieldSize(typ types.Type) (size, align int64)

CalcFieldSize calculates field size using platform-aware sizes

func CalcOptimizedSize

func CalcOptimizedSize(fields []FieldInfo) int64

CalcOptimizedSize calculates the size after optimization (uses FieldInfo sizes)

func CalcStructSize

func CalcStructSize(st *types.Struct) int64

CalcStructSize calculates struct size (uses types.Sizes to simulate unsafe.Sizeof)

func CalcStructSizeFromFields

func CalcStructSizeFromFields(fields []FieldInfo) int64

CalcStructSizeFromFields calculates struct size from field information

func EstimateFieldSizeWithLookup added in v1.7.4

func EstimateFieldSizeWithLookup(expr ast.Expr, pkgDir string) (size, align int64)

EstimateFieldSizeWithLookup estimates field size (with type lookup) - exported for testing

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

func ReorderFields(fields []FieldInfo, sortSameSize bool, reservedFields []string) []FieldInfo

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

func NewOptimizer(cfg *Config, analyzer *analyzer.Analyzer) *Optimizer

NewOptimizer creates a new optimizer

func (*Optimizer) GetOptimized

func (o *Optimizer) GetOptimized() map[string]*StructInfo

GetOptimized returns the optimized struct info map (thread-safe)

func (*Optimizer) GetReport

func (o *Optimizer) GetReport() *Report

GetReport returns the report (thread-safe)

func (*Optimizer) Log

func (o *Optimizer) Log(level int, format string, args ...interface{})

Log outputs a log message to stderr

func (*Optimizer) Optimize

func (o *Optimizer) Optimize() (*Report, error)

Optimize runs the optimization (entry point). Two-phase processing:

Phase 1: Collect struct location info only (no package loading, no field analysis)
Phase 2: Optimize all collected structs in parallel (load packages on demand)

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

type StructTask

type StructTask struct {
	PkgPath    string
	StructName string
	FilePath   string
	Depth      int
	Level      int
	ParentKey  string // "pkg.StructName" of parent, empty for root
}

StructTask represents a struct processing task

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL