Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve IsFQDN performance #1453

Merged
merged 2 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 13 additions & 7 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,24 @@ func IsMsg(buf []byte) error {

// IsFqdn checks if a domain name is fully qualified.
func IsFqdn(s string) bool {
s2 := strings.TrimSuffix(s, ".")
if s == s2 {
// Check for (and remove) a trailing dot, returning if there isn't one.
tmthrgd marked this conversation as resolved.
Show resolved Hide resolved
if s == "" || s[len(s)-1] != '.' {
return false
}
s = s[:len(s)-1]
tmthrgd marked this conversation as resolved.
Show resolved Hide resolved

i := strings.LastIndexFunc(s2, func(r rune) bool {
// If we don't have an escape sequence before the final dot, we know it's
// fully qualified and can return here.
if s == "" || s[len(s)-1] != '\\' {
tmthrgd marked this conversation as resolved.
Show resolved Hide resolved
return true
}

// Otherwise we have to check if the dot is escaped or not by checking if
// the escape sequences before the dot is odd or even.
i := strings.LastIndexFunc(s, func(r rune) bool {
return r != '\\'
})

// Test whether we have an even number of escape sequences before
// the dot or none.
return (len(s2)-i)%2 != 0
return (len(s)-i)%2 != 0
tmthrgd marked this conversation as resolved.
Show resolved Hide resolved
}

// IsRRset checks if a set of RRs is a valid RRset as defined by RFC 2181.
Expand Down
46 changes: 46 additions & 0 deletions dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,49 @@ func TestTKEY(t *testing.T) {
t.Fatalf("unable to parse TKEY string: %s", newError)
}
}

var (
sinkBool bool
sinkString string
)

func BenchmarkIsFQDN(b *testing.B) {
b.Run("no_dot", func(b *testing.B) {
var r bool
for n := 0; n < b.N; n++ {
r = IsFqdn("www.google.com")
}
sinkBool = r
})
b.Run("unescaped", func(b *testing.B) {
var r bool
for n := 0; n < b.N; n++ {
r = IsFqdn("www.google.com.")
}
sinkBool = r
})
b.Run("escaped", func(b *testing.B) {
var r bool
for n := 0; n < b.N; n++ {
r = IsFqdn(`www.google.com\\\\\\\\.`)
}
sinkBool = r
})
}

func BenchmarkFQDN(b *testing.B) {
b.Run("is_fqdn", func(b *testing.B) {
var r string
for n := 0; n < b.N; n++ {
r = Fqdn("www.google.com.")
}
sinkString = r
})
b.Run("not_fqdn", func(b *testing.B) {
var r string
for n := 0; n < b.N; n++ {
r = Fqdn("www.google.com")
}
sinkString = r
})
}