PERF: Preload settings, groups and badge icons in SvgSprite.

PERF: Preload settings, groups and badge icons in SvgSprite.

Identified as a hot path in production. Preload it early instead of executing the queries in a live request.

diff --git a/lib/discourse.rb b/lib/discourse.rb
index a2b6f29..641e06f 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -930,6 +930,7 @@ module Discourse
 
       JsLocaleHelper.load_translations(SiteSetting.default_locale)
       Site.json_for(Guardian.new)
+      SvgSprite.preload
     end
 
     [
diff --git a/lib/svg_sprite/svg_sprite.rb b/lib/svg_sprite/svg_sprite.rb
index 34c6510..ff1b4f7 100644
--- a/lib/svg_sprite/svg_sprite.rb
+++ b/lib/svg_sprite/svg_sprite.rb
@@ -221,6 +221,12 @@ module SvgSprite
 
   THEME_SPRITE_VAR_NAME = "icons-sprite"
 
+  def self.preload
+    settings_icons
+    group_icons
+    badge_icons
+  end
+
   def self.custom_svg_sprites(theme_ids = [])
     get_set_cache("custom_svg_sprites_#{Theme.transform_ids(theme_ids).join(',')}") do
       custom_sprite_paths = Dir.glob("#{Rails.root}/plugins/*/svg-icons/*.svg")
@@ -415,16 +421,18 @@ License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL
   end
 
   def self.settings_icons
-    # includes svg_icon_subset and any settings containing _icon (incl. plugin settings)
-    site_setting_icons = []
+    get_set_cache("settings_icons") do
+      # includes svg_icon_subset and any settings containing _icon (incl. plugin settings)
+      site_setting_icons = []
 
-    SiteSetting.settings_hash.select do |key, value|
-      if key.to_s.include?("_icon") && String === value
-        site_setting_icons |= value.split('|')
+      SiteSetting.settings_hash.select do |key, value|
+        if key.to_s.include?("_icon") && String === value
+          site_setting_icons |= value.split('|')
+        end
       end
-    end
 
-    site_setting_icons
+      site_setting_icons
+    end
   end
 
   def self.plugin_icons
@@ -432,11 +440,15 @@ License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL
   end
 
   def self.badge_icons
-    Badge.pluck(:icon).uniq
+    get_set_cache("badge_icons") do
+      Badge.pluck(:icon).uniq
+    end
   end
 
   def self.group_icons
-    Group.pluck(:flair_icon).uniq
+    get_set_cache("group_icons") do
+      Group.pluck(:flair_icon).uniq
+    end
   end
 
   def self.theme_icons(theme_ids)

GitHub sha: 58b30fb5

This commit appears in #13266 which was approved by vinothkannans. It was merged by tgxworld.