From 69a2a195d749fdc2c04451688f3569bd5ce24c73 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 29 Jun 2017 01:59:51 +0000 Subject: deflater: reduce live Time objects Only create a Time object if the Last-Modified header exists, (immediately use the Integer result of Time#to_). Otherwise, we can rely on the default behavior of Zlib::GzipWriter of setting the mtime to the current time. While we're at it, avoid repeating the hash lookup for Last-Modified. This results in an improvement from 11.0k to 11.4k iterations/sec with the following script: require 'benchmark/ips' require 'rack/mock' req = Rack::MockRequest.env_for('', 'HTTP_ACCEPT_ENCODING' => 'gzip') response = [200, {}, []] deflater = Rack::Deflater.new(lambda { |env| response }) Benchmark.ips do |x| x.report('deflater') do s, h, b = deflater.call(req) b.each { |buf| } b.close end end --- lib/rack/deflater.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/rack/deflater.rb b/lib/rack/deflater.rb index 821f708b..0d0d021f 100644 --- a/lib/rack/deflater.rb +++ b/lib/rack/deflater.rb @@ -58,8 +58,8 @@ module Rack when "gzip" headers['Content-Encoding'] = "gzip" headers.delete('Content-Length') - mtime = headers.key?("Last-Modified") ? - Time.httpdate(headers["Last-Modified"]) : Time.now + mtime = headers["Last-Modified"] + mtime = Time.httpdate(mtime).to_i if mtime [status, headers, GzipStream.new(body, mtime, @sync)] when "identity" [status, headers, body] @@ -80,7 +80,7 @@ module Rack def each(&block) @writer = block gzip =::Zlib::GzipWriter.new(self) - gzip.mtime = @mtime + gzip.mtime = @mtime if @mtime @body.each { |part| gzip.write(part) gzip.flush if @sync -- cgit v1.2.3-24-ge0c7