refactor(opt,sentinel): split opt.go, add OptimizeLines([]string), and define sentinel error types with errors.Is/As#7
Merged
Conversation
Co-authored-by: traeagent <traeagent@users.noreply.github.com>
Co-authored-by: traeagent <traeagent@users.noreply.github.com>
…Lines([]string), and define sentinel error types with errors.Is/As - opt/: split 2100-line opt.go into opt.go (public API), fold.go (constant folding), block.go (basic blocks), dataflow.go (copy propagation), peephole.go (peephole passes), aggressive.go (-O2 passes) - opt/: add OptimizeLines([]string) and PeepholeOnlyLines([]string) to eliminate string.Split/Join overhead in core.go - vas/errors.go: new file defining ErrInvalidRegister, ErrOperandCount, ErrLintError, ErrPreprocessing, ErrUndefinedLabel sentinels - core.go, prep.go, reg.go: replace fmt.Errorf strings with sentinel-wrapped errors using %w for errors.Is/As compatibility - Build and all tests pass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR refactors the optimizer package to improve maintainability, performance, and error handling.
Changes
1.
OptimizeLines([]string)API — eliminate string round-trips in core.goOptimizeLines(lines []string, level int) []stringandPeepholeOnlyLines(lines []string) []stringinvas/opt/opt.gocore.goswitched fromstrings.Split → strings.Jointo direct[]string-based pipeline, reducing string allocation overhead during optimization2. Split 2100-line
opt.gointo 6 focused filesopt.goOptimize,OptimizeLines,PeepholeOnly,PeepholeOnlyLines)fold.goFoldConstants,foldLine,tokenizeFold)block.gosplitBlocks,deadCodeElim,dstReg,readRegs,regIndex,extractLabel,stripComment)dataflow.gopeephole.goxorZero,testCmp,nopMerge,leaFuse,noopElim,pushPopMov,cancelPairElim, etc.)aggressive.gocse,licm, redundant load elim,pushPopElim,scanPushModPop,tailCallOpt)3. Sentinel error types (
vas/errors.go) — errors.Is/As compatiblecore.go→fmt.Errorf("%w: ...", ErrOperandCount, ...)reg.go→fmt.Errorf("%w: %s", ErrInvalidRegister, name)prep.go→fmt.Errorf("%w: %s", ErrUndefinedLabel, ...)prep.go→fmt.Errorf("%w: recursion limit exceeded", ErrPreprocessing)core.go→fmt.Errorf("%w (...)", ErrLintError)Callers can now use
errors.Is(err, vas.ErrOperandCount)etc. for precise error classification.Verification
go build ./...✅go test ./...✅ (all packages pass)