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

publicsuffix: spruce up code gen and speed up PublicSuffix #233

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 14 additions & 58 deletions publicsuffix/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package main
import (
"bufio"
"bytes"
"cmp"
"encoding/binary"
"flag"
"fmt"
Expand All @@ -29,7 +30,7 @@ import (
"net/http"
"os"
"regexp"
"sort"
"slices"
"strings"

"golang.org/x/net/idna"
Expand Down Expand Up @@ -62,39 +63,13 @@ var (
maxLo uint32
)

func max(a, b int) int {
if a < b {
return b
}
return a
}

func u32max(a, b uint32) uint32 {
if a < b {
return b
}
return a
}

const (
nodeTypeNormal = 0
nodeTypeException = 1
nodeTypeParentOnly = 2
numNodeType = 3
)

func nodeTypeStr(n int) string {
switch n {
case nodeTypeNormal:
return "+"
case nodeTypeException:
return "!"
case nodeTypeParentOnly:
return "o"
}
panic("unreachable")
}

const (
defaultURL = "https://publicsuffix.org/list/effective_tld_names.dat"
gitCommitURL = "https://api.github.com/repos/publicsuffix/list/commits?path=public_suffix_list.dat"
Expand Down Expand Up @@ -251,7 +226,7 @@ func main1() error {
for label := range labelsMap {
labelsList = append(labelsList, label)
}
sort.Strings(labelsList)
slices.Sort(labelsList)

combinedText = combineText(labelsList)
if combinedText == "" {
Expand Down Expand Up @@ -509,15 +484,13 @@ func (n *node) child(label string) *node {
icann: true,
}
n.children = append(n.children, c)
sort.Sort(byLabel(n.children))
slices.SortFunc(n.children, byLabel)
return c
}

type byLabel []*node

func (b byLabel) Len() int { return len(b) }
func (b byLabel) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byLabel) Less(i, j int) bool { return b[i].label < b[j].label }
func byLabel(a, b *node) int {
return strings.Compare(a.label, b.label)
}

var nextNodesIndex int

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

func icannStr(icann bool) string {
if icann {
return "I"
}
return " "
}

func wildcardStr(wildcard bool) string {
if wildcard {
return "*"
}
return " "
}

// combineText combines all the strings in labelsList to form one giant string.
// Overlapping strings will be merged: "arpa" and "parliament" could yield
// "arparliament".
Expand All @@ -616,18 +575,15 @@ func combineText(labelsList []string) string {
return text
}

type byLength []string

func (s byLength) Len() int { return len(s) }
func (s byLength) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s byLength) Less(i, j int) bool { return len(s[i]) < len(s[j]) }
func byLength(a, b string) int {
return cmp.Compare(len(a), len(b))
}

// removeSubstrings returns a copy of its input with any strings removed
// that are substrings of other provided strings.
func removeSubstrings(input []string) []string {
// Make a copy of input.
ss := append(make([]string, 0, len(input)), input...)
sort.Sort(byLength(ss))
ss := slices.Clone(input)
slices.SortFunc(ss, byLength)

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

// Remove the empty strings.
sort.Strings(ss)
slices.Sort(ss)
for len(ss) > 0 && ss[0] == "" {
ss = ss[1:]
}
Expand Down
26 changes: 14 additions & 12 deletions publicsuffix/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func PublicSuffix(domain string) (publicSuffix string, icann bool) {
s, suffix, icannNode, wildcard := domain, len(domain), false, false
loop:
for {
dot := strings.LastIndex(s, ".")
dot := strings.LastIndexByte(s, '.')
if wildcard {
icann = icannNode
suffix = 1 + dot
Expand Down Expand Up @@ -129,7 +129,7 @@ loop:
}
if suffix == len(domain) {
// If no rules match, the prevailing rule is "*".
return domain[1+strings.LastIndex(domain, "."):], icann
return domain[1+strings.LastIndexByte(domain, '.'):], icann
}
return domain[suffix:], icann
}
Expand Down Expand Up @@ -178,26 +178,28 @@ func EffectiveTLDPlusOne(domain string) (string, error) {
if domain[i] != '.' {
return "", fmt.Errorf("publicsuffix: invalid public suffix %q for domain %q", suffix, domain)
}
return domain[1+strings.LastIndex(domain[:i], "."):], nil
return domain[1+strings.LastIndexByte(domain[:i], '.'):], nil
}

type uint32String string

func (u uint32String) get(i uint32) uint32 {
off := i * 4
return (uint32(u[off])<<24 |
uint32(u[off+1])<<16 |
uint32(u[off+2])<<8 |
uint32(u[off+3]))
u = u[off:] // help the compiler reduce bounds checks
return uint32(u[3]) |
uint32(u[2])<<8 |
uint32(u[1])<<16 |
uint32(u[0])<<24
}

type uint40String string

func (u uint40String) get(i uint32) uint64 {
off := uint64(i * (nodesBits / 8))
return uint64(u[off])<<32 |
uint64(u[off+1])<<24 |
uint64(u[off+2])<<16 |
uint64(u[off+3])<<8 |
uint64(u[off+4])
u = u[off:] // help the compiler reduce bounds checks
return uint64(u[4]) |
uint64(u[3])<<8 |
uint64(u[2])<<16 |
uint64(u[1])<<24 |
uint64(u[0])<<32
}