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.
This commit is contained in:
evazion
2021-12-15 22:48:24 -06:00
parent 3fcecd59a8
commit 163ba8e7da
4 changed files with 14 additions and 15 deletions

View File

@@ -123,6 +123,6 @@ class PostPreviewComponent < ApplicationComponent
end end
def has_sound? def has_sound?
post.has_tag?("sound") is_animated? && post.has_tag?("sound")
end end
end end

View File

@@ -4,6 +4,9 @@ class MediaAsset < ApplicationRecord
class Error < StandardError; end class Error < StandardError; end
VARIANTS = %i[preview crop 180x180 360x360 720x720 sample original] 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 :media_metadata, dependent: :destroy
has_one :pixiv_ugoira_frame_data, class_name: "PixivUgoiraFrameData", foreign_key: :md5, primary_key: :md5 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" "/images/download-preview.png"
else else
slug = "__#{slug}__" if slug.present? 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}" "/#{variant}/#{md5[0..1]}/#{md5[2..3]}/#{slug}#{file_name}"
end end
end end
@@ -166,7 +169,7 @@ class MediaAsset < ApplicationRecord
when :"720x720" when :"720x720"
true true
when :sample 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 when :original
true true
end end
@@ -271,7 +274,7 @@ class MediaAsset < ApplicationRecord
end end
def storage_service def storage_service
Danbooru.config.storage_manager STORAGE_SERVICE
end end
def backup_storage_service def backup_storage_service

View File

@@ -8,6 +8,8 @@ class Post < ApplicationRecord
NOTE_COPY_TAGS = %w[translated partially_translated check_translation translation_request reverse_translation NOTE_COPY_TAGS = %w[translated partially_translated check_translation translation_request reverse_translation
annotated partially_annotated check_annotation annotation_request] annotated partially_annotated check_annotation annotation_request]
RESTRICTED_TAGS = Danbooru.config.restricted_tags
self.ignored_columns = [:pool_string, :fav_string] self.ignored_columns = [:pool_string, :fav_string]
deletable deletable
@@ -292,11 +294,11 @@ class Post < ApplicationRecord
module TagMethods module TagMethods
def tag_array def tag_array
@tag_array ||= tag_string.split tag_string.split
end end
def tag_array_was 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 end
def tags def tags
@@ -346,7 +348,6 @@ class Post < ApplicationRecord
end end
def merge_old_changes def merge_old_changes
reset_tag_array_cache
@removed_tags = [] @removed_tags = []
if old_tag_string if old_tag_string
@@ -380,14 +381,8 @@ class Post < ApplicationRecord
end end
end end
def reset_tag_array_cache
@tag_array = nil
@tag_array_was = nil
end
def set_tag_string(string) def set_tag_string(string)
self.tag_string = string self.tag_string = string
reset_tag_array_cache
end end
def normalize_tags def normalize_tags
@@ -1298,7 +1293,7 @@ class Post < ApplicationRecord
end end
def levelblocked?(user = CurrentUser.user) 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 end
def banblocked?(user = CurrentUser.user) def banblocked?(user = CurrentUser.user)
@@ -1312,7 +1307,6 @@ class Post < ApplicationRecord
def reload(options = nil) def reload(options = nil)
super super
reset_tag_array_cache
@pools = nil @pools = nil
@tag_categories = nil @tag_categories = nil
@typed_tags = nil @typed_tags = nil

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
#
# This file contains all the configuration settings for Danbooru. # This file contains all the configuration settings for Danbooru.
# #
# Don't edit this file. Instead, to configure your Danbooru instance, copy this # Don't edit this file. Instead, to configure your Danbooru instance, copy this