소스 검색

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 11 달 전
부모
커밋
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)