refactor: Pre-compile regexes used inside loops#9
Merged
Conversation
Move regexp.MustCompile calls from inside functions to package-level var declarations so patterns are compiled once at program startup instead of on every call. This affects: - homebrew/generator.go: extractPlatform regex (called per-bottle) - homebrew/parser.go: 5 formula-parsing regexes (compiled per file) - rpm/parser.go: 7 distro-version regexes (compiled per call) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.
Several regex patterns are compiled on every function call or inside loops:
internal/generator/homebrew/generator.go:212—extractPlatformcompilesregexp.MustCompile(...)each time it's called, and it's invoked per-bottle in a loop at line 131.internal/generator/rpm/parser.go:172-180—parseVersionFromDistrocompiles 6+ regexes in a loop on every call, plus one more fallback regex at line 180.internal/generator/homebrew/parser.go:53-57—parseFormulacompiles 5 regexes per formula file.Move all of these to package-level
vardeclarations so they are compiled once at program startup. This is idiomatic Go and avoids unnecessary allocations, especially when processing many packages.Automated improvement by yeti improvement-identifier