From 0caadf82f2c488bc971352d2e42fea1c9784d799 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Mon, 9 Jun 2025 14:58:36 +0200 Subject: [PATCH] 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. --- internal/reader/rss/adapter.go | 4 ++-- internal/urllib/url.go | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/reader/rss/adapter.go b/internal/reader/rss/adapter.go index 36b39da4..273209ef 100644 --- a/internal/reader/rss/adapter.go +++ b/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. diff --git a/internal/urllib/url.go b/internal/urllib/url.go index 55230187..56d41d96 100644 --- a/internal/urllib/url.go +++ b/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)