Skip to content

pkg/sliceutil + pkg/workflow: use map[T]struct{} for set semantics instead of map[T]bool #29183

@github-actions

Description

@github-actions

Problem

make(map[T]bool) is used as a set (tracking seen/visited items) throughout the codebase — 153+ non-test production files use this pattern. Meanwhile, map[T]struct{} is used in 14 production files for the same purpose, creating an inconsistency. Using bool values for set membership is less idiomatic Go and wastes memory: struct{} has zero size whereas bool is 1 byte per map entry.

The inconsistency originates from core utility functions that serve as patterns for the rest of the codebase:

  • pkg/sliceutil/sliceutil.go:64Deduplicate uses make(map[T]bool, len(slice))
  • pkg/workflow/validation_helpers.go:212validateNoDuplicateIDs uses make(map[string]bool)
  • pkg/stringutil/sanitize.go:22commonWorkflowKeywords declared as map[string]bool{...}

These three are referenced throughout the codebase and set the precedent for the map[...]bool pattern.

Impact

  • Severity: Medium
  • Affected Files: 153+ production Go files
  • Risk: No correctness risk; purely idiomatic and memory efficiency gap

Recommendation

Migrate set-semantics maps to map[T]struct{}. Start with the utility functions that serve as patterns:

Before (pkg/sliceutil/sliceutil.go):

func Deduplicate[T comparable](slice []T) []T {
    seen := make(map[T]bool, len(slice))
    for _, item := range slice {
        if !seen[item] {
            seen[item] = true

After:

func Deduplicate[T comparable](slice []T) []T {
    seen := make(map[T]struct{}, len(slice))
    for _, item := range slice {
        if _, ok := seen[item]; !ok {
            seen[item] = struct{}{}

Similarly for validateNoDuplicateIDs and the commonWorkflowKeywords literal.

Validation

  • Run go test ./pkg/sliceutil/... after changing Deduplicate
  • Run go test ./pkg/workflow/... after changing validateNoDuplicateIDs
  • Run go test ./pkg/stringutil/... after changing commonWorkflowKeywords
  • Run grep -r "make(map\[string\]bool" pkg/ to track remaining occurrences
  • Confirm no functional change in test results

Estimated Effort: Medium (many files, but mechanical change)

Generated by Sergo - Serena Go Expert · ● 536.8K ·

  • expires on May 6, 2026, 8:44 PM UTC

Metadata

Metadata

Labels

cookieIssue Monster Loves Cookies!sergo

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions