A platform for community discussion. Free, open, simple.
PERF: reduce workload when optimizing images
Sam4232d3269952
diff --git a/lib/file_helper.rb b/lib/file_helper.rbindex 3f148fc278c..feed479b0b4 100644--- a/lib/file_helper.rb+++ b/lib/file_helper.rb@@ -83,26 +83,41 @@ class FileHelperenddef self.optimize_image!(filename, allow_pngquant: false)- pngquant_options = false- if allow_pngquant- pngquant_options = { allow_lossy: true }+ image_optim(+ allow_pngquant: allow_pngquant,+ strip_image_metadata: SiteSetting.strip_image_metadata+ ).optimize_image!(filename)+ end++ def self.image_optim(allow_pngquant: false, strip_image_metadata: true)+ # memoization is critical, initializing an ImageOptim object is very expensive+ # sometimes up to 200ms searching for binaries and looking at versions+ memoize("image_optim", allow_pngquant, strip_image_metadata) do+ pngquant_options = false+ if allow_pngquant+ pngquant_options = { allow_lossy: true }+ end++ ImageOptim.new(+ # GLOBAL+ timeout: 15,+ skip_missing_workers: true,+ # PNG+ optipng: { level: 2, strip: strip_image_metadata },+ advpng: false,+ pngcrush: false,+ pngout: false,+ pngquant: pngquant_options,+ # JPG+ jpegoptim: { strip: strip_image_metadata ? "all" : "none" },+ jpegtran: false,+ jpegrecompress: false,+ )end+ end- ImageOptim.new(- # GLOBAL- timeout: 15,- skip_missing_workers: true,- # PNG- optipng: { level: 2, strip: SiteSetting.strip_image_metadata },- advpng: false,- pngcrush: false,- pngout: false,- pngquant: pngquant_options,- # JPG- jpegoptim: { strip: SiteSetting.strip_image_metadata ? "all" : "none" },- jpegtran: false,- jpegrecompress: false,- ).optimize_image!(filename)+ def self.memoize(*args)+ (@memoized ||= {})[args] ||= yieldenddef self.supported_images