Skip to content

Commit

Permalink
[chore/performance] Avoid unnecessary "uncached" queries (#3265)
Browse files Browse the repository at this point in the history
* [chore/performance] Avoid unnecessary "uncached" queries

* go fmt
  • Loading branch information
tsmethurst authored Sep 2, 2024
1 parent f924297 commit 25a815a
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 21 deletions.
9 changes: 8 additions & 1 deletion internal/db/bundb/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,15 @@ func (a *accountDB) GetAccountsByIDs(ctx context.Context, ids []string) ([]*gtsm
accounts, err := a.state.Caches.DB.Account.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Account, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached accounts.
accounts := make([]*gtsmodel.Account, 0, len(uncached))
accounts := make([]*gtsmodel.Account, 0, count)

// Perform database query scanning
// the remaining (uncached) account IDs.
Expand Down
9 changes: 8 additions & 1 deletion internal/db/bundb/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,15 @@ func (a *applicationDB) GetAllTokens(ctx context.Context) ([]*gtsmodel.Token, er
tokens, err := a.state.Caches.DB.Token.LoadIDs("ID",
tokenIDs,
func(uncached []string) ([]*gtsmodel.Token, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached tokens.
tokens := make([]*gtsmodel.Token, 0, len(uncached))
tokens := make([]*gtsmodel.Token, 0, count)

// Perform database query scanning
// the remaining (uncached) token IDs.
Expand Down
9 changes: 8 additions & 1 deletion internal/db/bundb/conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,15 @@ func (c *conversationDB) getConversationsByLastStatusIDs(
accountID,
conversationLastStatusIDs,
func(accountID string, uncached []string) ([]*gtsmodel.Conversation, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached conversations.
conversations := make([]*gtsmodel.Conversation, 0, len(uncached))
conversations := make([]*gtsmodel.Conversation, 0, count)

// Perform database query scanning the remaining (uncached) IDs.
if err := c.db.NewSelect().
Expand Down
18 changes: 16 additions & 2 deletions internal/db/bundb/emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,15 @@ func (e *emojiDB) GetEmojisByIDs(ctx context.Context, ids []string) ([]*gtsmodel
emojis, err := e.state.Caches.DB.Emoji.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Emoji, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached emojis.
emojis := make([]*gtsmodel.Emoji, 0, len(uncached))
emojis := make([]*gtsmodel.Emoji, 0, count)

// Perform database query scanning
// the remaining (uncached) IDs.
Expand Down Expand Up @@ -650,8 +657,15 @@ func (e *emojiDB) GetEmojiCategoriesByIDs(ctx context.Context, ids []string) ([]
categories, err := e.state.Caches.DB.EmojiCategory.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.EmojiCategory, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached categories.
categories := make([]*gtsmodel.EmojiCategory, 0, len(uncached))
categories := make([]*gtsmodel.EmojiCategory, 0, count)

// Perform database query scanning
// the remaining (uncached) IDs.
Expand Down
18 changes: 16 additions & 2 deletions internal/db/bundb/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,15 @@ func (l *listDB) GetListsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.L
lists, err := l.state.Caches.DB.List.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.List, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached lists.
lists := make([]*gtsmodel.List, 0, len(uncached))
lists := make([]*gtsmodel.List, 0, count)

// Perform database query scanning
// the remaining (uncached) IDs.
Expand Down Expand Up @@ -400,8 +407,15 @@ func (l *listDB) GetListEntriesByIDs(ctx context.Context, ids []string) ([]*gtsm
entries, err := l.state.Caches.DB.ListEntry.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.ListEntry, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached entries.
entries := make([]*gtsmodel.ListEntry, 0, len(uncached))
entries := make([]*gtsmodel.ListEntry, 0, count)

// Perform database query scanning
// the remaining (uncached) IDs.
Expand Down
9 changes: 8 additions & 1 deletion internal/db/bundb/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,15 @@ func (m *mediaDB) GetAttachmentsByIDs(ctx context.Context, ids []string) ([]*gts
media, err := m.state.Caches.DB.Media.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.MediaAttachment, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached media attachments.
media := make([]*gtsmodel.MediaAttachment, 0, len(uncached))
media := make([]*gtsmodel.MediaAttachment, 0, count)

// Perform database query scanning
// the remaining (uncached) IDs.
Expand Down
9 changes: 8 additions & 1 deletion internal/db/bundb/mention.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,15 @@ func (m *mentionDB) GetMentions(ctx context.Context, ids []string) ([]*gtsmodel.
mentions, err := m.state.Caches.DB.Mention.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Mention, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached mentions.
mentions := make([]*gtsmodel.Mention, 0, len(uncached))
mentions := make([]*gtsmodel.Mention, 0, count)

// Perform database query scanning
// the remaining (uncached) IDs.
Expand Down
8 changes: 5 additions & 3 deletions internal/db/bundb/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,15 @@ func (n *notificationDB) GetNotificationsByIDs(ctx context.Context, ids []string
notifs, err := n.state.Caches.DB.Notification.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Notification, error) {
// Skip query if everything was cached.
if len(uncached) == 0 {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached notifications.
notifs := make([]*gtsmodel.Notification, 0, len(uncached))
notifs := make([]*gtsmodel.Notification, 0, count)

// Perform database query scanning
// the remaining (uncached) IDs.
Expand Down
9 changes: 8 additions & 1 deletion internal/db/bundb/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,15 @@ func (p *pollDB) GetPollVotes(ctx context.Context, pollID string) ([]*gtsmodel.P
votes, err := p.state.Caches.DB.PollVote.LoadIDs("ID",
voteIDs,
func(uncached []string) ([]*gtsmodel.PollVote, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached votes.
votes := make([]*gtsmodel.PollVote, 0, len(uncached))
votes := make([]*gtsmodel.PollVote, 0, count)

// Perform database query scanning
// the remaining (uncached) IDs.
Expand Down
9 changes: 8 additions & 1 deletion internal/db/bundb/relationship_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,15 @@ func (r *relationshipDB) GetBlocksByIDs(ctx context.Context, ids []string) ([]*g
blocks, err := r.state.Caches.DB.Block.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Block, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached blocks.
blocks := make([]*gtsmodel.Block, 0, len(uncached))
blocks := make([]*gtsmodel.Block, 0, count)

// Perform database query scanning
// the remaining (uncached) IDs.
Expand Down
9 changes: 8 additions & 1 deletion internal/db/bundb/relationship_follow.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,15 @@ func (r *relationshipDB) GetFollowsByIDs(ctx context.Context, ids []string) ([]*
follows, err := r.state.Caches.DB.Follow.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Follow, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached follows.
follows := make([]*gtsmodel.Follow, 0, len(uncached))
follows := make([]*gtsmodel.Follow, 0, count)

// Perform database query scanning
// the remaining (uncached) IDs.
Expand Down
9 changes: 8 additions & 1 deletion internal/db/bundb/relationship_follow_req.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,15 @@ func (r *relationshipDB) GetFollowRequestsByIDs(ctx context.Context, ids []strin
follows, err := r.state.Caches.DB.FollowRequest.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.FollowRequest, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached followReqs.
follows := make([]*gtsmodel.FollowRequest, 0, len(uncached))
follows := make([]*gtsmodel.FollowRequest, 0, count)

// Perform database query scanning
// the remaining (uncached) IDs.
Expand Down
9 changes: 8 additions & 1 deletion internal/db/bundb/relationship_mute.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,15 @@ func (r *relationshipDB) getMutesByIDs(ctx context.Context, ids []string) ([]*gt
mutes, err := r.state.Caches.DB.UserMute.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.UserMute, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached mutes.
mutes := make([]*gtsmodel.UserMute, 0, len(uncached))
mutes := make([]*gtsmodel.UserMute, 0, count)

// Perform database query scanning
// the remaining (uncached) IDs.
Expand Down
9 changes: 8 additions & 1 deletion internal/db/bundb/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,15 @@ func (s *statusDB) GetStatusesByIDs(ctx context.Context, ids []string) ([]*gtsmo
statuses, err := s.state.Caches.DB.Status.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Status, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached statuses.
statuses := make([]*gtsmodel.Status, 0, len(uncached))
statuses := make([]*gtsmodel.Status, 0, count)

// Perform database query scanning
// the remaining (uncached) status IDs.
Expand Down
9 changes: 8 additions & 1 deletion internal/db/bundb/statusbookmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,15 @@ func (s *statusBookmarkDB) GetStatusBookmarksByIDs(ctx context.Context, ids []st
bookmarks, err := s.state.Caches.DB.StatusBookmark.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.StatusBookmark, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached bookmarks.
bookmarks := make([]*gtsmodel.StatusBookmark, 0, len(uncached))
bookmarks := make([]*gtsmodel.StatusBookmark, 0, count)

// Perform database query scanning
// the remaining (uncached) bookmarks.
Expand Down
9 changes: 8 additions & 1 deletion internal/db/bundb/statusfave.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,15 @@ func (s *statusFaveDB) GetStatusFaves(ctx context.Context, statusID string) ([]*
faves, err := s.state.Caches.DB.StatusFave.LoadIDs("ID",
faveIDs,
func(uncached []string) ([]*gtsmodel.StatusFave, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached faves.
faves := make([]*gtsmodel.StatusFave, 0, len(uncached))
faves := make([]*gtsmodel.StatusFave, 0, count)

// Perform database query scanning
// the remaining (uncached) fave IDs.
Expand Down
9 changes: 8 additions & 1 deletion internal/db/bundb/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,15 @@ func (t *tagDB) GetTags(ctx context.Context, ids []string) ([]*gtsmodel.Tag, err
tags, err := t.state.Caches.DB.Tag.LoadIDs("ID",
ids,
func(uncached []string) ([]*gtsmodel.Tag, error) {
// Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}

// Preallocate expected length of uncached tags.
tags := make([]*gtsmodel.Tag, 0, len(uncached))
tags := make([]*gtsmodel.Tag, 0, count)

// Perform database query scanning
// the remaining (uncached) IDs.
Expand Down

0 comments on commit 25a815a

Please sign in to comment.