Skip to content

refactor(difc): extract tagSet to deduplicate concurrent map logic from Label and Capabilities - #8780

Merged
lpcox merged 2 commits into
mainfrom
copilot/duplicate-code-concurrent-tag-set-methods
Jul 6, 2026
Merged

refactor(difc): extract tagSet to deduplicate concurrent map logic from Label and Capabilities#8780
lpcox merged 2 commits into
mainfrom
copilot/duplicate-code-concurrent-tag-set-methods

Conversation

Copilot AI commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Label and Capabilities both independently maintained a map[Tag]struct{} + sync.RWMutex with five nearly-identical methods (Add, AddAll, Remove, Contains, GetTags/GetAll). Any concurrency fix or logging change had to be applied twice.

Changes

  • New internal/difc/tagset.go: unexported tagSet struct with tags map[Tag]struct{} + sync.RWMutex and shared private methods: add, addAll, remove, removeAll, contains, getAll, clear, count, isEmpty.

  • capabilities.go: embed tagSet, delegate all seven public methods; debug logging preserved around the delegates; sync import removed.

  • labels.go: embed tagSet, delegate Add/AddAll/Remove/RemoveAll/Contains/GetTags/IsEmpty; Union, Intersect, and Clone are unchanged — they access l.tags/l.mu directly via Go field promotion; sync import removed.

// Before — duplicated in both files
type Label struct {
    tags map[Tag]struct{}
    mu   sync.RWMutex
}
func (l *Label) Add(tag Tag) {
    l.mu.Lock(); defer l.mu.Unlock()
    l.tags[tag] = struct{}{}
}

// After — single definition in tagset.go, embedded in both types
type Label struct{ tagSet }

func (l *Label) Add(tag Tag) { l.add(tag) }

The set-algebra methods (Union, Intersect) that acquire two locks simultaneously are left untouched; field promotion keeps them working without modification.

…nt map methods

Extract an unexported tagSet struct in internal/difc/tagset.go that holds
the shared map[Tag]struct{} + sync.RWMutex fields and provides common
concurrent mutation/read methods (add, addAll, remove, removeAll, contains,
getAll, clear, count, isEmpty).

Both Label (labels.go) and Capabilities (capabilities.go) now embed tagSet
and delegate their public methods to it. This eliminates ~25 lines of
duplicated mutex-locking logic and ensures any future concurrency fixes or
logging changes need to be made in only one place.

Closes #8772
Copilot AI changed the title [WIP] Refactor duplicate concurrent tag-set methods in capabilities and labels refactor(difc): extract tagSet to deduplicate concurrent map logic from Label and Capabilities Jul 6, 2026
Copilot AI requested a review from lpcox July 6, 2026 13:11
Copilot finished work on behalf of lpcox July 6, 2026 13:11
@lpcox
lpcox marked this pull request as ready for review July 6, 2026 13:14
Copilot AI review requested due to automatic review settings July 6, 2026 13:14
@lpcox
lpcox merged commit 1893ac2 into main Jul 6, 2026
36 checks passed
@lpcox
lpcox deleted the copilot/duplicate-code-concurrent-tag-set-methods branch July 6, 2026 13:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors DIFC tag management by extracting the shared concurrent map[Tag]struct{} + sync.RWMutex logic into a single unexported tagSet type, reducing duplication between Label and Capabilities while preserving existing behavior and debug logging.

Changes:

  • Added internal/difc/tagset.go with a shared tagSet implementation covering common set operations (add, addAll, remove, removeAll, contains, getAll, clear, count, isEmpty).
  • Updated internal/difc/capabilities.go to embed tagSet and delegate public methods, keeping existing debug logs around operations.
  • Updated internal/difc/labels.go to embed tagSet and delegate the basic set methods, while leaving set-algebra methods (Union, Intersect, Clone) intact and still operating via promoted fields.
Show a summary per file
File Description
internal/difc/tagset.go Introduces shared concurrent tag-set implementation used by DIFC label/capabilities types.
internal/difc/labels.go Embeds tagSet and delegates basic operations; set-algebra methods continue using promoted fields/locks.
internal/difc/capabilities.go Embeds tagSet and delegates public API while preserving existing debug logging behavior.

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 3/3 changed files
  • Comments generated: 0
  • Review effort level: Low

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.

[duplicate-code] Duplicate Code Pattern: Concurrent tag-set methods in Label and Capabilities

3 participants