From mboxrd@z Thu Jan 1 00:00:00 1970 Delivered-To: chneukirchen@gmail.com Received: by 10.86.23.1 with SMTP id 1cs150218fgw; Fri, 4 Sep 2009 14:42:04 -0700 (PDT) Received-SPF: pass (google.com: domain of grbounce-ceibQwUAAAB4YPBqaDIjI2bFOCxyyh3G=chneukirchen=gmail.com@googlegroups.com designates 10.143.20.42 as permitted sender) client-ip=10.143.20.42; Authentication-Results: mr.google.com; spf=pass (google.com: domain of grbounce-ceibQwUAAAB4YPBqaDIjI2bFOCxyyh3G=chneukirchen=gmail.com@googlegroups.com designates 10.143.20.42 as permitted sender) smtp.mail=grbounce-ceibQwUAAAB4YPBqaDIjI2bFOCxyyh3G=chneukirchen=gmail.com@googlegroups.com; dkim=pass header.i=grbounce-ceibQwUAAAB4YPBqaDIjI2bFOCxyyh3G=chneukirchen=gmail.com@googlegroups.com Received: from mr.google.com ([10.143.20.42]) by 10.143.20.42 with SMTP id x42mr4633789wfi.15.1252100523386 (num_hops = 1); Fri, 04 Sep 2009 14:42:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=beta; h=domainkey-signature:received:received:x-sender:x-apparently-to :received:received:received:received-spf:received:from:to:cc:subject :date:message-id:x-mailer:in-reply-to:references:reply-to:sender :precedence:x-google-loop:mailing-list:list-id:list-post:list-help :list-unsubscribe:x-beenthere-env:x-beenthere; bh=atgcAZpD15+4NN6qwDPva9jefWSWr5euOX8sMiy4ilE=; b=kZG7gEh3RZ1v08Ci4qdDnMWqC1XiHTlYteIRLPGOgPlXZ57ExkI9G4sqqt01GJmAN8 VwzUi0VCPmKBd88Ufnvg7y12gQi/ThAAAFTLqtImvc1WEHVSApGXs1U6TwQaAHpVcBhd LdswbGn6px5l1CjDkfWDJjNjO+XfU7jOK/q04= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlegroups.com; s=beta; h=x-sender:x-apparently-to:received-spf:authentication-results:from :to:cc:subject:date:message-id:x-mailer:in-reply-to:references :reply-to:sender:precedence:x-google-loop:mailing-list:list-id :list-post:list-help:list-unsubscribe:x-beenthere-env:x-beenthere; b=j+9XyGPSDlZljCI++OHtwCW/JRleE+fa9nSHnQTFBvTXWltg/a4EDuJ0yQIFrdVJOr CdhwTCQ6fjPRY7I0Ryt67qEmHakNm4D/TkViO1uELVQSDYyFbycx6uIc7Ecq/duIWdHK MK45qIVWMXgZzH9REx1eftfxN+Zr8fK0qs6ug= Received: by 10.143.20.42 with SMTP id x42mr866115wfi.15.1252100523338; Fri, 04 Sep 2009 14:42:03 -0700 (PDT) Received: by 10.106.99.27 with SMTP id w27gr1640prb.0; Fri, 04 Sep 2009 14:42:02 -0700 (PDT) X-Sender: normalperson@yhbt.net X-Apparently-To: rack-devel@googlegroups.com Received: by 10.141.35.18 with SMTP id n18mr3415929rvj.15.1252100520614; Fri, 04 Sep 2009 14:42:00 -0700 (PDT) Received: by 10.141.35.18 with SMTP id n18mr3415928rvj.15.1252100520594; Fri, 04 Sep 2009 14:42:00 -0700 (PDT) Return-Path: Received: from dcvr.yhbt.net (dcvr.yhbt.net [64.71.152.64]) by gmr-mx.google.com with ESMTP id 25si340397pzk.11.2009.09.04.14.42.00; Fri, 04 Sep 2009 14:42:00 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of normalperson@yhbt.net designates 64.71.152.64 as permitted sender) client-ip=64.71.152.64; Authentication-Results: gmr-mx.google.com; spf=pass (google.com: best guess record for domain of normalperson@yhbt.net designates 64.71.152.64 as permitted sender) smtp.mail=normalperson@yhbt.net Received: from localhost (unknown [12.186.229.34]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPSA id 21C931F585; Fri, 4 Sep 2009 21:42:00 +0000 (UTC) From: Eric Wong To: rack-devel@googlegroups.com Cc: Eric Wong Subject: [PATCH 1/3] HeaderHash.new avoids unnecessary object creation Date: Fri, 4 Sep 2009 14:41:52 -0700 Message-Id: <1252100514-2748-2-git-send-email-normalperson@yhbt.net> X-Mailer: git-send-email 1.6.4.2.236.gf324c In-Reply-To: <1252100514-2748-1-git-send-email-normalperson@yhbt.net> References: <1252100514-2748-1-git-send-email-normalperson@yhbt.net> Reply-To: rack-devel@googlegroups.com Sender: rack-devel@googlegroups.com Precedence: bulk X-Google-Loop: groups Mailing-List: list rack-devel@googlegroups.com; contact rack-devel+owner@googlegroups.com List-Id: List-Post: List-Help: List-Unsubscribe: , X-BeenThere-Env: rack-devel@googlegroups.com X-BeenThere: rack-devel@googlegroups.com Creating a new HeaderHash is an O(n) operation in addition to the cost of allocating a new object. When using multiple pieces of middleware, this can lead to unnecessary memory allocation and iteration overhead. We now explicitly define the HeaderHash.new class method to return its original argument if it is already a HeaderHash to avoid repeating work. --- lib/rack/utils.rb | 4 ++++ test/spec_rack_utils.rb | 8 ++++++++ 2 files changed, 12 insertions(+), 0 deletions(-) diff --git a/lib/rack/utils.rb b/lib/rack/utils.rb index 74303ef..21b58c9 100644 --- a/lib/rack/utils.rb +++ b/lib/rack/utils.rb @@ -258,6 +258,10 @@ module Rack # A case-insensitive Hash that preserves the original case of a # header when set. class HeaderHash < Hash + def self.new(hash={}) + HeaderHash === hash ? hash : super(hash) + end + def initialize(hash={}) super() @names = {} diff --git a/test/spec_rack_utils.rb b/test/spec_rack_utils.rb index c397429..3586f8c 100644 --- a/test/spec_rack_utils.rb +++ b/test/spec_rack_utils.rb @@ -268,6 +268,14 @@ context "Rack::Utils::HeaderHash" do h = Rack::Utils::HeaderHash.new("foo" => "bar") h.delete("Hello").should.be.nil end + + specify "should avoid unnecessary object creation if possible" do + a = Rack::Utils::HeaderHash.new("foo" => "bar") + b = Rack::Utils::HeaderHash.new(a) + b.object_id.should.equal(a.object_id) + b.should.equal(a) + end + end context "Rack::Utils::Context" do -- 1.6.4.2.236.gf324c