Skip to content

[fp-enhancer] Improve pkg/cli: pre-allocate slices with known capacity#21763

Closed
github-actions[bot] wants to merge 1 commit intomainfrom
fp-enhancer/pkg-cli-immutability-20260319-b79638a363fb44fb
Closed

[fp-enhancer] Improve pkg/cli: pre-allocate slices with known capacity#21763
github-actions[bot] wants to merge 1 commit intomainfrom
fp-enhancer/pkg-cli-immutability-20260319-b79638a363fb44fb

Conversation

@github-actions
Copy link
Contributor

Functional/Immutability Enhancements - Package: pkg/cli

Applies pre-allocation improvements to 13 files in pkg/cli, replacing bare var result []string declarations with make([]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/console

Summary of Changes

Immutability / Initialization Improvements

All 13 changed files follow the same pattern: a function receives a []string slice (typically named lines, lockFiles, or a map), builds a result slice by appending, and returns it. The original var result []string declaration causes the runtime to start with a nil slice and grow it on every append, reallocating 2× each time. By using make([]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.goreplaceBashAnonymousWithTrue
  • codemod_discussion_flag.go — anonymous transform closure
  • codemod_expires_integer.goconvertExpiresIntegersToDayStrings
  • codemod_github_app.gorenameAppToGitHubApp
  • codemod_mcp_mode_to_type.gorenameModeToTypeInMCPServers
  • codemod_mcp_network.goremoveFieldFromMCPServer, updateNetworkAllowed, addAllowedToNetwork
  • codemod_playwright_domains.goremoveFieldFromPlaywright
  • codemod_safe_inputs_to_mcp_scripts.gorenameTopLevelKey
  • codemod_serena_local_mode.goreplaceSerenaLocalModeWithDocker
  • codemod_yaml_utils.goremoveFieldFromBlock

Other pre-allocations:

  • actionlint.gorelPaths slice pre-allocated to len(lockFiles) (exact match)
  • add_interactive_engine.goengineOptions slice pre-allocated to len(catalog.All())
  • run_interactive.goresult slice pre-allocated to len(inputPtrs)

Benefits

  • Performance: Reduces GC pressure; avoids repeated slice growth for inputs of typical size
  • Clarity: Declarative capacity hint communicates the expected output size at a glance
  • Consistency: All line-transform functions in the codemod subsystem now use the same pattern

Principles Applied

  1. Immutability First: No var x []T when size is known
  2. Declarative Over Imperative: Initialization expresses expected capacity
  3. Pragmatic Balance: Only applied where size bound is clearly available; no speculative changes

Testing

  • ✅ All existing tests pass — verified with targeted test runs covering every changed file
  • go build ./pkg/cli/ succeeds
  • go vet ./pkg/cli/ passes
  • ✅ No behavioral changes — only allocation strategy changed
  • ✅ Test files verified present before refactoring (all 13 files have corresponding _test.go)

Before / After Example

// Before
func replaceSerenaLocalModeWithDocker(lines []string) ([]string, bool) {
    var result []string
    // ...appends grow slice from nil repeatedly
}

// After
func replaceSerenaLocalModeWithDocker(lines []string) ([]string, bool) {
    result := make([]string, 0, len(lines))
    // ...appends never reallocate for typical inputs
}

Generated by Functional Pragmatist ·

  • expires on Mar 20, 2026, 9:25 AM UTC

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>
@pelikhan
Copy link
Contributor

/q turn off this optimization, it generates codeql violations

@github-actions
Copy link
Contributor Author

github-actions bot commented Mar 19, 2026

🎩 Mission equipment ready! Q has optimized your workflow. Use wisely, 007! 🔫

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant