From 19cd5fe0c33a27e860cb3cf672f9f3f6f4b8e494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20R=C3=BCger?= Date: Tue, 4 Aug 2026 03:50:45 +0200 Subject: [PATCH] plugin/hosts: pre-convert Origins to plugin.Zones in setup (#8383) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test: add benchmark cases for Request IP/Port and parseRequest Signed-off-by: Manuel Rüger * test(kubernetes): add BenchmarkServices and BenchmarkServicesHeadless Signed-off-by: Manuel Rüger * perf(hosts): pre-convert Origins to plugin.Zones in hosts setup Signed-off-by: Manuel Rüger * style: fix gofmt trailing line formatting Signed-off-by: Manuel Rüger --------- Signed-off-by: Manuel Rüger --- plugin/hosts/hosts.go | 10 ++++++-- plugin/hosts/hosts_test.go | 48 ++++++++++++++++++++++++++++++++++++++ plugin/hosts/setup.go | 1 + 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/plugin/hosts/hosts.go b/plugin/hosts/hosts.go index 3e5d94144..b47f09bdf 100644 --- a/plugin/hosts/hosts.go +++ b/plugin/hosts/hosts.go @@ -17,6 +17,8 @@ type Hosts struct { Next plugin.Handler *Hostsfile + zones plugin.Zones + Fall fall.F fallthroughUnsupported bool @@ -27,9 +29,13 @@ func (h Hosts) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) ( state := request.Request{W: w, Req: r} qname := state.Name() - answers := []dns.RR{} + var answers []dns.RR - zone := plugin.Zones(h.Origins).Matches(qname) + zones := h.zones + if zones == nil { + zones = plugin.Zones(h.Origins) + } + zone := zones.Matches(qname) if zone == "" { // PTR zones don't need to be specified in Origins. if state.QType() != dns.TypePTR { diff --git a/plugin/hosts/hosts_test.go b/plugin/hosts/hosts_test.go index 45836588a..e36871a87 100644 --- a/plugin/hosts/hosts_test.go +++ b/plugin/hosts/hosts_test.go @@ -5,6 +5,7 @@ import ( "strings" "testing" + "github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin/pkg/dnstest" "github.com/coredns/coredns/plugin/pkg/fall" "github.com/coredns/coredns/plugin/test" @@ -211,3 +212,50 @@ const hostsExample = ` reload 5s timeout 3600 ` + +func BenchmarkHostsBaseline(b *testing.B) { + h := Hosts{ + Next: test.NextHandler(dns.RcodeNameError, nil), + Hostsfile: &Hostsfile{ + Origins: []string{"example.org."}, + hmap: newMap(), + inline: newMap(), + options: newOptions(), + }, + } + h.hmap = h.parse(strings.NewReader(hostsExample)) + + m := new(dns.Msg) + m.SetQuestion("example.org.", dns.TypeA) + rec := dnstest.NewRecorder(&test.ResponseWriter{}) + ctx := context.Background() + + b.ReportAllocs() + for b.Loop() { + _, _ = h.ServeDNS(ctx, rec, m) + } +} + +func BenchmarkHostsOptimized(b *testing.B) { + h := Hosts{ + Next: test.NextHandler(dns.RcodeNameError, nil), + Hostsfile: &Hostsfile{ + Origins: []string{"example.org."}, + hmap: newMap(), + inline: newMap(), + options: newOptions(), + }, + zones: plugin.Zones([]string{"example.org."}), + } + h.hmap = h.parse(strings.NewReader(hostsExample)) + + m := new(dns.Msg) + m.SetQuestion("example.org.", dns.TypeA) + rec := dnstest.NewRecorder(&test.ResponseWriter{}) + ctx := context.Background() + + b.ReportAllocs() + for b.Loop() { + _, _ = h.ServeDNS(ctx, rec, m) + } +} diff --git a/plugin/hosts/setup.go b/plugin/hosts/setup.go index c2ae44293..b72cac2c3 100644 --- a/plugin/hosts/setup.go +++ b/plugin/hosts/setup.go @@ -107,6 +107,7 @@ func hostsParse(c *caddy.Controller) (Hosts, error) { } h.Origins = plugin.OriginsFromArgsOrServerBlock(args, c.ServerBlockKeys) + h.zones = plugin.Zones(h.Origins) for c.NextBlock() { switch c.Val() {