Skip to content

perf: avoid string concatenation in loops#7572

Merged
yongtang merged 2 commits into
coredns:masterfrom
catenacyber:string-concat-loop
Oct 6, 2025
Merged

perf: avoid string concatenation in loops#7572
yongtang merged 2 commits into
coredns:masterfrom
catenacyber:string-concat-loop

Conversation

@catenacyber

Copy link
Copy Markdown
Contributor

1. Why is this pull request needed and what does it do?

This PR is about improving performance

It applies perfpsrint linter to remove string concatenation in loops, which has a quadratic complexity

2. Which issues (if any) are related?

None that I know

Do you have an opinion if the strings looked at here can have a big or unbounded length ? like rewriteToExpand in plugin/auto/regexp.go or maybeUnescape in plugin/route53/route53.go

3. Which documentation changes (if any) need to be made?

Behavior should remain the same

4. Does this introduce a backward incompatible change or deprecation?

It should not

Comment thread plugin/auto/regexp.go
copy := ""

var copySb12 strings.Builder
for _, c := range s {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Maybe a strings.ReplaceAll would be even better

@codecov

codecov Bot commented Sep 23, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 73.91304% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 62.57%. Comparing base (93c57b6) to head (1c41df0).
⚠️ Report is 1675 commits behind head on master.

Files with missing lines Patch % Lines
plugin/sign/keys.go 0.00% 3 Missing ⚠️
plugin/test/scrape.go 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7572      +/-   ##
==========================================
+ Coverage   55.70%   62.57%   +6.87%     
==========================================
  Files         224      278      +54     
  Lines       10016    18567    +8551     
==========================================
+ Hits         5579    11619    +6040     
- Misses       3978     6260    +2282     
- Partials      459      688     +229     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@yongtang

Copy link
Copy Markdown
Member

@catenacyber The naming of the variables like sSb36 seems to be non-conventional and may confuse people - any chance to rename them into more conventional format?

@catenacyber

Copy link
Copy Markdown
Contributor Author

catenacyber The naming of the variables like sSb36 seems to be non-conventional and may confuse people - any chance to rename them into more conventional format?

Sure, I did it in a fixup commit.

These names come from the automatic tool, that aimes to produce correct code in all cases, even if not the best code ;-)

@yongtang

Copy link
Copy Markdown
Member

Can you fix the DCO error?

@catenacyber

Copy link
Copy Markdown
Contributor Author

Commits squashed and signed off to fix DCO

@thevilledev

Copy link
Copy Markdown
Collaborator

Do you have an opinion if the strings looked at here can have a big or unbounded length ? like rewriteToExpand in plugin/auto/regexp.go or maybeUnescape in plugin/route53/route53.go

Unlikely - mostly sourced from the config file.

It applies perfpsrint linter to remove string concatenation in loops, which has a quadratic complexity

Would it be possible to also add perfsprint to .golangci.yml with the parameters you used here? Just so that all future code would go through the same set of linting rules. Minimum set of enabled parameters if possible, then extend in another PR since this one is already scoped for string concatenation in loops.

@catenacyber

Copy link
Copy Markdown
Contributor Author

Trying in a new commit on top of that

Note that concat-loop is not merged yet : catenacyber/perfsprint#58

@thevilledev

Copy link
Copy Markdown
Collaborator

Cool, lets enable it once merged (and if not enabled by default) 👍

@catenacyber can you fix DCO? Otherwise LGTM

Apply perfpsrint linter

Signed-off-by: Philippe Antoine <contact@catenacyber.fr>
Signed-off-by: Philippe Antoine <contact@catenacyber.fr>
@catenacyber

Copy link
Copy Markdown
Contributor Author

Oops... just fixed up, signed off and force-pushed...

@thevilledev nice work on tackling all oss-fuzz issues by the way 👏

@thevilledev thevilledev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you @catenacyber! 🥳 It's been fun fixing those

LGTM

@yongtang yongtang merged commit 625f6c9 into coredns:master Oct 6, 2025
13 checks passed
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.

3 participants