1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-26 18:21:01 +00:00

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.
This commit is contained in:
Julien Voisin 2025-08-21 03:54:49 +02:00 committed by GitHub
parent da8bf3890c
commit 9d32b23ab0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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
}