rack-devel archive mirror (unofficial) https://groups.google.com/group/rack-devel
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: rack-devel@googlegroups.com
Subject: [PATCH] conditionalget: avoid const lookup in case/when
Date: Sun, 5 Oct 2014 08:15:25 +0000	[thread overview]
Message-ID: <20141005081525.GA8997@dcvr.yhbt.net> (raw)
In-Reply-To: <20141003200836.GA4664@dcvr.yhbt.net>

case/when dispatches already optimize away allocation of constant
string literals in all C Ruby 1.9.x/2.x releases
(ref: opt_case_dispatch in Ruby insns.def)

Other Ruby implementations presumably have similar optimizations
to encourage prettier code.

The following code snippet does not cause GC.count to increase
during the two loops, regardless of what `nr' is.
Tested on Ruby 1.9.3, 2.1.3, and trunk r47786:

    GET = "GET"
    HEAD = "HEAD"
    REQUEST_METHOD = "REQUEST_METHOD" # unnecessary in 2.2.0+
    env = { REQUEST_METHOD => "GET" }

    nr = 10000000
    nr.times do |i|
      case env[REQUEST_METHOD]
      when GET, HEAD
        :foo
      else
        :bar
      end
    end
    a = GC.count

    nr.times do |i|
      case env[REQUEST_METHOD]
      when "GET", "HEAD"
        :foo
      else
        :bar
      end
    end
    b = GC.count
    p [ a, b ]
---
  Eric Wong <e@80x24.org> wrote:
  > I don't disagree with this commit at this time

  OK, I disagree with one part :)

  I think the following allocations may be elided in future C Ruby, too:
    a) foo == "lit" (and "lit" == foo)
    b) str << "lit"

  If you prefer `git pull" for this:

  The following changes since commit 022b0076b0eacad03eac48060198f05aa776a866:

    Merge pull request #739 from schneems/schneems/fix-respond_to (2014-10-02 21:29:17 +0200)

  are available in the git repository at:

    git://bogomips.org/rack.git when-lit

  for you to fetch changes up to 68e086d2b42ee2c9fc8017264aa353a0d543fd13:

    conditionalget: avoid const lookup in case/when (2014-10-05 08:09:50 +0000)

  ----------------------------------------------------------------
  Eric Wong (1):
        conditionalget: avoid const lookup in case/when

 lib/rack/conditionalget.rb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/rack/conditionalget.rb b/lib/rack/conditionalget.rb
index 3d4c78a..441dd38 100644
--- a/lib/rack/conditionalget.rb
+++ b/lib/rack/conditionalget.rb
@@ -21,7 +21,7 @@ module Rack
 
     def call(env)
       case env[REQUEST_METHOD]
-      when GET, HEAD
+      when "GET", "HEAD"
         status, headers, body = @app.call(env)
         headers = Utils::HeaderHash.new(headers)
         if status == 200 && fresh?(env, headers)
-- 
EW

-- 

--- 
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.

  reply	other threads:[~2014-10-05  8:15 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-03 20:08 Less allocated objects on each request Eric Wong
2014-10-05  8:15 ` Eric Wong [this message]
2014-10-05 21:37   ` [PATCH] conditionalget: avoid const lookup in case/when James Tucker
2014-10-06 20:00     ` richard schneeman
2016-05-12  3:04       ` Less allocated objects on each request Eric Wong
2016-05-12 16:54         ` Aaron Patterson
2016-05-12 17:01           ` richard schneeman
2016-05-13 23:41           ` Eric Wong
2016-06-17 19:43             ` richard schneeman
2016-06-17 21:00               ` Aaron Patterson
2016-06-17 23:12                 ` Eric Wong
2016-06-17 23:19                   ` Aaron Patterson
2016-06-17 23:43                     ` Eric Wong
2014-10-05 21:31 ` James Tucker

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:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  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=20141005081525.GA8997@dcvr.yhbt.net \
    --to=rack-devel@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).