A platform for community discussion. Free, open, simple.
PERF: optimize homepage and topic performance (#11607)
Sam1cd6ff15dc3d
diff --git a/lib/file_helper.rb b/lib/file_helper.rbindex 162de9a40b5..f87600ccdc5 100644--- a/lib/file_helper.rb+++ b/lib/file_helper.rb@@ -14,15 +14,15 @@ class FileHelperenddef self.is_supported_image?(filename)- (filename =~ supported_images_regexp).present?+ filename.match?(supported_images_regexp)enddef self.is_inline_image?(filename)- (filename =~ inline_images_regexp).present?+ filename.match?(inline_images_regexp)enddef self.is_supported_media?(filename)- (filename =~ supported_media_regexp).present?+ filename.match?(supported_media_regexp)endclass FakeIO@@ -162,7 +162,10 @@ class FileHelperenddef self.supported_media_regexp- media = supported_images | supported_audio | supported_video- @@supported_media_regexp ||= /\.(#{media.to_a.join("|")})$/i+ @@supported_media_regexp ||=+ begin+ media = supported_images | supported_audio | supported_video+ /\.(#{media.to_a.join("|")})$/i+ endendenddiff --git a/lib/guardian/post_guardian.rb b/lib/guardian/post_guardian.rbindex 4bbd2ac3e82..ca10a2924a9 100644--- a/lib/guardian/post_guardian.rb+++ b/lib/guardian/post_guardian.rb@@ -1,6 +1,6 @@# frozen_string_literal: true-#mixin for all guardian methods dealing with post permissions+# mixin for all guardian methods dealing with post permissionsmodule PostGuardiandef unrestricted_link_posting?@@ -104,7 +104,6 @@ module PostGuardianuser.post_count <= SiteSetting.delete_all_posts_max.to_iend- # Creating Methoddef can_create_post?(parent)return false if !SiteSetting.enable_system_message_replies? && parent.try(:subtype) == "system_message"@@ -115,7 +114,6 @@ module PostGuardian)end- # Editing Methoddef can_edit_post?(post)if Discourse.static_doc_topic_ids.include?(post.topic_id) && !is_admin?return false@@ -181,7 +179,6 @@ module PostGuardianpost.is_first_post? ? post.topic && can_delete_topic?(post.topic) : can_delete_post?(post)end- # Deleting Methodsdef can_delete_post?(post)return false if !can_see_post?(post)@@ -198,11 +195,11 @@ module PostGuardiancan_moderateend- # Recovery Methoddef can_recover_post?(post)return false unless post- topic = Topic.with_deleted.find(post.topic_id) if post.topic_id+ # PERF, vast majority of the time topic will not be deleted+ topic = (post.topic || Topic.with_deleted.find(post.topic_id)) if post.topic_idif can_moderate_topic?(topic)!!post.deleted_atdiff --git a/lib/url_helper.rb b/lib/url_helper.rbindex 885c6ee7675..eff8ab87e2a 100644--- a/lib/url_helper.rb+++ b/lib/url_helper.rb@@ -79,12 +79,6 @@ class UrlHelperdef self.cook_url(url, secure: false)return url unless is_local(url)- uri = URI.parse(url)- filename = File.basename(uri.path)- is_attachment = !FileHelper.is_supported_media?(filename)-- no_cdn = SiteSetting.login_required || SiteSetting.prevent_anons_from_downloading_files-url = secure ? secure_proxy_without_cdn(url) : absolute_without_cdn(url)# we always want secure media to come from@@ -92,6 +86,19 @@ class UrlHelper# to avoid asset_host mixupsreturn schemaless(url) if secure+ # PERF: avoid parsing url excpet for extreme conditions+ # this is a hot path used on home page+ filename = url+ if url.include?("?")+ uri = URI.parse(url)+ filename = File.basename(uri.path)+ end++ # this technically requires a filename, but will work with a URL as long as it end with the+ # extension and has no query params+ is_attachment = !FileHelper.is_supported_media?(filename)++ no_cdn = SiteSetting.login_required || SiteSetting.prevent_anons_from_downloading_filesunless is_attachment && no_cdnurl = Discourse.store.cdn_url(url)url = local_cdn_url(url) if Discourse.store.external?