From cda1f3381096cdb8c78a126a4993e458bf798ca2 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 5 Feb 2009 14:30:29 -0800 Subject: pre-generate HTTP_STATUS_HEADER to avoid repeated snprintf Regenerating headers constantly is a waste of time. --- lib/unicorn/const.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'lib/unicorn/const.rb') diff --git a/lib/unicorn/const.rb b/lib/unicorn/const.rb index 8cefd2b..b44bb29 100644 --- a/lib/unicorn/const.rb +++ b/lib/unicorn/const.rb @@ -44,6 +44,12 @@ module Unicorn 505 => 'HTTP Version not supported' } + HTTP_STATUS_HEADERS = HTTP_STATUS_CODES.inject({}) do |hash, (code, text)| + text.freeze + hash[code] = "HTTP/1.1 #{code} #{text}\r\nConnection: close\r\n".freeze + hash + end + # Frequently used constants when constructing requests or responses. Many times # the constant just refers to a string with the same contents. Using these constants # gave about a 3% to 10% performance improvement over using the strings directly. @@ -91,7 +97,6 @@ module Unicorn MAX_BODY=MAX_HEADER # A frozen format for this is about 15% faster - STATUS_FORMAT = "HTTP/1.1 %d %s\r\nConnection: close\r\n".freeze CONTENT_TYPE = "Content-Type".freeze LAST_MODIFIED = "Last-Modified".freeze ETAG = "ETag".freeze @@ -109,4 +114,5 @@ module Unicorn REDIRECT = "HTTP/1.1 302 Found\r\nLocation: %s\r\nConnection: close\r\n\r\n".freeze HOST = "HOST".freeze end + end -- cgit v1.2.3-24-ge0c7