Reviewers: golang-dev1,
Message:
Hello
golan...@googlegroups.com (cc:
golan...@googlegroups.com),
I'd like you to review this change to
https://code.google.com/p/go/
Description:
net: avoid string operation and make valid domain names explicit
Having a trailing dot in the string doesn't really simplify
the checking loop in isDomainName. Avoid this unnecessary allocation.
Also make the valid domain names more explicit by adding some more
test cases.
benchmark old ns/op new ns/op delta
BenchmarkDNSNames 2420.0 983.0 -59.38%
benchmark old allocs new allocs delta
BenchmarkDNSNames 12 0 -100.00%
benchmark old bytes new bytes delta
BenchmarkDNSNames 336 0 -100.00%
Please review this at
https://codereview.appspot.com/12662043/
Affected files:
M src/pkg/net/dnsclient.go
M src/pkg/net/dnsname_test.go
Index: src/pkg/net/dnsclient.go
===================================================================
--- a/src/pkg/net/dnsclient.go
+++ b/src/pkg/net/dnsclient.go
@@ -122,12 +122,9 @@
if len(s) > 255 {
return false
}
- if s[len(s)-1] != '.' { // simplify checking loop: make name end in dot
- s += "."
- }
last := byte('.')
- ok := false // ok once we've seen a letter
+ ok := false // Ok once we've seen a letter.
partlen := 0
for i := 0; i < len(s); i++ {
c := s[i]
@@ -141,13 +138,13 @@
// fine
partlen++
case c == '-':
- // byte before dash cannot be dot
+ // Byte before dash cannot be dot.
if last == '.' {
return false
}
partlen++
case c == '.':
- // byte before dot cannot be dot, dash
+ // Byte before dot cannot be dot, dash.
if last == '.' || last == '-' {
return false
}
@@ -158,6 +155,9 @@
}
last = c
}
+ if last == '-' || partlen > 63 {
+ return false
+ }
return ok
}
Index: src/pkg/net/dnsname_test.go
===================================================================
--- a/src/pkg/net/dnsname_test.go
+++ b/src/pkg/net/dnsname_test.go
@@ -16,7 +16,6 @@
var tests = []testCase{
// RFC2181, section 11.
{"_xmpp-server._
tcp.google.com", true},
- {"_xmpp-server._
tcp.google.com", true},
{"
foo.com", true},
{"
1foo.com", true},
{"
26.0.0.73.com", true},
@@ -24,6 +23,10 @@
{"
fo1o.com", true},
{"
foo1.com", true},
{"a.b..com", false},
+ {"a.b-.com", false},
+ {"a.b.com-", false},
+ {"a.b..", false},
+ {"
b.com.", true},
}
func getTestCases(ch chan<- testCase) {
@@ -63,3 +66,21 @@
}
}
}
+
+func BenchmarkDNSNames(b *testing.B) {
+ s16 := "0123456789abcdef"
+ s65 := s16 + s16 + s16 + s16
+ s64 := s65[:64]
+ s63 := s65[:63]
+ benchmarks := append(tests, []testCase{
+ {s63, true},
+ {s64, false},
+ }...)
+ for n := 0; n < b.N; n++ {
+ for i := range benchmarks {
+ if isDomainName(benchmarks[i].name) != benchmarks[i].result {
+ b.Fatalf("isDomainName(%q)", benchmarks[i].name)
+ }
+ }
+ }
+}