summary refs log tree commit
path: root/lib/rack/conditionalget.rb
diff options
context:
space:
mode:
authorschneems <richard.schneeman@gmail.com>2014-10-01 18:09:37 -0500
committerschneems <richard.schneeman@gmail.com>2014-10-02 07:42:20 -0500
commitdc53a8c26dc55d21240233b3d83d36efdef6e924 (patch)
tree991037ede0df148b5aef7137f321591401f98d53 /lib/rack/conditionalget.rb
parenta71be3c914c10d1089238f4e21b029b885be4029 (diff)
downloadrack-dc53a8c26dc55d21240233b3d83d36efdef6e924.tar.gz
Less allocated objects on each request
How many? Using `memory_profiler` and a Rails app (codetriage.com), master uses:

```
rack/lib x 7318
```

After this patch, the app uses:

```
rack/lib x 4598
```

Or `(7318 - 4598) / 7318.0 * 100 # => 37.16` % fewer objects __PER REQUEST__.

To do this, I extracted really commonly used strings into top level Rack constants. It makes for a bit of a big diff, but I believe the changes are worth it. 

Running benchmark/ips against the same app, I'm seeing a performance host of `2~4%` across the entire app response. This doesn't just make Rack faster, it will make your app faster.

While we could certainly go overboard and pre-define ALL strings as constants, that would be pretty gnarly to work with. This patch goes after the largest of the low hanging fruit.
Diffstat (limited to 'lib/rack/conditionalget.rb')
-rw-r--r--lib/rack/conditionalget.rb8
1 files changed, 4 insertions, 4 deletions
diff --git a/lib/rack/conditionalget.rb b/lib/rack/conditionalget.rb
index 88573166..3d4c78aa 100644
--- a/lib/rack/conditionalget.rb
+++ b/lib/rack/conditionalget.rb
@@ -20,14 +20,14 @@ module Rack
     end
 
     def call(env)
-      case env['REQUEST_METHOD']
-      when "GET", "HEAD"
+      case env[REQUEST_METHOD]
+      when GET, HEAD
         status, headers, body = @app.call(env)
         headers = Utils::HeaderHash.new(headers)
         if status == 200 && fresh?(env, headers)
           status = 304
-          headers.delete('Content-Type')
-          headers.delete('Content-Length')
+          headers.delete(CONTENT_TYPE)
+          headers.delete(CONTENT_LENGTH)
           original_body = body
           body = Rack::BodyProxy.new([]) do
             original_body.close if original_body.respond_to?(:close)