mirror of
https://github.com/miniflux/v2.git
synced 2025-08-01 17:38:37 +00:00
refactor: 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.
This commit is contained in:
parent
30c44380e0
commit
dea46ac0ea
1 changed files with 2 additions and 3 deletions
|
@ -4,7 +4,6 @@
|
||||||
package sanitizer // import "miniflux.app/v2/internal/reader/sanitizer"
|
package sanitizer // import "miniflux.app/v2/internal/reader/sanitizer"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"regexp"
|
"regexp"
|
||||||
"slices"
|
"slices"
|
||||||
|
@ -19,7 +18,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
youtubeEmbedRegex = regexp.MustCompile(`//(?:www\.)?youtube\.com/embed/(.+)$`)
|
youtubeEmbedRegex = regexp.MustCompile(`^(?:https?:)?//(?:www\.)?youtube\.com/embed/(.+)$`)
|
||||||
tagAllowList = map[string][]string{
|
tagAllowList = map[string][]string{
|
||||||
"a": {"href", "title", "id"},
|
"a": {"href", "title", "id"},
|
||||||
"abbr": {"title"},
|
"abbr": {"title"},
|
||||||
|
@ -221,7 +220,7 @@ func sanitizeAttributes(baseURL, tagName string, attributes []html.Attribute) ([
|
||||||
}
|
}
|
||||||
|
|
||||||
attrNames = append(attrNames, attribute.Key)
|
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 {
|
if !isAnchorLink {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue