Bläddra i källkod

perf(sanitizer): make sanitizer ~10% faster by using `slices.Contains` instead of nested maps

```console
$ go test -bench=. -count=25 > old.txt
$ go test -bench=. -count=25 > new.txt
$ benchstat old.txt new.txt
goos: linux
goarch: arm64
pkg: miniflux.app/v2/internal/reader/sanitizer
           │   old.txt   │            new.txt            │
           │   sec/op    │   sec/op     vs base          │
Sanitize-8   21.55m ± 5%   19.64m ± 9%  ~ (p=0.059 n=25)
```

Almost a 10% improvement, yay.
Julien Voisin 7 månader sedan
förälder
incheckning
9d32b23ab0
1 ändrade filer med 28 tillägg och 29 borttagningar
  1. 28 29
      internal/reader/sanitizer/sanitizer.go

+ 28 - 29
internal/reader/sanitizer/sanitizer.go

@@ -18,71 +18,71 @@ import (
 )
 
 var (
-	allowedHTMLTagsAndAttributes = map[string]map[string]struct{}{
-		"a":          {"href": {}, "title": {}, "id": {}},
-		"abbr":       {"title": {}},
-		"acronym":    {"title": {}},
+	allowedHTMLTagsAndAttributes = map[string][]string{
+		"a":          {"href", "title", "id"},
+		"abbr":       {"title"},
+		"acronym":    {"title"},
 		"aside":      {},
-		"audio":      {"src": {}},
+		"audio":      {"src"},
 		"blockquote": {},
 		"b":          {},
 		"br":         {},
 		"caption":    {},
 		"cite":       {},
 		"code":       {},
-		"dd":         {"id": {}},
+		"dd":         {"id"},
 		"del":        {},
 		"dfn":        {},
-		"dl":         {"id": {}},
-		"dt":         {"id": {}},
+		"dl":         {"id"},
+		"dt":         {"id"},
 		"em":         {},
 		"figcaption": {},
 		"figure":     {},
-		"h1":         {"id": {}},
-		"h2":         {"id": {}},
-		"h3":         {"id": {}},
-		"h4":         {"id": {}},
-		"h5":         {"id": {}},
-		"h6":         {"id": {}},
+		"h1":         {"id"},
+		"h2":         {"id"},
+		"h3":         {"id"},
+		"h4":         {"id"},
+		"h5":         {"id"},
+		"h6":         {"id"},
 		"hr":         {},
-		"iframe":     {"width": {}, "height": {}, "frameborder": {}, "src": {}, "allowfullscreen": {}},
-		"img":        {"alt": {}, "title": {}, "src": {}, "srcset": {}, "sizes": {}, "width": {}, "height": {}, "fetchpriority": {}, "decoding": {}},
+		"iframe":     {"width", "height", "frameborder", "src", "allowfullscreen"},
+		"img":        {"alt", "title", "src", "srcset", "sizes", "width", "height", "fetchpriority", "decoding"},
 		"ins":        {},
 		"kbd":        {},
-		"li":         {"id": {}},
-		"ol":         {"id": {}},
+		"li":         {"id"},
+		"ol":         {"id"},
 		"p":          {},
 		"picture":    {},
 		"pre":        {},
-		"q":          {"cite": {}},
+		"q":          {"cite"},
 		"rp":         {},
 		"rt":         {},
 		"rtc":        {},
 		"ruby":       {},
 		"s":          {},
 		"samp":       {},
-		"source":     {"src": {}, "type": {}, "srcset": {}, "sizes": {}, "media": {}},
+		"source":     {"src", "type", "srcset", "sizes", "media"},
 		"strong":     {},
 		"sub":        {},
-		"sup":        {"id": {}},
+		"sup":        {"id"},
 		"table":      {},
-		"td":         {"rowspan": {}, "colspan": {}},
+		"td":         {"rowspan", "colspan"},
 		"tfoot":      {},
-		"th":         {"rowspan": {}, "colspan": {}},
+		"th":         {"rowspan", "colspan"},
 		"thead":      {},
-		"time":       {"datetime": {}},
+		"time":       {"datetime"},
 		"tr":         {},
 		"u":          {},
-		"ul":         {"id": {}},
+		"ul":         {"id"},
 		"var":        {},
-		"video":      {"poster": {}, "height": {}, "width": {}, "src": {}},
+		"video":      {"poster", "height", "width", "src"},
 		"wbr":        {},
 
 		// MathML: https://w3c.github.io/mathml-core/ and https://developer.mozilla.org/en-US/docs/Web/MathML/Reference/Element
 		"annotation":     {},
 		"annotation-xml": {},
 		"maction":        {},
-		"math":           {"xmlns": {}},
+		"math":           {"xmlns"},
 		"merror":         {},
 		"mfrac":          {},
 		"mi":             {},
@@ -419,8 +419,7 @@ func isValidTag(tagName string) bool {
 
 func isValidAttribute(tagName, attributeName string) bool {
 	if attributes, ok := allowedHTMLTagsAndAttributes[tagName]; ok {
-		_, allowed := attributes[attributeName]
-		return allowed
+		return slices.Contains(attributes, attributeName)
 	}
 	return false
 }