A platform for community discussion. Free, open, simple.
PERF: Eager load linkables when loading custom sidebar sections (#20490)
Alan Guo Xiang Tan8fec1a412bd1
diff --git a/app/serializers/current_user_serializer.rb b/app/serializers/current_user_serializer.rbindex a3b3df8548b..ddd2d0dfa5b 100644--- a/app/serializers/current_user_serializer.rb+++ b/app/serializers/current_user_serializer.rb@@ -79,6 +79,7 @@ class CurrentUserSerializer < BasicUserSerializerdef sidebar_sectionsSidebarSection+ .includes(sidebar_section_links: :linkable).where("public OR user_id = ?", object.id).order("(public IS TRUE) DESC").map { |section| SidebarSectionSerializer.new(section, root: false) }diff --git a/spec/fabricators/sidebar_section_link_fabricator.rb b/spec/fabricators/sidebar_section_link_fabricator.rbindex 569de894fe0..7da90f86e3a 100644--- a/spec/fabricators/sidebar_section_link_fabricator.rb+++ b/spec/fabricators/sidebar_section_link_fabricator.rb@@ -7,3 +7,8 @@ Fabricator(:category_sidebar_section_link, from: :sidebar_section_link) doendFabricator(:tag_sidebar_section_link, from: :sidebar_section_link) { linkable(fabricator: :tag) }++Fabricator(:custom_sidebar_section_link, from: :sidebar_section_link) do+ linkable(fabricator: :sidebar_url)+ sidebar_section(fabricator: :sidebar_section)+enddiff --git a/spec/fabricators/sidebar_url.rb b/spec/fabricators/sidebar_url_fabricator.rbsimilarity index 100%rename from spec/fabricators/sidebar_url.rbrename to spec/fabricators/sidebar_url_fabricator.rbdiff --git a/spec/serializers/current_user_serializer_spec.rb b/spec/serializers/current_user_serializer_spec.rbindex 27492cecbd7..b9be21cc7b5 100644--- a/spec/serializers/current_user_serializer_spec.rb+++ b/spec/serializers/current_user_serializer_spec.rb@@ -297,4 +297,53 @@ RSpec.describe CurrentUserSerializer doendinclude_examples "User Sidebar Serializer Attributes", described_class++ describe "#sidebar_sections" do+ fab!(:group) { Fabricate(:group) }+ fab!(:sidebar_section) { Fabricate(:sidebar_section, user: user) }++ before do+ group.add(user)+ SiteSetting.enable_custom_sidebar_sections = group.id+ end++ it "eager loads sidebar_urls" do+ custom_sidebar_section_link_1 =+ Fabricate(:custom_sidebar_section_link, user: user, sidebar_section: sidebar_section)++ # warmup+ described_class.new(user, scope: Guardian.new(user), root: false).as_json++ initial_count =+ track_sql_queries do+ serialized = described_class.new(user, scope: Guardian.new(user), root: false).as_json++ expect(serialized[:sidebar_sections].map { |sidebar_section| sidebar_section.id }).to eq(+ [sidebar_section.id],+ )++ expect(serialized[:sidebar_sections].first.links.map { |link| link.id }).to eq(+ [custom_sidebar_section_link_1.linkable.id],+ )+ end.count++ custom_sidebar_section_link_2 =+ Fabricate(:custom_sidebar_section_link, user: user, sidebar_section: sidebar_section)++ final_count =+ track_sql_queries do+ serialized = described_class.new(user, scope: Guardian.new(user), root: false).as_json++ expect(serialized[:sidebar_sections].map { |sidebar_section| sidebar_section.id }).to eq(+ [sidebar_section.id],+ )++ expect(serialized[:sidebar_sections].first.links.map { |link| link.id }).to eq(+ [custom_sidebar_section_link_1.linkable.id, custom_sidebar_section_link_2.linkable.id],+ )+ end.count++ expect(initial_count).to eq(final_count)+ end+ endend