From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS14383 205.234.109.0/24 X-Spam-Status: No, score=0.0 required=3.0 tests=MSGID_FROM_MTA_HEADER shortcircuit=no autolearn=unavailable version=3.3.2 Path: news.gmane.org!not-for-mail From: James Tucker Newsgroups: gmane.comp.lang.ruby.rainbows.general Subject: Re: [RFC/PATCH] event_machine: lazily set async env keys Date: Thu, 15 Jul 2010 15:22:29 +0100 Message-ID: References: <20100715060338.GA2561@dcvr.yhbt.net> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 (Apple Message framework v1081) Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1279203840 29526 80.91.229.12 (15 Jul 2010 14:24:00 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 15 Jul 2010 14:24:00 +0000 (UTC) To: Rainbows! list Original-X-From: rainbows-talk-bounces-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org Thu Jul 15 16:23:58 2010 Return-path: Envelope-to: gclrrg-rainbows-talk@m.gmane.org X-Original-To: rainbows-talk-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org Delivered-To: rainbows-talk-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:content-type:mime-version :subject:from:in-reply-to:date:content-transfer-encoding:message-id :references:to:x-mailer; bh=khkrrz1drI5JVSrntizS1/dRQaQbwZeCAbW3XfR4aqg=; b=L88PPoDVuiU2bBaigyROxBN74jhvpSCw9pl3V4vyMvrRYqfNwIvwgbFgik0iR49O1h 1Z01xCpoMVGn0gy2wFX2RpJEXEXRmCMI27tj3X7SlSKU+ILG3r0TJ1MZnCRF1sNY1nwA JmkczephFTqgpcfjRopKLbJLlgeBkNm1H7iE4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=content-type:mime-version:subject:from:in-reply-to:date :content-transfer-encoding:message-id:references:to:x-mailer; b=c0h2txmM1YaM2yh0KcFHe2muf2DbI6TGgUAXK/BVsZ8SPXeTf/ojh/sIB27LNeisma FG1cSdZKFBvV4IuWFf8TpNyg8nmiewgP6nSXCkFbIn/vtGvTbG5S0/kaP2jG7WykBBOe 5PJoCFW+ZgKX9vgPs7laZkQFK8JoP+EHw7lZw= In-Reply-To: <20100715060338.GA2561-yBiyF41qdooeIZ0/mPfg9Q@public.gmane.org> X-Mailer: Apple Mail (2.1081) X-BeenThere: rainbows-talk-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: rainbows-talk-bounces-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org Errors-To: rainbows-talk-bounces-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org Xref: news.gmane.org gmane.comp.lang.ruby.rainbows.general:102 Archived-At: Received: from rubyforge.org ([205.234.109.19]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1OZPLt-0002oF-Uy for gclrrg-rainbows-talk@m.gmane.org; Thu, 15 Jul 2010 16:23:54 +0200 Received: from rubyforge.org (rubyforge.org [127.0.0.1]) by rubyforge.org (Postfix) with ESMTP id 57DBC1858348; Thu, 15 Jul 2010 10:23:35 -0400 (EDT) Received: from mail-wy0-f178.google.com (mail-wy0-f178.google.com [74.125.82.178]) by rubyforge.org (Postfix) with ESMTP id 0195C1858348 for ; Thu, 15 Jul 2010 10:22:32 -0400 (EDT) Received: by wyb33 with SMTP id 33so838050wyb.23 for ; Thu, 15 Jul 2010 07:22:32 -0700 (PDT) Received: by 10.216.159.21 with SMTP id r21mr13711184wek.100.1279203752058; Thu, 15 Jul 2010 07:22:32 -0700 (PDT) Received: from [192.168.1.101] (bb-87-81-237-21.ukonline.co.uk [87.81.237.21]) by mx.google.com with ESMTPS id l55sm142131weq.41.2010.07.15.07.22.30 (version=TLSv1/SSLv3 cipher=RC4-MD5); Thu, 15 Jul 2010 07:22:30 -0700 (PDT) Sorry for the top post, trying to be quick... Just regarding the async close, it's used primarily when the client disconnects so that you can stop asynchronous operations. It's quite important from a resource standpoint. Why don't you just use a lambda instead of a method? lambda { |*args| em_write_response(*args) } That'd avoid the leak, and provide nearly the same performance profile. In fact on 1.9, lambdas are pretty damn quick. On 15 Jul 2010, at 07:03, Eric Wong wrote: > This has the small likelyhood of breaking apps that > check via: > env.include?("async.callback") > > Or any of member?/has_key?/key?, as they're all the same method. > >> From all the async examples I see, I just see apps just > use env["async.callback"]. > > Worth it? > >> From 40ef059dfc99c7daad1b1829713727b86c8fc9a0 Mon Sep 17 00:00:00 2001 > From: Eric Wong > Date: Thu, 15 Jul 2010 05:48:29 +0000 > Subject: [PATCH] event_machine: lazily set async env keys > > This is a micro-optimization when running a 'hello world' benchmark and > also reduces the strength of a memory leak under 1.9.2-rc2: > > http://redmine.ruby-lang.org/issues/show/3466 > --- > lib/rainbows/ev_core.rb | 1 - > lib/rainbows/event_machine.rb | 17 ++++++++++++----- > lib/rainbows/rev/client.rb | 1 + > 3 files changed, 13 insertions(+), 6 deletions(-) > > diff --git a/lib/rainbows/ev_core.rb b/lib/rainbows/ev_core.rb > index 5ca693b..357042b 100644 > --- a/lib/rainbows/ev_core.rb > +++ b/lib/rainbows/ev_core.rb > @@ -16,7 +16,6 @@ module Rainbows > > def post_init > @remote_addr = Rainbows.addr(@_io) > - @env = {} > @hp = HttpParser.new > @state = :headers # [ :body [ :trailers ] ] :app_call :close > @buf = "" > diff --git a/lib/rainbows/event_machine.rb b/lib/rainbows/event_machine.rb > index 173340e..14da118 100644 > --- a/lib/rainbows/event_machine.rb > +++ b/lib/rainbows/event_machine.rb > @@ -54,6 +54,17 @@ module Rainbows > G = Rainbows::G > > def initialize(io) > + @env = Hash.new do |hash,key| > + case key > + when ASYNC_CALLBACK > + hash[ASYNC_CALLBACK] = method(:em_write_response) > + when ASYNC_CLOSE > + # we're not sure if anybody uses this, but Thin sets it, too > + hash[ASYNC_CLOSE] = EM::DefaultDeferrable.new > + else > + nil > + end > + end > @_io = io > @body = nil > end > @@ -71,10 +82,6 @@ module Rainbows > begin > @env[RACK_INPUT] = @input > @env[REMOTE_ADDR] = @remote_addr > - @env[ASYNC_CALLBACK] = method(:em_write_response) > - > - # we're not sure if anybody uses this, but Thin sets it, too > - @env[ASYNC_CLOSE] = EM::DefaultDeferrable.new > > response = catch(:async) { APP.call(@env.update(RACK_DEFAULTS)) } > > @@ -143,7 +150,7 @@ module Rainbows > end > > def unbind > - async_close = @env[ASYNC_CLOSE] and async_close.succeed > + @env.include?(ASYNC_CLOSE) and @env[ASYNC_CLOSE].succeed > @body.respond_to?(:fail) and @body.fail > @_io.close > end > diff --git a/lib/rainbows/rev/client.rb b/lib/rainbows/rev/client.rb > index d08992b..0df7415 100644 > --- a/lib/rainbows/rev/client.rb > +++ b/lib/rainbows/rev/client.rb > @@ -15,6 +15,7 @@ module Rainbows > CONN[self] = false > super(io) > post_init > + @env = {} > @deferred_bodies = [] # for (fast) regular files only > end > > -- > Eric Wong > _______________________________________________ > Rainbows! mailing list - rainbows-talk-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org > http://rubyforge.org/mailman/listinfo/rainbows-talk > Do not quote signatures (like this one) or top post when replying _______________________________________________ Rainbows! mailing list - rainbows-talk-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org http://rubyforge.org/mailman/listinfo/rainbows-talk Do not quote signatures (like this one) or top post when replying