PERF: Only load the current user's topic_user for bookmarks list (#17873)

Previously, for every bookmarked topic, all topic_user records were being preloaded. Only the current user's record is actually required.

This commit introduces a new `perform_custom_preload!` API which bookmarkables can use to add custom preloading logic. We use this in topic_bookmarkable to load just the topic_user data we need (in the same way as `topic_list.rb`).

Co-authored-by: Blake Erickson <o.blakeerickson@gmail.com>
This commit is contained in:
David Taylor
2022-08-17 02:40:24 +01:00
committed by GitHub
parent 2e09a88a29
commit 913db5d546
9 changed files with 135 additions and 84 deletions

View File

@@ -15,7 +15,6 @@ class UserPostTopicBookmarkBaseSerializer < UserBookmarkBaseSerializer
:archived,
:archetype,
:highest_post_number,
:last_read_post_number,
:bumped_at,
:slug
@@ -51,10 +50,6 @@ class UserPostTopicBookmarkBaseSerializer < UserBookmarkBaseSerializer
scope.is_whisperer? ? topic.highest_staff_post_number : topic.highest_post_number
end
def last_read_post_number
topic_user&.last_read_post_number
end
def bumped_at
topic.bumped_at
end
@@ -62,10 +57,4 @@ class UserPostTopicBookmarkBaseSerializer < UserBookmarkBaseSerializer
def slug
topic.slug
end
private
def topic_user
@topic_user ||= topic.topic_users.find { |tu| tu.user_id == scope.user.id }
end
end

View File

@@ -4,6 +4,9 @@ class UserTopicBookmarkSerializer < UserPostTopicBookmarkBaseSerializer
# it does not matter what the linked post number is for topic bookmarks,
# on the client we always take the user to the last unread post in the
# topic when the bookmark URL is clicked
attributes :last_read_post_number
def linked_post_number
1
end
@@ -63,9 +66,17 @@ class UserTopicBookmarkSerializer < UserPostTopicBookmarkBaseSerializer
end
end
def last_read_post_number
topic_user&.last_read_post_number
end
private
def topic
object.bookmarkable
end
def topic_user
topic.user_data
end
end