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

@@ -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