Skip to content

Commit 87567e7

Browse files
author
Julien Cretel
committed
publicsuffix: speed up PublicSuffix
Reduce the number of bounds checks. Replace calls to strings.LastIndex by calls to strings.LastIndexByte. goos: darwin goarch: amd64 pkg: golang.org/x/net/publicsuffix cpu: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz │ old │ new │ │ sec/op │ sec/op vs base │ PublicSuffix-8 13.46µ ± 0% 13.23µ ± 0% -1.67% (p=0.000 n=20) │ old │ new │ │ B/op │ B/op vs base │ PublicSuffix-8 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=20) ¹ ¹ all samples are equal │ old │ new │ │ allocs/op │ allocs/op vs base │ PublicSuffix-8 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=20) ¹ ¹ all samples are equal
1 parent 395867e commit 87567e7

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

publicsuffix/list.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func PublicSuffix(domain string) (publicSuffix string, icann bool) {
8888
s, suffix, icannNode, wildcard := domain, len(domain), false, false
8989
loop:
9090
for {
91-
dot := strings.LastIndex(s, ".")
91+
dot := strings.LastIndexByte(s, '.')
9292
if wildcard {
9393
icann = icannNode
9494
suffix = 1 + dot
@@ -129,7 +129,7 @@ loop:
129129
}
130130
if suffix == len(domain) {
131131
// If no rules match, the prevailing rule is "*".
132-
return domain[1+strings.LastIndex(domain, "."):], icann
132+
return domain[1+strings.LastIndexByte(domain, '.'):], icann
133133
}
134134
return domain[suffix:], icann
135135
}
@@ -178,26 +178,28 @@ func EffectiveTLDPlusOne(domain string) (string, error) {
178178
if domain[i] != '.' {
179179
return "", fmt.Errorf("publicsuffix: invalid public suffix %q for domain %q", suffix, domain)
180180
}
181-
return domain[1+strings.LastIndex(domain[:i], "."):], nil
181+
return domain[1+strings.LastIndexByte(domain[:i], '.'):], nil
182182
}
183183

184184
type uint32String string
185185

186186
func (u uint32String) get(i uint32) uint32 {
187187
off := i * 4
188-
return (uint32(u[off])<<24 |
189-
uint32(u[off+1])<<16 |
190-
uint32(u[off+2])<<8 |
191-
uint32(u[off+3]))
188+
u = u[off:] // help the compiler reduce bounds checks
189+
return uint32(u[3]) |
190+
uint32(u[2])<<8 |
191+
uint32(u[1])<<16 |
192+
uint32(u[0])<<24
192193
}
193194

194195
type uint40String string
195196

196197
func (u uint40String) get(i uint32) uint64 {
197198
off := uint64(i * (nodesBits / 8))
198-
return uint64(u[off])<<32 |
199-
uint64(u[off+1])<<24 |
200-
uint64(u[off+2])<<16 |
201-
uint64(u[off+3])<<8 |
202-
uint64(u[off+4])
199+
u = u[off:] // help the compiler reduce bounds checks
200+
return uint64(u[4]) |
201+
uint64(u[3])<<8 |
202+
uint64(u[2])<<16 |
203+
uint64(u[1])<<24 |
204+
uint64(u[0])<<32
203205
}

0 commit comments

Comments
 (0)