From 79c088aacbb93510255582bc2765284ee3159b41 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 8 Dec 2024 22:32:32 +0100 Subject: [PATCH] Optimize sanitizeAttributes - Use string concatenation instead of `Sprintf`, as this is much faster, and the call to `Sprintf` is responsible for 30% of the CPU time of the function - Anchor the youtube regex, to allow it to bail early, as this also account for another 30% of the CPU time. It might be worth chaining calls to `TrimPrefix` and check if the string has been trimmed instead of using a regex, to speed things up even more, but this needs to be benchmarked properly. --- internal/reader/sanitizer/sanitizer.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/reader/sanitizer/sanitizer.go b/internal/reader/sanitizer/sanitizer.go index 4608c335175..9e337075788 100644 --- a/internal/reader/sanitizer/sanitizer.go +++ b/internal/reader/sanitizer/sanitizer.go @@ -4,7 +4,6 @@ package sanitizer // import "miniflux.app/v2/internal/reader/sanitizer" import ( - "fmt" "io" "regexp" "slices" @@ -19,7 +18,7 @@ import ( ) var ( - youtubeEmbedRegex = regexp.MustCompile(`//(?:www\.)?youtube\.com/embed/(.+)$`) + youtubeEmbedRegex = regexp.MustCompile(`^(?:https?:)?//(?:www\.)?youtube\.com/embed/(.+)$`) tagAllowList = map[string][]string{ "a": {"href", "title", "id"}, "abbr": {"title"}, @@ -221,7 +220,7 @@ func sanitizeAttributes(baseURL, tagName string, attributes []html.Attribute) ([ } attrNames = append(attrNames, attribute.Key) - htmlAttrs = append(htmlAttrs, fmt.Sprintf(`%s=%q`, attribute.Key, html.EscapeString(value))) + htmlAttrs = append(htmlAttrs, attribute.Key+`="`+html.EscapeString(value)+`"`) } if !isAnchorLink {