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