diff options
| author | 2025-10-15 13:32:02 +0200 | |
|---|---|---|
| committer | 2025-10-17 15:33:35 +0200 | |
| commit | 2bdff66f0a12a16684e5d25bcace551446ec1c78 (patch) | |
| tree | 4a667a71f97d6d2d52d5b3dae1e56d74192219b3 /internal/db/bundb/timeline.go | |
| parent | [chore/performance] Use CTE for list select statuses query (#4501) (diff) | |
| download | gotosocial-2bdff66f0a12a16684e5d25bcace551446ec1c78.tar.xz | |
[performance] cache account IDs in home timeline query not in exclusive lists (#4502)
this caches the stage of the home timeline query in which we calculate which account IDs should be shown in a particular user's timeline.
Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4502
Co-authored-by: kim <grufwub@gmail.com>
Co-committed-by: kim <grufwub@gmail.com>
Diffstat (limited to 'internal/db/bundb/timeline.go')
| -rw-r--r-- | internal/db/bundb/timeline.go | 130 |
1 files changed, 68 insertions, 62 deletions
diff --git a/internal/db/bundb/timeline.go b/internal/db/bundb/timeline.go index 3b217cc5c..0e330f258 100644 --- a/internal/db/bundb/timeline.go +++ b/internal/db/bundb/timeline.go @@ -47,75 +47,18 @@ func (t *timelineDB) GetHomeTimeline(ctx context.Context, accountID string, page // of any paging parameters that selects by followings. func(q *bun.SelectQuery) (*bun.SelectQuery, error) { - // As this is the home timeline, it should be - // populated by statuses from accounts followed - // by accountID, and posts from accountID itself. - // - // So, begin by seeing who accountID follows. - // It should be a little cheaper to do this in - // a separate query like this, rather than using - // a join, since followIDs are cached in memory. - follows, err := t.state.DB.GetAccountFollows( - gtscontext.SetBarebones(ctx), - accountID, - nil, // select all - ) - if err != nil && !errors.Is(err, db.ErrNoEntries) { - return nil, gtserror.Newf("db error getting follows for account %s: %w", accountID, err) - } - - // To take account of exclusive lists, get all of - // this account's lists, so we can filter out follows - // that are in contained in exclusive lists. - lists, err := t.state.DB.GetListsByAccountID(ctx, accountID) - if err != nil && !errors.Is(err, db.ErrNoEntries) { - return nil, gtserror.Newf("db error getting lists for account %s: %w", accountID, err) - } - - // Index all follow IDs that fall in exclusive lists. - ignoreFollowIDs := make(map[string]struct{}) - for _, list := range lists { - if !*list.Exclusive { - // Not exclusive, - // we don't care. - continue - } - - // Fetch all follow IDs of the entries ccontained in this list. - listFollowIDs, err := t.state.DB.GetFollowIDsInList(ctx, list.ID, nil) - if err != nil && !errors.Is(err, db.ErrNoEntries) { - return nil, gtserror.Newf("db error getting list entry follow ids: %w", err) - } - - // Exclusive list, index all its follow IDs. - for _, followID := range listFollowIDs { - ignoreFollowIDs[followID] = struct{}{} - } - } - - // Extract just the accountID from each follow, - // ignoring follows that are in exclusive lists. - targetAccountIDs := make([]string, 0, len(follows)+1) - for _, f := range follows { - _, ignore := ignoreFollowIDs[f.ID] - if !ignore { - targetAccountIDs = append( - targetAccountIDs, - f.TargetAccountID, - ) - } + // Get account IDs that should be in this home timeline. + accountIDs, err := t.getHomeAccountIDs(ctx, accountID) + if err != nil { + return nil, gtserror.Newf("error getting home account ids: %w", err) } - // Add accountID itself as a pseudo follow so that - // accountID can see its own posts in the timeline. - targetAccountIDs = append(targetAccountIDs, accountID) - // Select only statuses authored by // accounts with IDs in the slice. q = q.Where( "? IN (?)", bun.Ident("account_id"), - bun.In(targetAccountIDs), + bun.In(accountIDs), ) // Only include statuses that aren't pending approval. @@ -309,6 +252,69 @@ func (t *timelineDB) GetTagTimeline(ctx context.Context, tagID string, page *pag ) } +func (t *timelineDB) getHomeAccountIDs(ctx context.Context, accountID string) ([]string, error) { + return t.state.Caches.DB.HomeAccountIDs.Load(accountID, func() ([]string, error) { + // As this is the home timeline, it should be + // populated by statuses from accounts followed + // by accountID, and posts from accountID itself. + // So, begin by seeing who accountID follows. + follows, err := t.state.DB.GetAccountFollows( + gtscontext.SetBarebones(ctx), + accountID, + nil, // select all + ) + if err != nil && !errors.Is(err, db.ErrNoEntries) { + return nil, gtserror.Newf("db error getting follows for account %s: %w", accountID, err) + } + + // To take account of exclusive lists, get all of this account's + // lists, so we can filter out follows that are in exclusive lists. + lists, err := t.state.DB.GetListsByAccountID(ctx, accountID) + if err != nil && !errors.Is(err, db.ErrNoEntries) { + return nil, gtserror.Newf("db error getting lists for account %s: %w", accountID, err) + } + + // Index all follow IDs that fall in exclusive lists. + ignoreFollowIDs := make(map[string]struct{}) + for _, list := range lists { + if !*list.Exclusive { + // Not exclusive, + // we don't care. + continue + } + + // Fetch all follow IDs of the entries ccontained in this list. + listFollowIDs, err := t.state.DB.GetFollowIDsInList(ctx, list.ID, nil) + if err != nil && !errors.Is(err, db.ErrNoEntries) { + return nil, gtserror.Newf("db error getting list entry follow ids: %w", err) + } + + // Exclusive list, index all its follow IDs. + for _, followID := range listFollowIDs { + ignoreFollowIDs[followID] = struct{}{} + } + } + + // Extract just the accountID from each follow, + // ignoring follows that are in exclusive lists. + targetAccountIDs := make([]string, 0, len(follows)+1) + for _, f := range follows { + _, ignore := ignoreFollowIDs[f.ID] + if !ignore { + targetAccountIDs = append( + targetAccountIDs, + f.TargetAccountID, + ) + } + } + + // Add accountID itself as a pseudo follow so that + // accountID can see its own posts in the timeline. + targetAccountIDs = append(targetAccountIDs, accountID) + return targetAccountIDs, nil + }) +} + func loadStatusTimelinePage( ctx context.Context, db *bun.DB, |
