rewrite/cname: point to config instead of copying (#8568)

Signed-off-by: Amila Senadheera <amilaruk1995@gmail.com>
This commit is contained in:
Amila Senadheera
2026-09-24 05:32:02 +05:30
committed by GitHub
parent 6cb01361d7
commit 95484f76ed
2 changed files with 30 additions and 4 deletions

View File

@@ -269,7 +269,7 @@ func TestCNAMETargetRewrite_upstreamFailurePaths(t *testing.T) {
req.SetQuestion("bad.test.", dns.TypeA)
state := request.Request{Req: req}
rrState := &cnameTargetRuleWithReqState{rule: rule, state: state, ctx: context.Background()}
rrState := &cnameTargetRuleWithReqState{rule: &rule, state: state, ctx: context.Background()}
res := new(dns.Msg)
res.SetReply(req)
@@ -287,6 +287,30 @@ func TestCNAMETargetRewrite_upstreamFailurePaths(t *testing.T) {
}
}
// BenchmarkCNAMETargetRuleRewrite isolates cnameTargetRule.Rewrite - the call
// that used to copy the whole shared rule struct by value into
// cnameTargetRuleWithReqState on every matching request. It deliberately
// excludes RewriteResponse/the upstream lookup, whose own allocations would
// dwarf the struct copy and hide the difference this benchmark exists to show.
func BenchmarkCNAMETargetRuleRewrite(b *testing.B) {
rule, err := newCNAMERule("stop", "exact", "def.example.com", "xyz.example.com")
if err != nil {
b.Fatal(err)
}
rule.(*cnameTargetRule).Upstream = &MockedUpstream{}
ctx := b.Context()
m := new(dns.Msg)
m.SetQuestion("abc.example.com.", dns.TypeA)
state := request.Request{Req: m}
b.ReportAllocs()
for b.Loop() {
rules, _ := rule.Rewrite(ctx, state)
rulesSink = rules
}
}
func TestNewCNAMERuleLargeRegex(t *testing.T) {
largeRegex := strings.Repeat("a", maxRegexpLen+1)
_, err := newCNAMERule("stop", "regex", largeRegex, "replacement")