From dc53a8c26dc55d21240233b3d83d36efdef6e924 Mon Sep 17 00:00:00 2001 From: schneems Date: Wed, 1 Oct 2014 18:09:37 -0500 Subject: 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. --- lib/rack/commonlogger.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/rack/commonlogger.rb') diff --git a/lib/rack/commonlogger.rb b/lib/rack/commonlogger.rb index 1c99045e..d2d6dc34 100644 --- a/lib/rack/commonlogger.rb +++ b/lib/rack/commonlogger.rb @@ -46,9 +46,9 @@ module Rack env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-", env["REMOTE_USER"] || "-", now.strftime("%d/%b/%Y:%H:%M:%S %z"), - env["REQUEST_METHOD"], - env["PATH_INFO"], - env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"], + env[REQUEST_METHOD], + env[PATH_INFO], + env[QUERY_STRING].empty? ? "" : "?"+env[QUERY_STRING], env["HTTP_VERSION"], status.to_s[0..3], length, @@ -65,7 +65,7 @@ module Rack end def extract_content_length(headers) - value = headers['Content-Length'] or return '-' + value = headers[CONTENT_LENGTH] or return '-' value.to_s == '0' ? '-' : value end end -- cgit v1.2.3-24-ge0c7