summary refs log tree commit
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2017-06-29 01:59:51 +0000
committerEric Wong <e@80x24.org>2017-06-29 02:04:50 +0000
commit69a2a195d749fdc2c04451688f3569bd5ce24c73 (patch)
tree17085a4ffb143087dee9673ff3e62857193428ca
parentbe758b9c0311bd820be485949a5d5ea99dfabd0b (diff)
downloadrack-deflater.tar.gz
deflater: reduce live Time objects deflater
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
-rw-r--r--lib/rack/deflater.rb6
1 files 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