Просмотр исходного кода

perf(reader): use a minify.M singleton

There is no need to allocate a new HTMLMinifer every time a page needs to be
minified, as HTMLMinifer is thread-safe, we can using a singleton instead.
The memory overhead is negligible, as a minify.M struct is small.
jvoisin 1 день назад
Родитель
Сommit
ecd663a094
1 измененных файлов с 17 добавлено и 16 удалено
  1. 17 16
      internal/reader/processor/utils.go

+ 17 - 16
internal/reader/processor/utils.go

@@ -14,6 +14,20 @@ import (
 	"github.com/tdewolff/minify/v2/html"
 )
 
+var htmlMinifier = newHTMLMinifier()
+
+func newHTMLMinifier() *minify.M {
+	m := minify.New()
+	m.Add("text/html", &html.Minifier{
+		KeepEndTags:         true,
+		KeepQuotes:          true,
+		KeepComments:        false,
+		KeepSpecialComments: false,
+		KeepDefaultAttrVals: false,
+	})
+	return m
+}
+
 // parseISO8601Duration parses a subset of ISO8601 durations, mainly for youtube video.
 func parseISO8601Duration(duration string) (time.Duration, error) {
 	after, ok := strings.CutPrefix(duration, "PT")
@@ -60,20 +74,7 @@ func parseISO8601Duration(duration string) (time.Duration, error) {
 }
 
 func minifyContent(content string) string {
-	m := minify.New()
-
-	// Options required to avoid breaking the HTML content.
-	m.Add("text/html", &html.Minifier{
-		KeepEndTags:         true,
-		KeepQuotes:          true,
-		KeepComments:        false,
-		KeepSpecialComments: false,
-		KeepDefaultAttrVals: false,
-	})
-
-	if minifiedHTML, err := m.String("text/html", content); err == nil {
-		content = minifiedHTML
-	}
-
-	return content
+	// when an error occurs, String returns the original content.
+	ret, _ := htmlMinifier.String("text/html", content)
+	return ret
 }