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

perf(rss): optimize a bit BuildFeed

Calls to urllib.AbsoluteURL take a bit less than 10% of the time spent in
parser.ParseFeed, completely parsing an url only to check if it's absolute, and
if not, to make it so.

Checking if it starts with `https://` or `http://` is usually enough to find if
an url is absolute, and if is doesn't, it's always possible to fall back to
urllib.AbsoluteURL.

This also comes with the advantage of reducing heap allocations, as most of the
time spent in urllib.AbsoluteURL is heap-related (de)allocations.
jvoisin 10 месяцев назад
Родитель
Сommit
0caadf82f2
2 измененных файлов с 6 добавлено и 3 удалено
  1. 2 2
      internal/reader/rss/adapter.go
  2. 4 1
      internal/urllib/url.go

+ 2 - 2
internal/reader/rss/adapter.go

@@ -35,8 +35,8 @@ func (r *RSSAdapter) BuildFeed(baseURL string) *model.Feed {
 	}
 
 	// Ensure the Site URL is absolute.
-	if siteURL, err := urllib.AbsoluteURL(baseURL, feed.SiteURL); err == nil {
-		feed.SiteURL = siteURL
+	if absoluteSiteURL, err := urllib.AbsoluteURL(baseURL, feed.SiteURL); err == nil {
+		feed.SiteURL = absoluteSiteURL
 	}
 
 	// Try to find the feed URL from the Atom links.

+ 4 - 1
internal/urllib/url.go

@@ -21,7 +21,10 @@ func IsAbsoluteURL(link string) bool {
 // AbsoluteURL converts the input URL as absolute URL if necessary.
 func AbsoluteURL(baseURL, input string) (string, error) {
 	if strings.HasPrefix(input, "//") {
-		input = "https://" + input[2:]
+		return "https:" + input, nil
+	}
+	if strings.HasPrefix(input, "https://") || strings.HasPrefix(input, "http://") {
+		return input, nil
 	}
 
 	u, err := url.Parse(input)