Skip to content

Eliminate redundant zero-capacity slice allocations#12303

Merged
pelikhan merged 4 commits intomainfrom
copilot/eliminate-redundant-slice-allocations
Jan 28, 2026
Merged

Eliminate redundant zero-capacity slice allocations#12303
pelikhan merged 4 commits intomainfrom
copilot/eliminate-redundant-slice-allocations

Conversation

Copy link
Contributor

Copilot AI commented Jan 28, 2026

Replace make([]T, 0) with zero-value slice declarations. Zero-value slices work identically with append() and are more idiomatic.

Changes

Pattern:

// Before
result := make([]string, 0)
return &FileTracker{
    CreatedFiles: make([]string, 0),
}

// After
var result []string
return &FileTracker{
    // Field defaults to nil slice
}

Locations (13 instances):

  • pkg/cli/mcp_registry.go - 5 instances in runtime/package arg extraction
  • pkg/cli/run_push.go - File collection result
  • pkg/cli/file_tracker.go - Struct initialization (omitted nil fields)
  • pkg/cli/tool_graph.go - Struct initialization (omitted nil field)
  • pkg/parser/include_processor.go - Unexpected fields tracking
  • pkg/parser/import_processor.go - Topological sort queue
  • Test files - 2 mock tracker implementations

Net: 8 files, -9 lines

Original prompt

This section details on the original issue you should resolve

<issue_title>[Code Quality] Eliminate redundant zero-capacity slice allocations</issue_title>
<issue_description>## Description

Found 10 instances of explicit make([]T, 0) allocations across the codebase. This pattern is redundant and less idiomatic than zero-value slices in Go.

Current Pattern

envVarNames := make([]string, 0)  // Redundant allocation
for _, name := range vars {
    envVarNames = append(envVarNames, name)
}

Proposed Pattern

var envVarNames []string  // Cleaner, idiomatic Go
for _, name := range vars {
    envVarNames = append(envVarNames, name)
}

Locations to Fix

Production Code (8 instances)

  • pkg/workflow/mcp_setup_generator.go:607
  • pkg/workflow/jobs.go:48, 432
  • pkg/workflow/action_cache.go:241, 242, 279
  • pkg/workflow/compiler_safe_outputs_job.go:372
  • pkg/workflow/step_order_validation.go:42

Test Code (2 instances)

  • Test files (acceptable but can be improved)

Success Criteria

  • All 10 instances of make([]T, 0) replaced with zero-value declarations
  • All existing tests pass
  • Verify with go vet (no warnings)
  • Review nil slice behavior matches expectations
  • Run golangci-lint to confirm no regressions

Impact

  • Severity: Low - Minor code quality improvement
  • Effort: Low - Estimated 30 minutes
  • Risk: Minimal - Zero-value slices work identically with append
  • Benefit: More idiomatic Go code, slightly reduced memory allocations

Why This Matters

  1. Idiomatic Go: Zero-value slices are the preferred Go pattern
  2. Clarity: More explicit about intentional zero initialization
  3. Minor optimization: Eliminates unnecessary allocation syscall
  4. Consistency: Aligns with Go best practices

Validation Steps

# Find remaining instances
grep -r "make(\[\].*,\s*0)" pkg/

# Run tests
make test-unit

# Verify with go vet
go vet ./...

# Check with linter
make lint

Source

Extracted from Sergo Report: Memory Allocation & Documentation Quality - 2026-01-27

Priority

Low-Medium - Easy improvement, good for first-time contributors

AI generated by Discussion Task Miner - Code Quality Improvement Agent

  • expires on Feb 11, 2026, 9:11 AM UTC

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits January 28, 2026 16:15
…larations

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix redundant zero-capacity slice allocations in codebase Eliminate redundant zero-capacity slice allocations Jan 28, 2026
Copilot AI requested a review from pelikhan January 28, 2026 16:24
@pelikhan pelikhan marked this pull request as ready for review January 28, 2026 17:12
@pelikhan pelikhan merged commit 87a922e into main Jan 28, 2026
56 checks passed
@pelikhan pelikhan deleted the copilot/eliminate-redundant-slice-allocations branch January 28, 2026 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Code Quality] Eliminate redundant zero-capacity slice allocations

2 participants