Skip to content

Commit fe7f039

Browse files
jub0bsgopherbot
authored andcommitted
publicsuffix: spruce up code gen and speed up PublicSuffix
Rely on functions from the slices package where convenient. Drop custom max functions in favor of max builtin. Remove unused non-exported functions. 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 Change-Id: Id72967560884d98a5c0791ccea73dbb27d120c2c GitHub-Last-Rev: 87567e7 GitHub-Pull-Request: #233 Reviewed-on: https://go-review.googlesource.com/c/net/+/652236 Reviewed-by: Damien Neil <dneil@google.com> Commit-Queue: Ian Lance Taylor <iant@golang.org> Auto-Submit: Ian Lance Taylor <iant@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Pratt <mpratt@google.com>
1 parent 459513d commit fe7f039

File tree

2 files changed

+28
-70
lines changed

2 files changed

+28
-70
lines changed

publicsuffix/gen.go

Lines changed: 14 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package main
2121
import (
2222
"bufio"
2323
"bytes"
24+
"cmp"
2425
"encoding/binary"
2526
"flag"
2627
"fmt"
@@ -29,7 +30,7 @@ import (
2930
"net/http"
3031
"os"
3132
"regexp"
32-
"sort"
33+
"slices"
3334
"strings"
3435

3536
"golang.org/x/net/idna"
@@ -62,39 +63,13 @@ var (
6263
maxLo uint32
6364
)
6465

65-
func max(a, b int) int {
66-
if a < b {
67-
return b
68-
}
69-
return a
70-
}
71-
72-
func u32max(a, b uint32) uint32 {
73-
if a < b {
74-
return b
75-
}
76-
return a
77-
}
78-
7966
const (
8067
nodeTypeNormal = 0
8168
nodeTypeException = 1
8269
nodeTypeParentOnly = 2
8370
numNodeType = 3
8471
)
8572

86-
func nodeTypeStr(n int) string {
87-
switch n {
88-
case nodeTypeNormal:
89-
return "+"
90-
case nodeTypeException:
91-
return "!"
92-
case nodeTypeParentOnly:
93-
return "o"
94-
}
95-
panic("unreachable")
96-
}
97-
9873
const (
9974
defaultURL = "https://publicsuffix.org/list/effective_tld_names.dat"
10075
gitCommitURL = "https://api.github.com/repos/publicsuffix/list/commits?path=public_suffix_list.dat"
@@ -251,7 +226,7 @@ func main1() error {
251226
for label := range labelsMap {
252227
labelsList = append(labelsList, label)
253228
}
254-
sort.Strings(labelsList)
229+
slices.Sort(labelsList)
255230

256231
combinedText = combineText(labelsList)
257232
if combinedText == "" {
@@ -509,15 +484,13 @@ func (n *node) child(label string) *node {
509484
icann: true,
510485
}
511486
n.children = append(n.children, c)
512-
sort.Sort(byLabel(n.children))
487+
slices.SortFunc(n.children, byLabel)
513488
return c
514489
}
515490

516-
type byLabel []*node
517-
518-
func (b byLabel) Len() int { return len(b) }
519-
func (b byLabel) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
520-
func (b byLabel) Less(i, j int) bool { return b[i].label < b[j].label }
491+
func byLabel(a, b *node) int {
492+
return strings.Compare(a.label, b.label)
493+
}
521494

522495
var nextNodesIndex int
523496

@@ -557,7 +530,7 @@ func assignIndexes(n *node) error {
557530
n.childrenIndex = len(childrenEncoding)
558531
lo := uint32(n.firstChild)
559532
hi := lo + uint32(len(n.children))
560-
maxLo, maxHi = u32max(maxLo, lo), u32max(maxHi, hi)
533+
maxLo, maxHi = max(maxLo, lo), max(maxHi, hi)
561534
if lo >= 1<<childrenBitsLo {
562535
return fmt.Errorf("children lo %d is too large, or childrenBitsLo is too small", lo)
563536
}
@@ -586,20 +559,6 @@ func printNodeLabel(w io.Writer, n *node) error {
586559
return nil
587560
}
588561

589-
func icannStr(icann bool) string {
590-
if icann {
591-
return "I"
592-
}
593-
return " "
594-
}
595-
596-
func wildcardStr(wildcard bool) string {
597-
if wildcard {
598-
return "*"
599-
}
600-
return " "
601-
}
602-
603562
// combineText combines all the strings in labelsList to form one giant string.
604563
// Overlapping strings will be merged: "arpa" and "parliament" could yield
605564
// "arparliament".
@@ -616,18 +575,15 @@ func combineText(labelsList []string) string {
616575
return text
617576
}
618577

619-
type byLength []string
620-
621-
func (s byLength) Len() int { return len(s) }
622-
func (s byLength) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
623-
func (s byLength) Less(i, j int) bool { return len(s[i]) < len(s[j]) }
578+
func byLength(a, b string) int {
579+
return cmp.Compare(len(a), len(b))
580+
}
624581

625582
// removeSubstrings returns a copy of its input with any strings removed
626583
// that are substrings of other provided strings.
627584
func removeSubstrings(input []string) []string {
628-
// Make a copy of input.
629-
ss := append(make([]string, 0, len(input)), input...)
630-
sort.Sort(byLength(ss))
585+
ss := slices.Clone(input)
586+
slices.SortFunc(ss, byLength)
631587

632588
for i, shortString := range ss {
633589
// For each string, only consider strings higher than it in sort order, i.e.
@@ -641,7 +597,7 @@ func removeSubstrings(input []string) []string {
641597
}
642598

643599
// Remove the empty strings.
644-
sort.Strings(ss)
600+
slices.Sort(ss)
645601
for len(ss) > 0 && ss[0] == "" {
646602
ss = ss[1:]
647603
}

publicsuffix/list.go

Lines changed: 14 additions & 12 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy