[fp-enhancer] Improve pkg/cli: pre-allocate slices with known capacity#21763
Closed
github-actions[bot] wants to merge 1 commit intomainfrom
Closed
[fp-enhancer] Improve pkg/cli: pre-allocate slices with known capacity#21763github-actions[bot] wants to merge 1 commit intomainfrom
github-actions[bot] wants to merge 1 commit intomainfrom
Conversation
Replace bare 'var result []string' declarations with 'make([]string, 0, len(lines))' in transformation functions that process line slices. Also pre-allocate other slices where the upper bound on element count is known at declaration time. This reduces GC pressure by avoiding repeated slice growth and re-allocation during append operations. Files changed: - codemod_bash_anonymous.go: pre-allocate result from lines - codemod_discussion_flag.go: pre-allocate result from lines - codemod_expires_integer.go: pre-allocate result from lines - codemod_github_app.go: pre-allocate result from lines - codemod_mcp_mode_to_type.go: pre-allocate result from lines - codemod_mcp_network.go: pre-allocate 3 result slices from lines - codemod_playwright_domains.go: pre-allocate result from lines - codemod_safe_inputs_to_mcp_scripts.go: pre-allocate result from lines - codemod_serena_local_mode.go: pre-allocate result from lines - codemod_yaml_utils.go: pre-allocate result from lines - actionlint.go: pre-allocate relPaths from lockFiles - add_interactive_engine.go: pre-allocate engineOptions from catalog - run_interactive.go: pre-allocate result from inputPtrs Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
/q turn off this optimization, it generates codeql violations |
Contributor
Author
|
🎩 Mission equipment ready! Q has optimized your workflow. Use wisely, 007! 🔫 |
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.
Functional/Immutability Enhancements - Package:
pkg/cliApplies pre-allocation improvements to 13 files in
pkg/cli, replacing barevar result []stringdeclarations withmake([]string, 0, len(lines))in functions that process YAML frontmatter line slices.Round-Robin Progress: First run, processing
pkg/cli. Next package to process:pkg/consoleSummary of Changes
Immutability / Initialization Improvements
All 13 changed files follow the same pattern: a function receives a
[]stringslice (typically namedlines,lockFiles, or a map), builds a result slice by appending, and returns it. The originalvar result []stringdeclaration causes the runtime to start with a nil slice and grow it on every append, reallocating 2× each time. By usingmake([]string, 0, len(input))we declare the upper-bound capacity upfront, eliminating redundant allocations for typical-size inputs.Codemod line-transform functions — output is always ≤ input length:
codemod_bash_anonymous.go—replaceBashAnonymousWithTruecodemod_discussion_flag.go— anonymous transform closurecodemod_expires_integer.go—convertExpiresIntegersToDayStringscodemod_github_app.go—renameAppToGitHubAppcodemod_mcp_mode_to_type.go—renameModeToTypeInMCPServerscodemod_mcp_network.go—removeFieldFromMCPServer,updateNetworkAllowed,addAllowedToNetworkcodemod_playwright_domains.go—removeFieldFromPlaywrightcodemod_safe_inputs_to_mcp_scripts.go—renameTopLevelKeycodemod_serena_local_mode.go—replaceSerenaLocalModeWithDockercodemod_yaml_utils.go—removeFieldFromBlockOther pre-allocations:
actionlint.go—relPathsslice pre-allocated tolen(lockFiles)(exact match)add_interactive_engine.go—engineOptionsslice pre-allocated tolen(catalog.All())run_interactive.go—resultslice pre-allocated tolen(inputPtrs)Benefits
Principles Applied
var x []Twhen size is knownTesting
go build ./pkg/cli/succeedsgo vet ./pkg/cli/passes_test.go)Before / After Example