Commit Graph

9 Commits

Author SHA1 Message Date
Manuel Rüger
50ffa1ecf9 plugin/rewrite: drop the per-record dot-prefixed temporary in remapStringRewriter (#8382)
remapStringRewriter matches a record name against orig and its sub domains. The
sub domain check was strings.HasSuffix(src, "."+r.orig), which built the
dot-prefixed string on every call and threw it away. Match the label boundary by
index instead: src is a sub domain of orig when orig sits at the end of src with
a "." immediately before it, which is exactly what the HasSuffix call tested.

Go concatenates short strings into a 32-byte stack buffer, so "."+orig only
reached the heap once orig passed 31 bytes. Below that the temporary was free
and this saves a few ns per record. Above it, 48 B was allocated per record.
Kubernetes service names are past the threshold -
my-service.my-namespace.svc.cluster.local. is 42 bytes - and those are the names
an auto rule rewrites to when a Corefile maps an external name onto an
in-cluster one. orig is the name the question was rewritten to, so whether a
deployment sees the allocation is a property of its Corefile, not its queries.

Caching "."+orig on the rewriter instead does not work: responseRuleFor
constructs a new rewriter for every request an auto name rule rewrites, so the
concatenation would run once per request rather than once per rule, and escapes
to the heap from there. That buys per-record work with a per-request allocation
and regresses every response short enough not to amortize it.

Per record, one rewriteString call on an existing rewriter:

    name                                master          this PR
    RemapStringRewriter/short/match     186.6n 16 B/1   120.4n 16 B/1   -35%
    RemapStringRewriter/short/nomatch    61.5n  0 B/0    16.5n  0 B/0   -73%
    RemapStringRewriter/long/match      381.9n 72 B/2   165.3n 24 B/1   -57%
    RemapStringRewriter/long/nomatch    219.1n 48 B/1    18.7n  0 B/0   -91%

Per request - rewrite the question, build the response rules, apply them to the
answer:

    name                                  master           this PR
    AutoNameRuleResponse/exact             935n   96 B/4    945n   96 B/4    ~
    AutoNameRuleResponse/subdomain        1.248µ 112 B/5   1.242µ 112 B/5    ~
    AutoNameRuleResponse/nomatch          1.016µ  96 B/4    945n   96 B/4   -7%
    AutoNameRuleResponse/subdomain-8      3.376µ 224 B/12  2.891µ 224 B/12 -14%
    AutoNameRuleResponse/k8s/subdomain    1.611µ 176 B/6   1.380µ 128 B/5  -14%
    AutoNameRuleResponse/k8s/subdomain-8  5.144µ 672 B/20  3.305µ 288 B/12 -36%

benchstat over 8 runs, i7-1065G7. With short names this is flat at the request
level: one rewriteString call is small next to the four allocations that
building the rules costs. The saving is per record and per byte of name, so it
shows up where responses carry several records and the rewritten-to name is
long.

This applies to exact, prefix, substring and regex name rules with answer auto.
suffix rules build a suffixStringRewriter instead and are not affected.

TestRemapStringRewriter pins the label boundary semantics the index arithmetic
now carries, notably that notexample.com. is not a sub domain of example.com.
It passes against the previous implementation too.

Signed-off-by: Manuel Rüger <manuel@rueg.eu>
2026-08-19 20:27:19 -07:00
Ville Vesilehto
b723bd94d4 fix(plugins): add regex length limit (#7802) 2026-01-05 09:48:48 -08:00
Ville Vesilehto
39abf5aeba chore(lint): modernize Go (#7536)
Use modern Go constructs through the modernize analyzer from the
golang.org/x/tools package.

Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
2025-09-10 13:08:27 -07:00
Ondřej Benkovský
c2dbb7141a add golangci-lint linter (#5499) 2022-07-10 11:06:33 -07:00
Uwe Krueger
40edf1e566 plugin/rewrite: streamline the ResponseRule handling. (#4473)
* plugin/rewrite: streamline the ResponseRule handling.

The functionality of a response rule is now completely encapsulated behind
a `ResponseRule` interface. This significantly simplifies the complete
processing flow, it enables more flexible response handling and it is possible
to eliminate lots of state flags, ifs and switches.

Based on the new flexibility the pull request also enables to support a
response name rewrite for all name rewrite types.
To be compatible, an explicit `answer auto` option is added to support
a best effort response rewrite (name and value).
Additionally now all name rewrite rules support additional name and value
reponse rewrite options.

Using this feature it is also possible now to rewrite a complete sub domain
hierarchy to a single domain name combined with a correct rewrite (#2389).

Signed-off-by: Uwe Krueger <uwe.krueger@sap.com>

* revert policy

Signed-off-by: Uwe Krueger <uwe.krueger@sap.com>

Co-authored-by: Miek Gieben <miek@miek.nl>
2021-05-04 10:05:45 +02:00
Daniel Garcia
4a3f5cc41e Use Trim(Prefix/Suffix) instead of Trim(Left/Right) in rewrite prefix plugin (#2364) (#2372) 2018-12-06 22:10:46 +00:00
Daniel Garcia
f51c110511 Use TrimPrefix instead of TrimLeft in rewrite prefix plugin (#2364) (#2370) 2018-12-06 21:02:07 +00:00
Paul G
a9ce35ae4e plugin/rewrite: add closing dot for suffix rewrite rule (#2070)
* add closing dot for suffix rewrite rule
* improve rule syntax checks

Resolves: #1881
2018-10-23 16:59:59 -04:00
Miek Gieben
d9b9a955ba plugin/rewrite: prevent illegal names (#1972)
Log and returns an error when the name rewrite creates a name that is
illegal. Add test in name_test.go to see if an error is returned.

Possible followup could be the only check this if a name-rewrite is
done.

Fixes: #1638

Signed-off-by: Miek Gieben <miek@miek.nl>
2018-07-13 09:32:07 -04:00