PERF: Memoize core svgs in memory to avoid expensive XML parsing.

The XML parsing of SVGs is done whenever the cache expires or on the
first load after a reboot. In one of our production instance, parsing
ranges from 30ms to 70ms which is not ideal. Instead, we've decided to
make a small memory trade off here by memoizing the core SVGs once on
boot to avoid parsing of the SVG files during the duration of a request.
The memozied hash will take up 57440 bytes or 0.05744 megabytes in size.
This commit is contained in:
Alan Guo Xiang Tan 2021-06-01 14:57:24 +08:00
parent ab8949d13a
commit 0700e9382d
2 changed files with 67 additions and 23 deletions

View File

@ -914,9 +914,9 @@ module Discourse
schema_cache = ActiveRecord::Base.connection.schema_cache
RailsMultisite::ConnectionManagement.safe_each_connection do
# load up schema cache for all multisite assuming all dbs have
# an identical schema
RailsMultisite::ConnectionManagement.safe_each_connection do
dup_cache = schema_cache.dup
# this line is not really needed, but just in case the
# underlying implementation changes lets give it a shot
@ -948,6 +948,9 @@ module Discourse
},
Thread.new {
LetterAvatar.image_magick_version
},
Thread.new {
SvgSprite.core_svgs
}
].each(&:join)
ensure

View File

@ -225,6 +225,7 @@ module SvgSprite
get_set_cache("custom_svg_sprites_#{Theme.transform_ids(theme_ids).join(',')}") do
custom_sprite_paths = Dir.glob("#{Rails.root}/plugins/*/svg-icons/*.svg")
if theme_ids.present?
ThemeField.where(type_id: ThemeField.types[:theme_upload_var], name: THEME_SPRITE_VAR_NAME, theme_id: Theme.transform_ids(theme_ids))
.pluck(:upload_id).each do |upload_id|
@ -239,6 +240,7 @@ module SvgSprite
custom_sprite_paths << original_path if original_path.present?
end
end
custom_sprite_paths
end
@ -275,7 +277,33 @@ module SvgSprite
end
def self.sprite_sources(theme_ids)
CORE_SVG_SPRITES | custom_svg_sprites(theme_ids)
sources = CORE_SVG_SPRITES
if theme_ids.present?
sources = sources + custom_svg_sprites(theme_ids)
end
sources
end
def self.core_svgs
@core_svgs ||= begin
symbols = {}
CORE_SVG_SPRITES.each do |filename|
svg_filename = "#{File.basename(filename, ".svg")}"
Nokogiri::XML(File.open(filename)) do |config|
config.options = Nokogiri::XML::ParseOptions::NOBLANKS
end.css('symbol').each do |sym|
icon_id = prepare_symbol(sym, svg_filename)
sym.attributes['id'].value = icon_id
symbols[icon_id] = sym.to_xml
end
end
symbols
end
end
def self.bundle(theme_ids = [])
@ -288,15 +316,23 @@ License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL
<svg xmlns='http://www.w3.org/2000/svg' style='display: none;'>
""".dup
sprite_sources(theme_ids).each do |fname|
core_svgs.each do |icon_id, sym|
if icons.include?(icon_id)
svg_subset << sym
end
end
if theme_ids.present?
custom_svg_sprites(theme_ids).each do |fname|
svg_file = Nokogiri::XML(File.open(fname)) do |config|
config.options = Nokogiri::XML::ParseOptions::NOBLANKS
end
svg_filename = "#{File.basename(fname, ".svg")}"
svg_file.css('symbol').each do |sym|
svg_file.css("symbol").each do |sym|
icon_id = prepare_symbol(sym, svg_filename)
if icons.include? icon_id
sym.attributes['id'].value = icon_id
sym.css('title').each(&:remove)
@ -304,6 +340,7 @@ License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL
end
end
end
end
svg_subset << '</svg>'
end
@ -405,6 +442,8 @@ License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL
end
def self.theme_icons(theme_ids)
return [] if theme_ids.blank?
theme_icon_settings = []
# Need to load full records for default values
@ -422,6 +461,8 @@ License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL
end
def self.custom_icons(theme_ids)
return [] if theme_ids.blank?
# Automatically register icons in sprites added via themes or plugins
icons = []
custom_svg_sprites(theme_ids).each do |fname|