rack-devel archive mirror (unofficial) https://groups.google.com/group/rack-devel
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: rack-devel@googlegroups.com
Subject: [PATCH 5/5] deflater: reduce live Time objects
Date: Thu, 29 Jun 2017 02:19:15 +0000	[thread overview]
Message-ID: <20170629021915.22517-6-e@80x24.org> (raw)
In-Reply-To: <20170629021915.22517-1-e@80x24.org>

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

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| }
 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 821f708..0d0d021 100644
--- a/lib/rack/deflater.rb
+++ b/lib/rack/deflater.rb
@@ -58,8 +58,8 @@ module Rack
       when "gzip"
         headers['Content-Encoding'] = "gzip"
-        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.flush if @sync


You received this message because you are subscribed to the Google Groups "Rack Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rack-devel+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

  parent reply	other threads:[~2017-06-29  2:19 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-29  2:19 [PATCH 0/5] deflater: tiny optimizations + 1 new feature Eric Wong
2017-06-29  2:19 ` [PATCH 1/5] deflater: rely on frozen_string_literal Eric Wong
2017-06-29  2:19 ` [PATCH 2/5] deflater: avoid wasting an ivar slot on @closed Eric Wong
2017-06-29  2:19 ` [PATCH 3/5] deflater: support "sync: false" option Eric Wong
2017-06-29  2:19 ` [PATCH 4/5] deflater: additional mtime tests Eric Wong
2017-06-29  2:19 ` Eric Wong [this message]
2017-07-24  1:33 ` [PATCH 0/5] deflater: tiny optimizations + 1 new feature Eric Wong
2017-07-25  1:11   ` Rafael Mendonça França

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: https://groups.google.com/group/rack-devel

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170629021915.22517-6-e@80x24.org \
    --to=rack-devel@googlegroups.com \
    --subject='Re: [PATCH 5/5] deflater: reduce live Time objects' \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

rack-devel archive mirror (unofficial) https://groups.google.com/group/rack-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/rack-devel

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 rack-devel rack-devel/ https://public-inbox.org/rack-devel \
	public-inbox-index rack-devel

Example config snippet for mirrors.
Newsgroups are available over NNTP:
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for project(s) associated with this inbox:


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git