From b4aebaebf54246ae1f7260f32e0ae1b7d3acbdf9 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 19 Mar 2026 09:23:03 +0000 Subject: [PATCH] refactor(pkg/cli): pre-allocate slices with known capacity 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> --- pkg/cli/actionlint.go | 2 +- pkg/cli/add_interactive_engine.go | 2 +- pkg/cli/codemod_bash_anonymous.go | 2 +- pkg/cli/codemod_discussion_flag.go | 2 +- pkg/cli/codemod_expires_integer.go | 2 +- pkg/cli/codemod_github_app.go | 2 +- pkg/cli/codemod_mcp_mode_to_type.go | 2 +- pkg/cli/codemod_mcp_network.go | 6 +++--- pkg/cli/codemod_playwright_domains.go | 2 +- pkg/cli/codemod_safe_inputs_to_mcp_scripts.go | 2 +- pkg/cli/codemod_serena_local_mode.go | 2 +- pkg/cli/codemod_yaml_utils.go | 2 +- pkg/cli/run_interactive.go | 2 +- 13 files changed, 15 insertions(+), 15 deletions(-) diff --git a/pkg/cli/actionlint.go b/pkg/cli/actionlint.go index ddbbdbdbb1d..dd2ef876ba4 100644 --- a/pkg/cli/actionlint.go +++ b/pkg/cli/actionlint.go @@ -210,7 +210,7 @@ func runActionlintOnFiles(lockFiles []string, verbose bool, strict bool) error { } // Get relative paths from git root for all files - var relPaths []string + relPaths := make([]string, 0, len(lockFiles)) for _, lockFile := range lockFiles { relPath, err := filepath.Rel(gitRoot, lockFile) if err != nil { diff --git a/pkg/cli/add_interactive_engine.go b/pkg/cli/add_interactive_engine.go index 7dbe6349aa9..386ed7cdb89 100644 --- a/pkg/cli/add_interactive_engine.go +++ b/pkg/cli/add_interactive_engine.go @@ -85,7 +85,7 @@ func (c *AddInteractiveConfig) selectAIEngineAndKey() error { // Build engine options with notes about existing secrets and workflow specification. // The list of engines is derived from the catalog to ensure all registered engines appear. catalog := workflow.NewEngineCatalog(workflow.NewEngineRegistry()) - var engineOptions []huh.Option[string] + engineOptions := make([]huh.Option[string], 0, len(catalog.All())) for _, def := range catalog.All() { opt := constants.GetEngineOption(def.ID) label := fmt.Sprintf("%s - %s", def.DisplayName, def.Description) diff --git a/pkg/cli/codemod_bash_anonymous.go b/pkg/cli/codemod_bash_anonymous.go index 03ceab47104..2bbfba8a58b 100644 --- a/pkg/cli/codemod_bash_anonymous.go +++ b/pkg/cli/codemod_bash_anonymous.go @@ -49,7 +49,7 @@ func getBashAnonymousRemovalCodemod() Codemod { // replaceBashAnonymousWithTrue replaces 'bash:' with 'bash: true' in the tools block func replaceBashAnonymousWithTrue(lines []string) ([]string, bool) { - var result []string + result := make([]string, 0, len(lines)) var modified bool var inToolsBlock bool var toolsIndent string diff --git a/pkg/cli/codemod_discussion_flag.go b/pkg/cli/codemod_discussion_flag.go index 700d942c502..a07ae543a5b 100644 --- a/pkg/cli/codemod_discussion_flag.go +++ b/pkg/cli/codemod_discussion_flag.go @@ -45,7 +45,7 @@ func getDiscussionFlagRemovalCodemod() Codemod { } newContent, applied, err := applyFrontmatterLineTransform(content, func(lines []string) ([]string, bool) { - var result []string + result := make([]string, 0, len(lines)) var modified bool var inSafeOutputsBlock bool var safeOutputsIndent string diff --git a/pkg/cli/codemod_expires_integer.go b/pkg/cli/codemod_expires_integer.go index e7dab3a7321..a416f6a3d0e 100644 --- a/pkg/cli/codemod_expires_integer.go +++ b/pkg/cli/codemod_expires_integer.go @@ -64,7 +64,7 @@ func getExpiresIntegerToStringCodemod() Codemod { // convertExpiresIntegersToDayStrings converts integer expires values to day strings within safe-outputs blocks. // Only affects expires lines nested inside a safe-outputs block. func convertExpiresIntegersToDayStrings(lines []string) ([]string, bool) { - var result []string + result := make([]string, 0, len(lines)) var modified bool var inSafeOutputsBlock bool var safeOutputsIndent string diff --git a/pkg/cli/codemod_github_app.go b/pkg/cli/codemod_github_app.go index 2214a33d548..4684fa9a59c 100644 --- a/pkg/cli/codemod_github_app.go +++ b/pkg/cli/codemod_github_app.go @@ -80,7 +80,7 @@ func hasDeprecatedAppField(frontmatter map[string]any) bool { // renameAppToGitHubApp renames 'app:' to 'github-app:' within tools.github, safe-outputs, and checkout blocks. func renameAppToGitHubApp(lines []string) ([]string, bool) { - var result []string + result := make([]string, 0, len(lines)) modified := false // Block tracking diff --git a/pkg/cli/codemod_mcp_mode_to_type.go b/pkg/cli/codemod_mcp_mode_to_type.go index c23a17164e9..6ce496048fc 100644 --- a/pkg/cli/codemod_mcp_mode_to_type.go +++ b/pkg/cli/codemod_mcp_mode_to_type.go @@ -55,7 +55,7 @@ func getMCPModeToTypeCodemod() Codemod { // renameModeToTypeInMCPServers renames 'mode:' to 'type:' within mcp-servers blocks func renameModeToTypeInMCPServers(lines []string) ([]string, bool) { - var result []string + result := make([]string, 0, len(lines)) var modified bool var inMCPServers bool var mcpServersIndent string diff --git a/pkg/cli/codemod_mcp_network.go b/pkg/cli/codemod_mcp_network.go index f46ac26c1e0..1cb5e633739 100644 --- a/pkg/cli/codemod_mcp_network.go +++ b/pkg/cli/codemod_mcp_network.go @@ -146,7 +146,7 @@ func getMCPNetworkMigrationCodemod() Codemod { // removeFieldFromMCPServer removes a field from a specific MCP server configuration func removeFieldFromMCPServer(lines []string, serverName string, fieldName string) ([]string, bool) { - var result []string + result := make([]string, 0, len(lines)) var modified bool var inMCPServers bool var mcpServersIndent string @@ -283,7 +283,7 @@ func addTopLevelNetwork(lines []string, domains []string) []string { // updateNetworkAllowed updates the existing top-level network.allowed configuration func updateNetworkAllowed(lines []string, domains []string) []string { - var result []string + result := make([]string, 0, len(lines)) var inNetworkBlock bool var networkIndent string var inAllowedBlock bool @@ -359,7 +359,7 @@ func updateNetworkAllowed(lines []string, domains []string) []string { // addAllowedToNetwork adds an allowed field to an existing network block func addAllowedToNetwork(lines []string, domains []string) []string { - var result []string + result := make([]string, 0, len(lines)) var inNetworkBlock bool var networkIndent string var insertIndex = -1 diff --git a/pkg/cli/codemod_playwright_domains.go b/pkg/cli/codemod_playwright_domains.go index e98fae60b0c..adcf3e5e009 100644 --- a/pkg/cli/codemod_playwright_domains.go +++ b/pkg/cli/codemod_playwright_domains.go @@ -106,7 +106,7 @@ func getPlaywrightDomainsCodemod() Codemod { // removeFieldFromPlaywright removes a field from the tools.playwright block (two-level nesting) func removeFieldFromPlaywright(lines []string, fieldName string) ([]string, bool) { - var result []string + result := make([]string, 0, len(lines)) var modified bool var inTools bool var toolsIndent string diff --git a/pkg/cli/codemod_safe_inputs_to_mcp_scripts.go b/pkg/cli/codemod_safe_inputs_to_mcp_scripts.go index dafaf34255d..6d8616ab7b7 100644 --- a/pkg/cli/codemod_safe_inputs_to_mcp_scripts.go +++ b/pkg/cli/codemod_safe_inputs_to_mcp_scripts.go @@ -33,7 +33,7 @@ func getSafeInputsToMCPScriptsCodemod() Codemod { // renameTopLevelKey renames a top-level YAML key from oldKey to newKey, preserving formatting. func renameTopLevelKey(lines []string, oldKey, newKey string) ([]string, bool) { - var result []string + result := make([]string, 0, len(lines)) applied := false for _, line := range lines { diff --git a/pkg/cli/codemod_serena_local_mode.go b/pkg/cli/codemod_serena_local_mode.go index f821e481892..01034ef05c7 100644 --- a/pkg/cli/codemod_serena_local_mode.go +++ b/pkg/cli/codemod_serena_local_mode.go @@ -62,7 +62,7 @@ func getSerenaLocalModeCodemod() Codemod { // replaceSerenaLocalModeWithDocker replaces 'mode: local' with 'mode: docker' within the // tools.serena block in frontmatter lines. func replaceSerenaLocalModeWithDocker(lines []string) ([]string, bool) { - var result []string + result := make([]string, 0, len(lines)) var modified bool var inTools bool var toolsIndent string diff --git a/pkg/cli/codemod_yaml_utils.go b/pkg/cli/codemod_yaml_utils.go index 2a818d75d60..89c0ff3474d 100644 --- a/pkg/cli/codemod_yaml_utils.go +++ b/pkg/cli/codemod_yaml_utils.go @@ -112,7 +112,7 @@ func applyFrontmatterLineTransform(content string, transform func([]string) ([]s // removeFieldFromBlock removes a field and its nested content from a YAML block // Returns the modified lines and whether any changes were made func removeFieldFromBlock(lines []string, fieldName string, parentBlock string) ([]string, bool) { - var result []string + result := make([]string, 0, len(lines)) var modified bool var inParentBlock bool var parentIndent string diff --git a/pkg/cli/run_interactive.go b/pkg/cli/run_interactive.go index 59193cb87f8..c986470990a 100644 --- a/pkg/cli/run_interactive.go +++ b/pkg/cli/run_interactive.go @@ -320,7 +320,7 @@ func collectInputsWithMap(inputs map[string]*workflow.InputDefinition) ([]string } // Collect the final values from the pointers - var result []string + result := make([]string, 0, len(inputPtrs)) for name, valuePtr := range inputPtrs { value := *valuePtr if value != "" {