Skip to content
Permalink
Browse files

Optimize AttributeSelector ~= (whitspace separated contains)

strings.Fields unnecessarily allocates a slice - we just want to check a few
substrings and can do without allocation.

Found via
$ go test -run=none -bench=Niklas -memprofile=memprof
$ go tool pprof --web css.test memprof
  • Loading branch information...
niklasfasching committed Apr 22, 2019
1 parent bd769f6 commit 05c6523dab32351c1951ecde30b73f4121bcbaa7
Showing with 6 additions and 3 deletions.
  1. +6 −3 util.go
@@ -114,12 +114,15 @@ func attributeSelector(key, value, kind string) Selector {
switch kind {
case "~=":
s.match = func(value string) bool {
for _, v := range strings.Fields(value) {
if s.Value == v {
for {
if i := strings.IndexAny(value, " \t\r\n\f"); i == -1 {
return value == s.Value
} else if value[:i] == s.Value {
return true
} else {
value = value[i+1:]
}
}
return false
}
case "|=":
s.match = func(value string) bool { return s.Value == value || strings.HasPrefix(value, s.Value+"-") }

0 comments on commit 05c6523

Please sign in to comment.
You can’t perform that action at this time.