Skip to content

Performance tuning for plugin/file#7658

Merged
yongtang merged 2 commits into
coredns:masterfrom
ywc689:file-perf-tuning
Nov 6, 2025
Merged

Performance tuning for plugin/file#7658
yongtang merged 2 commits into
coredns:masterfrom
ywc689:file-perf-tuning

Conversation

@ywc689

@ywc689 ywc689 commented Nov 6, 2025

Copy link
Copy Markdown
Contributor

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

The functions tree.less and nameFromRight are in the critical DNS data processing path of file plugin. Optimization of the two functions can help reduce server load and boost throughput of CoreDNS server significantly.

Note that both functions call dns.PrevLabel in a loop, and always start each call from the tail of the domain name. This patch improves the two functions performance by calling dns.PrevLabel starting from the last processed label, which dramatically reduces the cost of dns.PrevLabel.

As the benchmark results showed, the performance of tree.less is improved by about 15%,

$ go test -bench=Less -run=^$
goos: linux
goarch: amd64
pkg: github.com/coredns/coredns/plugin/file/tree
cpu: Intel(R) Xeon(R) Platinum 8336C CPU @ 2.30GHz
BenchmarkLess/base-16              99003             12105 ns/op
BenchmarkLess/optimized-16        114522             10590 ns/op
PASS
ok      github.com/coredns/coredns/plugin/file/tree     2.416s

and performance of nameFromRight with the optimization is gained by double or triple.

### Benchmark test result for the original implementation:
$ go test -bench=. -run=^$                                                                                                                  
goos: linux
goarch: amd64
pkg: github.com/coredns/coredns/plugin/file
cpu: Intel(R) Xeon(R) Platinum 8336C CPU @ 2.30GHz
BenchmarkFileLookupDNSSEC-16              173002              5902 ns/op
BenchmarkFileParseInsert-16                15434             78844 ns/op
BenchmarkFileLookup-16                    469035              2535 ns/op
BenchmarkNameFromRight/i0_origin-16     430719652                2.794 ns/op
BenchmarkNameFromRight/eq_origin_i1_shot-16             30933135                37.52 ns/op
BenchmarkNameFromRight/two_labels_i1-16                 29375857                40.71 ns/op
BenchmarkNameFromRight/two_labels_i2-16                 18556830                63.97 ns/op
BenchmarkNameFromRight/two_labels_i3_shot-16            14678812                84.73 ns/op
BenchmarkNameFromRight/ten_labels_i5-16                  8522132               133.0 ns/op
BenchmarkNameFromRight/ten_labels_i11_shot-16            3154410               378.2 ns/op
BenchmarkNameFromRight/not_subdomain_shot-16            35297224                33.59 ns/op
BenchmarkNameFromRightRandomized-16                     10638702               113.4 ns/op             0 B/op          0 allocs/op                                                                               
PASS
ok      github.com/coredns/coredns/plugin/file  14.276s

$ vim zone.go  ## apply the optimization

### Benchmark test result after optimized:
$ go test -bench=. -run=^$
goos: linux
goarch: amd64
pkg: github.com/coredns/coredns/plugin/file
cpu: Intel(R) Xeon(R) Platinum 8336C CPU @ 2.30GHz
BenchmarkFileLookupDNSSEC-16              185454              5870 ns/op
BenchmarkFileParseInsert-16                15174             78894 ns/op
BenchmarkFileLookup-16                    461064              2528 ns/op
BenchmarkNameFromRight/i0_origin-16     425864671                2.808 ns/op
BenchmarkNameFromRight/eq_origin_i1_shot-16             60903428                19.53 ns/op
BenchmarkNameFromRight/two_labels_i1-16                 50209297                24.21 ns/op
BenchmarkNameFromRight/two_labels_i2-16                 42483711                27.88 ns/op
BenchmarkNameFromRight/two_labels_i3_shot-16            40898925                29.24 ns/op
BenchmarkNameFromRight/ten_labels_i5-16                 27916532                44.54 ns/op
BenchmarkNameFromRight/ten_labels_i11_shot-16           17540040                67.59 ns/op
BenchmarkNameFromRight/not_subdomain_shot-16            67180514                17.46 ns/op
BenchmarkNameFromRightRandomized-16                     32692081                38.21 ns/op            0 B/op          0 allocs/op
PASS
ok      github.com/coredns/coredns/plugin/file  14.344s

2. Which issues (if any) are related?

nothing

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

nothing

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

no

@codecov

codecov Bot commented Nov 6, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 63.26%. Comparing base (93c57b6) to head (dfeffae).
⚠️ Report is 1726 commits behind head on master.

Files with missing lines Patch % Lines
plugin/file/zone.go 75.00% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7658      +/-   ##
==========================================
+ Coverage   55.70%   63.26%   +7.56%     
==========================================
  Files         224      278      +54     
  Lines       10016    15129    +5113     
==========================================
+ Hits         5579     9571    +3992     
- Misses       3978     4871     +893     
- Partials      459      687     +228     

☔ 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.

ywc689 and others added 2 commits November 6, 2025 11:06
PrevLabel always begins its iteration from the tail of domain name.
less(..) loop can improve its performance by calling PrevLabel starting
from the last processed label.

As the benchmark results showed, the performance is improved by about 15%.
$ go test -bench=Less -run=^$
goos: linux
goarch: amd64
pkg: github.com/coredns/coredns/plugin/file/tree
cpu: Intel(R) Xeon(R) Platinum 8336C CPU @ 2.30GHz
BenchmarkLess/base-16              99003             12105 ns/op
BenchmarkLess/optimized-16        114522             10590 ns/op
PASS
ok      github.com/coredns/coredns/plugin/file/tree     2.416s

Signed-off-by: yuwenchao <ywc689@163.com>
Similar to tree.less(..), performance of function nameFromRight
can boost by utilizing dns.PrevLabel more efficiently.

As benchmark tests shown, performance of this function with the
optimization is gained by double or triple.

* Benchmark test result for the original implementation:
BenchmarkNameFromRight/i0_origin-16     430719652                2.794 ns/op
BenchmarkNameFromRight/eq_origin_i1_shot-16             30933135                37.52 ns/op
BenchmarkNameFromRight/two_labels_i1-16                 29375857                40.71 ns/op
BenchmarkNameFromRight/two_labels_i2-16                 18556830                63.97 ns/op
BenchmarkNameFromRight/two_labels_i3_shot-16            14678812                84.73 ns/op
BenchmarkNameFromRight/ten_labels_i5-16                  8522132               133.0 ns/op
BenchmarkNameFromRight/ten_labels_i11_shot-16            3154410               378.2 ns/op
BenchmarkNameFromRight/not_subdomain_shot-16            35297224                33.59 ns/op
BenchmarkNameFromRightRandomized-16                     10638702               113.4 ns/op             0 B/op          0 allocs/op

* Benchmark test result with this optimization:
BenchmarkNameFromRight/i0_origin-16     425864671                2.808 ns/op
BenchmarkNameFromRight/eq_origin_i1_shot-16             60903428                19.53 ns/op
BenchmarkNameFromRight/two_labels_i1-16                 50209297                24.21 ns/op
BenchmarkNameFromRight/two_labels_i2-16                 42483711                27.88 ns/op
BenchmarkNameFromRight/two_labels_i3_shot-16            40898925                29.24 ns/op
BenchmarkNameFromRight/ten_labels_i5-16                 27916532                44.54 ns/op
BenchmarkNameFromRight/ten_labels_i11_shot-16           17540040                67.59 ns/op
BenchmarkNameFromRight/not_subdomain_shot-16            67180514                17.46 ns/op
BenchmarkNameFromRightRandomized-16                     32692081                38.21 ns/op            0 B/op          0 allocs/op

Signed-off-by: yuwenchao <yuwenchao@bytedance.com>
@yongtang yongtang merged commit deae7ec into coredns:master Nov 6, 2025
13 checks passed
@miekg

miekg commented Nov 12, 2025

Copy link
Copy Markdown
Member

brillant

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