chore: enable prealloc linter and address issues by NathanBaulch · Pull Request #3458 · testcontainers/testcontainers-go

Summary by CodeRabbit

  • Chores
    • Enabled the prealloc linter to improve code quality standards.
    • Optimized internal memory allocation patterns across the codebase for improved efficiency.

Walkthrough

The pull request enables the prealloc linter and applies allocation optimizations across the codebase. Changes include preallocating slices with capacity hints, adjusting slice/map initialization styles, and consolidating test logic. Behavior remains functionally unchanged.

Changes

Cohort / File(s) Summary
Linter Configuration
\.golangci\.yml
Enabled prealloc linter to enforce efficient slice preallocation practices
Slice Preallocation Optimizations
docker.go, internal/core/images.go, modules/cassandra/cassandra.go, modules/dolt/dolt.go, modules/mariadb/mariadb.go, modules/mysql/mysql.go
Replaced nil slice declarations with make() calls that preallocate capacity equal to the expected size, improving allocation efficiency
Slice and Map Initialization Adjustments
modules/couchbase/couchbase.go, modulegen/internal/context/context.go, modules/nats/options.go, modules/redpanda/options.go, mounts_test.go
Adjusted slice and map initialization styles (e.g., make() to literal form or vice versa) without behavioral changes
Test Optimization and Consolidation
modulegen/main_test.go
Moved GetModules call before slice construction and preallocated modulesAndExamples with combined capacity, removing redundant fetch logic

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Most changes are homogeneous, following a consistent preallocation pattern across multiple files
  • Changes are low-logic-density cosmetic/allocation adjustments with no observable behavioral impact
  • modulegen/main_test.go requires slightly more attention due to test flow reorganization and consolidation logic

Suggested labels

chore

Poem

🐰 Hops through slices with glee,
Prealloc'd and fancy and free,
No reallocs to waste,
With capacity placed,
Memory's as happy as can be!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 30.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The pull request title "chore: enable prealloc linter and address issues" directly and accurately summarizes the main changes in the changeset. The title references enabling the prealloc linter (reflected in the .golangci.yml modification) and addressing issues (reflected in the slice preallocation changes across eight files). The title is concise, specific, and uses appropriate conventional commit messaging with the "chore:" prefix, allowing teammates to understand the purpose of the change when scanning history.
Description Check ✅ Passed The pull request description is directly related to the changeset and provides meaningful information. It clearly states that the PR enables the golangci-lint prealloc linter and fixes eight slices that can be safely pre-allocated, which matches the actual changes shown in the raw summary. The description also explains the rationale, noting it's best-practice and straightforward to enforce, even if not expected to impact performance. The description is neither vague nor generic and accurately represents the purpose and scope of the changes.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 16dfc65 and 54855d7.

📒 Files selected for processing (13)
  • .golangci.yml (1 hunks)
  • docker.go (1 hunks)
  • internal/core/images.go (1 hunks)
  • modulegen/internal/context/context.go (2 hunks)
  • modulegen/main_test.go (1 hunks)
  • modules/cassandra/cassandra.go (1 hunks)
  • modules/couchbase/couchbase.go (1 hunks)
  • modules/dolt/dolt.go (1 hunks)
  • modules/mariadb/mariadb.go (1 hunks)
  • modules/mysql/mysql.go (1 hunks)
  • modules/nats/options.go (1 hunks)
  • modules/redpanda/options.go (1 hunks)
  • mounts_test.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (6)
modules/mariadb/mariadb.go (1)
container.go (1)
  • ContainerFile (110-115)
modules/couchbase/couchbase.go (1)
modules/couchbase/service.go (1)
  • Service (3-7)
modules/mysql/mysql.go (1)
container.go (1)
  • ContainerFile (110-115)
modulegen/main_test.go (1)
modulegen/internal/context/types.go (1)
  • TestcontainersModule (16-31)
modules/dolt/dolt.go (1)
container.go (1)
  • ContainerFile (110-115)
modules/cassandra/cassandra.go (2)
container.go (1)
  • ContainerFile (110-115)
options.go (1)
  • Executable (279-285)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: lint (modules/consul) / lint: modules/consul
  • GitHub Check: lint (modules/toxiproxy) / lint: modules/toxiproxy
  • GitHub Check: lint (modules/redpanda) / lint: modules/redpanda
  • GitHub Check: lint (modules/qdrant) / lint: modules/qdrant
  • GitHub Check: lint (modules/azure) / lint: modules/azure
  • GitHub Check: lint (modules/kafka) / lint: modules/kafka
  • GitHub Check: lint (modules/aerospike) / lint: modules/aerospike
  • GitHub Check: lint (modules/compose) / lint: modules/compose
  • GitHub Check: lint (modules/nats) / lint: modules/nats
  • GitHub Check: lint (modules/nebulagraph) / lint: modules/nebulagraph
  • GitHub Check: lint (modules/openldap) / lint: modules/openldap
  • GitHub Check: lint (modules/weaviate) / lint: modules/weaviate
  • GitHub Check: lint (modules/elasticsearch) / lint: modules/elasticsearch
  • GitHub Check: lint (modules/localstack) / lint: modules/localstack
  • GitHub Check: lint (modules/clickhouse) / lint: modules/clickhouse
  • GitHub Check: lint (modules/artemis) / lint: modules/artemis
  • GitHub Check: lint (modules/ollama) / lint: modules/ollama
  • GitHub Check: lint (modules/etcd) / lint: modules/etcd
  • GitHub Check: lint (modules/redis) / lint: modules/redis
  • GitHub Check: Analyze (go)
🔇 Additional comments (14)
.golangci.yml (1)

20-20: LGTM! Prealloc linter enabled.

This enables the prealloc linter to catch opportunities for slice preallocation, which aligns with the PR's objective to enforce this best practice.

mounts_test.go (1)

94-94: LGTM! Idiomatic empty slice literal.

Using []mount.Mount{} is the preferred idiomatic way to express an empty slice in test expectations, making the intent clearer than make([]mount.Mount, 0).

docker.go (1)

522-522: Excellent preallocation!

Preallocating the slice with capacity len(networks) is optimal here since you append exactly one IP address per network. This eliminates unnecessary allocations and slice growth.

modules/dolt/dolt.go (1)

262-262: Good preallocation pattern.

Preallocating initScripts with the exact capacity needed (len(scripts)) is efficient and follows best practices. This pattern is consistently applied across multiple modules in this PR.

modulegen/internal/context/context.go (2)

72-72: LGTM! Idiomatic nil slice declaration.

Using var dirs []string is the preferred idiomatic approach when starting with an empty slice that will be populated via append. The nil slice behaves identically to an empty slice for appending and is slightly more efficient.


93-93: LGTM! Consistent nil slice declaration.

Same idiomatic pattern as line 72. Using var dirs []string is appropriate when the slice will be populated dynamically via append.

modules/mariadb/mariadb.go (1)

97-97: Good preallocation pattern.

Preallocating initScripts with capacity len(scripts) prevents unnecessary slice growth. This matches the same efficient pattern applied in the dolt and mysql modules.

modules/nats/options.go (1)

16-16: LGTM! Cleaner map initialization.

Removing the explicit zero-capacity hint simplifies the code. For maps, make(map[string]string) is the idiomatic form when you don't need to specify an initial capacity.

modules/couchbase/couchbase.go (1)

69-69: LGTM! Idiomatic empty slice literal.

Using []Service{} is the preferred idiomatic form for initializing an empty slice. It's more concise than make([]Service, 0) and makes the intent clear.

modules/redpanda/options.go (1)

69-82: LGTM! Idiomatic initialization improvements.

The changes improve code clarity by:

  • Removing redundant zero-capacity hints from map initializations (Lines 76, 80)
  • Using literal slice syntax []listener{} instead of make([]listener, 0) (Line 79)

These are idiomatic Go patterns that maintain the same behavior while simplifying the code.

modulegen/main_test.go (1)

302-305: LGTM! Effective slice preallocation.

The slice is now preallocated with the exact required capacity (len(examples)+len(modules)), eliminating potential reallocations during the subsequent append operations. This optimization aligns well with the prealloc linter guidelines.

modules/mysql/mysql.go (1)

187-202: LGTM! Proper slice preallocation.

The initScripts slice is now preallocated with capacity equal to len(scripts), matching the exact number of elements to be appended in the loop. This eliminates unnecessary reallocations and aligns with the prealloc linter requirements.

internal/core/images.go (1)

41-77: LGTM! Conservative preallocation approach.

The images slice is preallocated with capacity len(lines), which represents an upper bound since not all lines contain FROM statements. While this may allocate slightly more capacity than needed in typical Dockerfiles, it guarantees no reallocations during the loop and is a reasonable trade-off for this optimization.

modules/cassandra/cassandra.go (1)

46-64: LGTM! Dual slice preallocation.

Both initScripts and execs slices are now preallocated with capacity len(scripts), matching the exact number of elements appended in the loop. This optimization prevents reallocations for both slices and follows the same pattern used in other module implementations.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.