From b744306654c716a2306b1c5b2f98c39a3e70d465 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 20 Apr 2016 09:34:30 +0800 Subject: [PATCH] PERF: Uglify and gzip assets concurrently. --- lib/tasks/assets.rake | 54 +++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/lib/tasks/assets.rake b/lib/tasks/assets.rake index 3e68b4213b1..dd71a22f84a 100644 --- a/lib/tasks/assets.rake +++ b/lib/tasks/assets.rake @@ -116,6 +116,16 @@ def compress(from,to) end end +def concurrent? + if ENV["CONCURRENT"] == "1" + concurrent_compressors = [] + yield(Proc.new { |&block| concurrent_compressors << Concurrent::Future.execute { block.call } }) + concurrent_compressors.each(&:wait!) + else + yield(Proc.new { |&block| block.call }) + end +end + task 'assets:precompile' => 'assets:precompile:before' do # Run after assets:precompile Rake::Task["assets:precompile:css"].invoke @@ -124,30 +134,34 @@ task 'assets:precompile' => 'assets:precompile:before' do puts "Compressing Javascript and Generating Source Maps" manifest = Sprockets::Manifest.new(assets_path) - to_skip = Rails.configuration.assets.skip_minification || [] - manifest.files - .select{|k,v| k =~ /\.js$/} - .each do |file, info| + concurrent? do |proc| + to_skip = Rails.configuration.assets.skip_minification || [] + manifest.files + .select{|k,v| k =~ /\.js$/} + .each do |file, info| - path = "#{assets_path}/#{file}" - _file = (d = File.dirname(file)) == "." ? "_#{file}" : "#{d}/_#{File.basename(file)}" - _path = "#{assets_path}/#{_file}" + path = "#{assets_path}/#{file}" + _file = (d = File.dirname(file)) == "." ? "_#{file}" : "#{d}/_#{File.basename(file)}" + _path = "#{assets_path}/#{_file}" - if File.exists?(_path) - STDERR.puts "Skipping: #{file} already compressed" - else - STDERR.puts "Compressing: #{file}" + if File.exists?(_path) + STDERR.puts "Skipping: #{file} already compressed" + else + STDERR.puts "Compressing: #{file}" - # We can specify some files to never minify - unless (ENV["DONT_MINIFY"] == "1") || to_skip.include?(info['logical_path']) - FileUtils.mv(path, _path) - compress(_file,file) + proc.call do + # We can specify some files to never minify + unless (ENV["DONT_MINIFY"] == "1") || to_skip.include?(info['logical_path']) + FileUtils.mv(path, _path) + compress(_file,file) + end + + info["size"] = File.size(path) + info["mtime"] = File.mtime(path).iso8601 + gzip(path) + end end - - info["size"] = File.size(path) - info["mtime"] = File.mtime(path).iso8601 - gzip(path) - end + end end # protected