[chore/performance] Further reduce nil uncached queries (#3267)

* [chore/performance] Further reduce nil uncached queries

* more checks
This commit is contained in:
tobi 2024-09-02 18:15:12 +02:00 committed by GitHub
parent 0560c5ce89
commit 7b7659f1fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 62 additions and 13 deletions

View file

@ -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)).

View file

@ -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 (f *filterDB) getFilterKeywords(ctx context.Context, idColumn string, id st
return false
})
return uncachedFilterKeywords, nil
return filterKeywords, nil
},
)
if err != nil {

View file

@ -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 {

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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 {