From 54bc3e44f7f46fb311fd07fe8a974814b1df59c2 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Tue, 10 Dec 2013 22:06:41 -0800 Subject: [PATCH] Parallelize gzip extension using four threads --- .../lib/middleman-more/extensions/gzip.rb | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/middleman-core/lib/middleman-more/extensions/gzip.rb b/middleman-core/lib/middleman-more/extensions/gzip.rb index 3264c020..d52e8cfc 100644 --- a/middleman-core/lib/middleman-more/extensions/gzip.rb +++ b/middleman-core/lib/middleman-more/extensions/gzip.rb @@ -18,19 +18,41 @@ class Middleman::Extensions::Gzip < ::Middleman::Extension require 'zlib' require 'stringio' require 'find' + require 'thread' end def after_build(builder) + num_threads = 4 paths = ::Middleman::Util.all_files_under(app.build_dir) total_savings = 0 + # Fill a queue with inputs + in_queue = Queue.new + paths.each do |path| + in_queue << path if options.exts.include?(path.extname) + end + num_paths = in_queue.size + + # Farm out gzip tasks to threads and put the results in in_queue + out_queue = Queue.new + threads = num_threads.times.map do + Thread.new do + while path = in_queue.pop + out_queue << gzip_file(path.to_s) + end + end + end + + # Insert a nil for each thread to stop it + num_threads.times do + in_queue << nil + end + old_locale = I18n.locale I18n.locale = :en # use the english localizations for printing out file sizes to make sure the localizations exist - paths.each do |path| - next unless options.exts.include? path.extname - - output_filename, old_size, new_size = gzip_file(path.to_s) + num_paths.times do + output_filename, old_size, new_size = out_queue.pop if output_filename total_savings += (old_size - new_size) @@ -50,7 +72,7 @@ class Middleman::Extensions::Gzip < ::Middleman::Extension # Check if the right file's already there if File.exist?(output_filename) && File.mtime(output_filename) == input_file_time - return + return [nil, nil, nil] end File.open(output_filename, 'wb') do |f|