perf: optimize converting releases to feed items (#7221)

- `releasesToFeedItems` is called to convert release structs to feed items, which is then used to render RSS or Atom feeds.
- Optimize the loading of attributes for the releases, introduce `ReleaseList` type which uses caching to load repository and publishers. It also no longer loads release attachments and downloads counts as that is not used in feed items.
- Optimize the composing of meta by introducing caching, this operation is especially slow when the owner is an organization.
- Add unit test (ensures new `LoadAttributes` works correctly).
- Add integration test (ensures that feed output is still as expected).

Loading https://codeberg.org/forgejo/forgejo/releases.rss reduced from ~15s to ~1s. (It is currently is deployed on codeberg.org)

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7221
Reviewed-by: Otto <otto@codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
Gusted 2025-03-17 09:00:34 +00:00 committed by Earl Warren
parent ccd87001c8
commit d5c8091e08
4 changed files with 192 additions and 8 deletions

View file

@ -0,0 +1,45 @@
// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package repo
import (
"context"
user_model "code.gitea.io/gitea/models/user"
)
type ReleaseList []*Release
// LoadAttributes loads the repository and publisher for the releases.
func (r ReleaseList) LoadAttributes(ctx context.Context) error {
repoCache := make(map[int64]*Repository)
userCache := make(map[int64]*user_model.User)
for _, release := range r {
var err error
repo, ok := repoCache[release.RepoID]
if !ok {
repo, err = GetRepositoryByID(ctx, release.RepoID)
if err != nil {
return err
}
repoCache[release.RepoID] = repo
}
release.Repo = repo
publisher, ok := userCache[release.PublisherID]
if !ok {
publisher, err = user_model.GetUserByID(ctx, release.PublisherID)
if err != nil {
if !user_model.IsErrUserNotExist(err) {
return err
}
publisher = user_model.NewGhostUser()
}
userCache[release.PublisherID] = publisher
}
release.Publisher = publisher
}
return nil
}

View file

@ -0,0 +1,42 @@
// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package repo
import (
"testing"
"code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestReleaseListLoadAttributes(t *testing.T) {
require.NoError(t, unittest.PrepareTestDatabase())
releases := ReleaseList{&Release{
RepoID: 1,
PublisherID: 1,
}, &Release{
RepoID: 2,
PublisherID: 2,
}, &Release{
RepoID: 1,
PublisherID: 2,
}, &Release{
RepoID: 2,
PublisherID: 1,
}}
require.NoError(t, releases.LoadAttributes(t.Context()))
assert.EqualValues(t, 1, releases[0].Repo.ID)
assert.EqualValues(t, 1, releases[0].Publisher.ID)
assert.EqualValues(t, 2, releases[1].Repo.ID)
assert.EqualValues(t, 2, releases[1].Publisher.ID)
assert.EqualValues(t, 1, releases[2].Repo.ID)
assert.EqualValues(t, 2, releases[2].Publisher.ID)
assert.EqualValues(t, 2, releases[3].Repo.ID)
assert.EqualValues(t, 1, releases[3].Publisher.ID)
}

View file

@ -298,14 +298,14 @@ func GetFeedType(name string, req *http.Request) (bool, string, string) {
return false, name, ""
}
// feedActionsToFeedItems convert gitea's Repo's Releases to feeds Item
func releasesToFeedItems(ctx *context.Context, releases []*repo_model.Release) (items []*feeds.Item, err error) {
for _, rel := range releases {
err := rel.LoadAttributes(ctx)
if err != nil {
// feedActionsToFeedItems convert repository releases into feed items.
func releasesToFeedItems(ctx *context.Context, releases repo_model.ReleaseList) (items []*feeds.Item, err error) {
if err := releases.LoadAttributes(ctx); err != nil {
return nil, err
}
composeCache := make(map[int64]map[string]string)
for _, rel := range releases {
var title string
var content template.HTML
@ -315,13 +315,19 @@ func releasesToFeedItems(ctx *context.Context, releases []*repo_model.Release) (
title = rel.Title
}
metas, ok := composeCache[rel.RepoID]
if !ok {
metas = rel.Repo.ComposeMetas(ctx)
composeCache[rel.RepoID] = metas
}
link := &feeds.Link{Href: rel.HTMLURL()}
content, err = markdown.RenderString(&markup.RenderContext{
Ctx: ctx,
Links: markup.Links{
Base: rel.Repo.Link(),
},
Metas: rel.Repo.ComposeMetas(ctx),
Metas: metas,
}, rel.Note)
if err != nil {
return nil, err

View file

@ -0,0 +1,91 @@
// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package integration
import (
"net/http"
"regexp"
"testing"
"code.gitea.io/gitea/tests"
"github.com/stretchr/testify/assert"
)
func TestReleaseFeed(t *testing.T) {
defer tests.PrepareTestEnv(t)()
normalize := func(body string) string {
// Remove port.
body = regexp.MustCompile(`localhost:\d+`).ReplaceAllString(body, "localhost")
// date is timezone dependent.
body = regexp.MustCompile(`<pubDate>.*</pubDate>`).ReplaceAllString(body, "<pubDate></pubDate>")
body = regexp.MustCompile(`<updated>.*</updated>`).ReplaceAllString(body, "<updated></updated>")
return body
}
t.Run("RSS feed", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
resp := MakeRequest(t, NewRequest(t, "GET", "/user2/repo1/releases.rss"), http.StatusOK)
assert.EqualValues(t, `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>Releases for user2/repo1</title>
<link>http://localhost/user2/repo1/release</link>
<description></description>
<pubDate></pubDate>
<item>
<title>pre-release</title>
<link>http://localhost/user2/repo1/releases/tag/v1.0</link>
<description></description>
<content:encoded><![CDATA[<p dir="auto">some text for a pre release</p>
]]></content:encoded>
<author>user2</author>
<guid>5: http://localhost/user2/repo1/releases/tag/v1.0</guid>
<pubDate></pubDate>
</item>
<item>
<title>testing-release</title>
<link>http://localhost/user2/repo1/releases/tag/v1.1</link>
<description></description>
<author>user2</author>
<guid>1: http://localhost/user2/repo1/releases/tag/v1.1</guid>
<pubDate></pubDate>
</item>
</channel>
</rss>`, normalize(resp.Body.String()))
})
t.Run("Atom feed", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
resp := MakeRequest(t, NewRequest(t, "GET", "/user2/repo1/releases.atom"), http.StatusOK)
assert.EqualValues(t, `<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.w3.org/2005/Atom">
<title>Releases for user2/repo1</title>
<id>http://localhost/user2/repo1/release</id>
<updated></updated>
<link href="http://localhost/user2/repo1/release"></link>
<entry>
<title>pre-release</title>
<updated></updated>
<id>5: http://localhost/user2/repo1/releases/tag/v1.0</id>
<content type="html">&lt;p dir=&#34;auto&#34;&gt;some text for a pre release&lt;/p&gt;&#xA;</content>
<link href="http://localhost/user2/repo1/releases/tag/v1.0" rel="alternate"></link>
<author>
<name>user2</name>
<email>user2@noreply.example.org</email>
</author>
</entry>
<entry>
<title>testing-release</title>
<updated></updated>
<id>1: http://localhost/user2/repo1/releases/tag/v1.1</id>
<link href="http://localhost/user2/repo1/releases/tag/v1.1" rel="alternate"></link>
<author>
<name>user2</name>
<email>user2@noreply.example.org</email>
</author>
</entry>
</feed>`, normalize(resp.Body.String()))
})
}