From 163ba8e7dae45778518243c7fc2fee29729eef86 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 15 Dec 2021 22:48:24 -0600 Subject: [PATCH] posts: micro-optimize allocations during thumbnail generation. Do a few micro-optimizations to reduce the number of memory allocations during thumbnail generation. This commit, combined with freezing string literals in a7dc05 and 67b961, reduces the number of allocations on the front page from 180,000 to 150,000, and the number of retained objects from 8,000 to 4,000. --- app/components/post_preview_component.rb | 2 +- app/models/media_asset.rb | 9 ++++++--- app/models/post.rb | 16 +++++----------- config/danbooru_default_config.rb | 2 ++ 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/app/components/post_preview_component.rb b/app/components/post_preview_component.rb index 7c55fdc63..725763559 100644 --- a/app/components/post_preview_component.rb +++ b/app/components/post_preview_component.rb @@ -123,6 +123,6 @@ class PostPreviewComponent < ApplicationComponent end def has_sound? - post.has_tag?("sound") + is_animated? && post.has_tag?("sound") end end diff --git a/app/models/media_asset.rb b/app/models/media_asset.rb index ced1351bc..ac0d28f89 100644 --- a/app/models/media_asset.rb +++ b/app/models/media_asset.rb @@ -4,6 +4,9 @@ class MediaAsset < ApplicationRecord class Error < StandardError; end VARIANTS = %i[preview crop 180x180 360x360 720x720 sample original] + ENABLE_SEO_POST_URLS = Danbooru.config.enable_seo_post_urls + LARGE_IMAGE_WIDTH = Danbooru.config.large_image_width + STORAGE_SERVICE = Danbooru.config.storage_manager has_one :media_metadata, dependent: :destroy has_one :pixiv_ugoira_frame_data, class_name: "PixivUgoiraFrameData", foreign_key: :md5, primary_key: :md5 @@ -88,7 +91,7 @@ class MediaAsset < ApplicationRecord "/images/download-preview.png" else slug = "__#{slug}__" if slug.present? - slug = nil if !Danbooru.config.enable_seo_post_urls + slug = nil if !ENABLE_SEO_POST_URLS "/#{variant}/#{md5[0..1]}/#{md5[2..3]}/#{slug}#{file_name}" end end @@ -166,7 +169,7 @@ class MediaAsset < ApplicationRecord when :"720x720" true when :sample - media_asset.is_ugoira? || (media_asset.is_static_image? && media_asset.image_width > Danbooru.config.large_image_width) + media_asset.is_ugoira? || (media_asset.is_static_image? && media_asset.image_width > LARGE_IMAGE_WIDTH) when :original true end @@ -271,7 +274,7 @@ class MediaAsset < ApplicationRecord end def storage_service - Danbooru.config.storage_manager + STORAGE_SERVICE end def backup_storage_service diff --git a/app/models/post.rb b/app/models/post.rb index 5640391bd..6f851a4a9 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -8,6 +8,8 @@ class Post < ApplicationRecord NOTE_COPY_TAGS = %w[translated partially_translated check_translation translation_request reverse_translation annotated partially_annotated check_annotation annotation_request] + RESTRICTED_TAGS = Danbooru.config.restricted_tags + self.ignored_columns = [:pool_string, :fav_string] deletable @@ -292,11 +294,11 @@ class Post < ApplicationRecord module TagMethods def tag_array - @tag_array ||= tag_string.split + tag_string.split end def tag_array_was - @tag_array_was ||= (tag_string_in_database.presence || tag_string_before_last_save || "").split + (tag_string_in_database.presence || tag_string_before_last_save || "").split end def tags @@ -346,7 +348,6 @@ class Post < ApplicationRecord end def merge_old_changes - reset_tag_array_cache @removed_tags = [] if old_tag_string @@ -380,14 +381,8 @@ class Post < ApplicationRecord end end - def reset_tag_array_cache - @tag_array = nil - @tag_array_was = nil - end - def set_tag_string(string) self.tag_string = string - reset_tag_array_cache end def normalize_tags @@ -1298,7 +1293,7 @@ class Post < ApplicationRecord end def levelblocked?(user = CurrentUser.user) - !user.is_gold? && Danbooru.config.restricted_tags.any? { |tag| tag.in?(tag_array) } + !user.is_gold? && RESTRICTED_TAGS.any? { |tag| tag.in?(tag_array) } end def banblocked?(user = CurrentUser.user) @@ -1312,7 +1307,6 @@ class Post < ApplicationRecord def reload(options = nil) super - reset_tag_array_cache @pools = nil @tag_categories = nil @typed_tags = nil diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index f5768ff15..bc5b4c840 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true +# # This file contains all the configuration settings for Danbooru. # # Don't edit this file. Instead, to configure your Danbooru instance, copy this