Skip to content

chore: Use maps.Copy instead of manual copy loops - #19727

Open
SungJin1212 wants to merge 2 commits into
influxdata:masterfrom
SungJin1212:modernize/mapsloop
Open

SungJin1212 wants to merge 2 commits into
influxdata:masterfrom
SungJin1212:modernize/mapsloop

Conversation

@SungJin1212

Copy link
Copy Markdown
Contributor

Summary

Part of the modernize linter series (follow-up to #19675).
This PR replaces for k, v := range src { dst[k] = v } loops with maps.Copy (analyzer: mapsloop).

The rewrite is the output of the modernize linter with only the listed analyzer(s) enabled, applied via golangci-lint run --fix for GOOS=darwin, linux and windows, followed by goimports / golangci-lint fmt to drop imports left unused and regroup new ones.

Checklist

Related issues

resolves #

@skartikey skartikey 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.

@SungJin1212 Thanks for the contribution!

@skartikey skartikey added the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label Sep 17, 2026

@srebhan srebhan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your contribution @SungJin1212! Some comments, mostly about using maps.Clone instead of allocating then copying.

Comment on lines +206 to +207
j.Tags = make(map[string]string, len(o.Tags))
for k, v := range o.Tags {
j.Tags[k] = v
}
maps.Copy(j.Tags, o.Tags)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wouldn't

Suggested change
j.Tags = make(map[string]string, len(o.Tags))
for k, v := range o.Tags {
j.Tags[k] = v
}
maps.Copy(j.Tags, o.Tags)
j.Tags = maps.Clone(o.Tags)

be even better? More occurrences below...

Comment thread migrations/inputs_jolokia/migration.go Outdated
Comment on lines +274 to +275
j.TagPassFilters = make(map[string][]string, len(o.TagPassFilters))
for k, v := range o.TagPassFilters {
j.TagPassFilters[k] = v
}
maps.Copy(j.TagPassFilters, o.TagPassFilters)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Clone, same for the other places below.

Comment thread plugins/common/gnmi/handler.go Outdated
tags[key] = val
}
maps.Copy(tags, headerTags)
maps.Copy(tags, fullPath.tags(h.TagPathPrefix))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Pre-allocation doesn't take this into account. Get the result of the function first and pre-allocate more precisely.

Comment thread plugins/inputs/aliyuncms/discovery.go Outdated
Comment on lines +427 to +428
lastData = make(map[string]interface{}, len(data))
for k, v := range data {
lastData[k] = v
}
maps.Copy(lastData, data)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Clone.

Comment on lines +85 to +86
j.TagPassFilters = make(map[string][]string, len(o.TagPassFilters))
for k, v := range o.TagPassFilters {
j.TagPassFilters[k] = v
}
maps.Copy(j.TagPassFilters, o.TagPassFilters)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Clone.

Comment thread selfstat/selfstat.go
Comment on lines 147 to +148
t := make(map[string]string, len(tags))
for k, v := range tags {
t[k] = v
}
maps.Copy(t, tags)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Clone.

Comment thread selfstat/stat.go
Comment on lines 38 to 40
m := make(map[string]string, len(s.tags))
for k, v := range s.tags {
m[k] = v
}
maps.Copy(m, s.tags)
return m

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Clone.

Suggested change
m := make(map[string]string, len(s.tags))
for k, v := range s.tags {
m[k] = v
}
maps.Copy(m, s.tags)
return m
return maps.Clone(s.tags)

Comment thread selfstat/timingStat.go
Comment on lines 54 to 56
m := make(map[string]string, len(s.tags))
for k, v := range s.tags {
m[k] = v
}
maps.Copy(m, s.tags)
return m

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Clone, see above.

Comment thread testutil/accumulator.go
Comment on lines 113 to +114
tagsCopy := make(map[string]string, len(tags))
for k, v := range tags {
tagsCopy[k] = v
}
maps.Copy(tagsCopy, tags)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Clone.

Comment thread testutil/accumulator.go
Comment on lines 116 to +117
fieldsCopy := make(map[string]any, len(fields))
for k, v := range fields {
fieldsCopy[k] = v
}
maps.Copy(fieldsCopy, fields)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Clone.

@SungJin1212

Copy link
Copy Markdown
Contributor Author

@srebhan
I updated your comments to the latest commit.

But baseTags() is a method of the client interface, so I didn't remove it.

Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
@telegraf-tiger

Copy link
Copy Markdown
Contributor

Download PR build artifacts for linux_amd64.tar.gz, darwin_arm64.tar.gz, and windows_amd64.zip.
Downloads for additional architectures and packages are available below.

⚠️ This pull request increases the Telegraf binary size by 6.33 % for linux amd64 (new size: 327.7 MB, nightly size 308.2 MB)

📦 Click here to get additional PR build artifacts

Artifact URLs

. DEB . RPM . TAR . GZ . ZIP
amd64.deb aarch64.rpm darwin_amd64.tar.gz windows_amd64.zip
arm64.deb armel.rpm darwin_arm64.tar.gz windows_arm64.zip
armel.deb armv6hl.rpm freebsd_amd64.tar.gz windows_i386.zip
armhf.deb i386.rpm freebsd_armv7.tar.gz
i386.deb ppc64le.rpm freebsd_i386.tar.gz
mips.deb riscv64.rpm linux_amd64.tar.gz
mipsel.deb s390x.rpm linux_arm64.tar.gz
ppc64el.deb x86_64.rpm linux_armel.tar.gz
riscv64.deb linux_armhf.tar.gz
s390x.deb linux_i386.tar.gz
linux_mips.tar.gz
linux_mipsel.tar.gz
linux_ppc64le.tar.gz
linux_riscv64.tar.gz
linux_s390x.tar.gz

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

Labels

chore ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants