From 7b7659f1fa9d42410c4431caea2096c30169a718 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Mon, 2 Sep 2024 18:15:12 +0200 Subject: [PATCH] [chore/performance] Further reduce nil uncached queries (#3267) * [chore/performance] Further reduce nil uncached queries * more checks --- internal/db/bundb/conversation.go | 9 ++++++++- internal/db/bundb/filterkeyword.go | 19 ++++++++++++------- internal/db/bundb/filterstatus.go | 17 ++++++++++++----- internal/db/bundb/relationship_block.go | 6 ++++++ internal/db/bundb/relationship_follow.go | 6 ++++++ internal/db/bundb/relationship_follow_req.go | 6 ++++++ internal/db/bundb/relationship_mute.go | 6 ++++++ internal/db/bundb/timeline.go | 6 ++++++ 8 files changed, 62 insertions(+), 13 deletions(-) diff --git a/internal/db/bundb/conversation.go b/internal/db/bundb/conversation.go index 5756dac59..d8245dc58 100644 --- a/internal/db/bundb/conversation.go +++ b/internal/db/bundb/conversation.go @@ -313,7 +313,14 @@ func (c *conversationDB) DeleteConversationsByOwnerAccountID(ctx context.Context return gtserror.Newf("error deleting conversations for account %s: %w", accountID, err) } - // Delete any conversation-to-status links matching the deleted conversation IDs. + if len(deletedConversationIDs) == 0 { + // Nothing + // to delete. + return nil + } + + // Delete any conversation-to-status links + // matching the deleted conversation IDs. if _, err := tx.NewDelete(). Model((*gtsmodel.ConversationToStatus)(nil)). Where("? IN (?)", bun.Ident("conversation_id"), bun.In(deletedConversationIDs)). diff --git a/internal/db/bundb/filterkeyword.go b/internal/db/bundb/filterkeyword.go index e010fbb0c..cb9958b81 100644 --- a/internal/db/bundb/filterkeyword.go +++ b/internal/db/bundb/filterkeyword.go @@ -112,20 +112,25 @@ func (f *filterDB) getFilterKeywords(ctx context.Context, idColumn string, id st // Get each filter keyword by ID from the cache or DB. filterKeywords, err := f.state.Caches.DB.FilterKeyword.LoadIDs("ID", filterKeywordIDs, - func(uncachedFilterKeywordIDs []string) ([]*gtsmodel.FilterKeyword, error) { - uncachedFilterKeywords := make([]*gtsmodel.FilterKeyword, 0, len(uncachedFilterKeywordIDs)) + func(uncached []string) ([]*gtsmodel.FilterKeyword, error) { + // Avoid querying + // if none uncached. + count := len(uncached) + if count == 0 { + return nil, nil + } - // Scan from DB. + filterKeywords := make([]*gtsmodel.FilterKeyword, 0, count) if err := f.db. NewSelect(). - Model(&uncachedFilterKeywords). - Where("? IN (?)", bun.Ident("id"), bun.In(uncachedFilterKeywordIDs)). + Model(&filterKeywords). + Where("? IN (?)", bun.Ident("id"), bun.In(uncached)). Scan(ctx); err != nil { return nil, err } // Compile all the keyword regular expressions. - uncachedFilterKeywords = slices.DeleteFunc(uncachedFilterKeywords, func(filterKeyword *gtsmodel.FilterKeyword) bool { + filterKeywords = slices.DeleteFunc(filterKeywords, func(filterKeyword *gtsmodel.FilterKeyword) bool { if err := filterKeyword.Compile(); err != nil { log.Errorf(ctx, "error compiling filter keyword regex: %v", err) return true @@ -133,7 +138,7 @@ func(uncachedFilterKeywordIDs []string) ([]*gtsmodel.FilterKeyword, error) { return false }) - return uncachedFilterKeywords, nil + return filterKeywords, nil }, ) if err != nil { diff --git a/internal/db/bundb/filterstatus.go b/internal/db/bundb/filterstatus.go index 615724e81..8256cd401 100644 --- a/internal/db/bundb/filterstatus.go +++ b/internal/db/bundb/filterstatus.go @@ -99,16 +99,23 @@ func (f *filterDB) getFilterStatuses(ctx context.Context, idColumn string, id st // Get each filter status by ID from the cache or DB. filterStatuses, err := f.state.Caches.DB.FilterStatus.LoadIDs("ID", filterStatusIDs, - func(uncachedFilterStatusIDs []string) ([]*gtsmodel.FilterStatus, error) { - uncachedFilterStatuses := make([]*gtsmodel.FilterStatus, 0, len(uncachedFilterStatusIDs)) + func(uncached []string) ([]*gtsmodel.FilterStatus, error) { + // Avoid querying + // if none uncached. + count := len(uncached) + if count == 0 { + return nil, nil + } + + filterStatuses := make([]*gtsmodel.FilterStatus, 0, count) if err := f.db. NewSelect(). - Model(&uncachedFilterStatuses). - Where("? IN (?)", bun.Ident("id"), bun.In(uncachedFilterStatusIDs)). + Model(&filterStatuses). + Where("? IN (?)", bun.Ident("id"), bun.In(uncached)). Scan(ctx); err != nil { return nil, err } - return uncachedFilterStatuses, nil + return filterStatuses, nil }, ) if err != nil { diff --git a/internal/db/bundb/relationship_block.go b/internal/db/bundb/relationship_block.go index 99d77c745..4093bad07 100644 --- a/internal/db/bundb/relationship_block.go +++ b/internal/db/bundb/relationship_block.go @@ -286,6 +286,12 @@ func (r *relationshipDB) DeleteAccountBlocks(ctx context.Context, accountID stri return err } + if len(blockIDs) == 0 { + // Nothing + // to delete. + return nil + } + defer func() { // Invalidate all account's incoming / outoing blocks on return. r.state.Caches.DB.Block.Invalidate("AccountID", accountID) diff --git a/internal/db/bundb/relationship_follow.go b/internal/db/bundb/relationship_follow.go index adf3a0161..413f3a2af 100644 --- a/internal/db/bundb/relationship_follow.go +++ b/internal/db/bundb/relationship_follow.go @@ -351,6 +351,12 @@ func (r *relationshipDB) DeleteAccountFollows(ctx context.Context, accountID str return err } + if len(followIDs) == 0 { + // Nothing + // to delete. + return nil + } + defer func() { // Invalidate all account's incoming / outoing follows on return. r.state.Caches.DB.Follow.Invalidate("AccountID", accountID) diff --git a/internal/db/bundb/relationship_follow_req.go b/internal/db/bundb/relationship_follow_req.go index a738f762b..2e058fbbb 100644 --- a/internal/db/bundb/relationship_follow_req.go +++ b/internal/db/bundb/relationship_follow_req.go @@ -387,6 +387,12 @@ func (r *relationshipDB) DeleteAccountFollowRequests(ctx context.Context, accoun return err } + if len(followReqIDs) == 0 { + // Nothing + // to delete. + return nil + } + defer func() { // Invalidate all account's incoming / outoing follow requests on return. r.state.Caches.DB.FollowRequest.Invalidate("AccountID", accountID) diff --git a/internal/db/bundb/relationship_mute.go b/internal/db/bundb/relationship_mute.go index 07ea4fa13..61b89d323 100644 --- a/internal/db/bundb/relationship_mute.go +++ b/internal/db/bundb/relationship_mute.go @@ -249,6 +249,12 @@ func (r *relationshipDB) DeleteAccountMutes(ctx context.Context, accountID strin return err } + if len(muteIDs) == 0 { + // Nothing + // to delete. + return nil + } + defer func() { // Invalidate all account's incoming / outoing mutes on return. r.state.Caches.DB.UserMute.Invalidate("AccountID", accountID) diff --git a/internal/db/bundb/timeline.go b/internal/db/bundb/timeline.go index 995c4e84f..1dc9dcf1b 100644 --- a/internal/db/bundb/timeline.go +++ b/internal/db/bundb/timeline.go @@ -351,6 +351,12 @@ func (t *timelineDB) GetListTimeline( return nil, fmt.Errorf("error getting entries for list %s: %w", listID, err) } + // If there's no list entries we can't + // possibly return anything for this list. + if len(listEntries) == 0 { + return make([]*gtsmodel.Status, 0), nil + } + // Extract just the IDs of each follow. followIDs := make([]string, 0, len(listEntries)) for _, listEntry := range listEntries {