From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Status: No, score=-4.7 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.0 Received: from mail-io0-f187.google.com (mail-io0-f187.google.com [209.85.223.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 81C3920179 for ; Fri, 17 Jun 2016 23:12:40 +0000 (UTC) Received: by mail-io0-f187.google.com with SMTP id l5sf29001352ioa.1 for ; Fri, 17 Jun 2016 16:12:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20120806; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :x-original-sender:x-original-authentication-results:reply-to :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post :list-help:list-archive:list-subscribe:list-unsubscribe; bh=z1piCmvKXp04Hn7XImMPdUFDwovv47cliCD8Qj0VGdY=; b=amN0VuzhQYcAXsM+cs+0ySj49nbbWJv89ZS4WAvf6Afu+EKyBawb7pu2bJnENK1fGG 0bhIQ/7WwoM13QwVtoAvhUmpDcKn+QyLgNS1dDYvUHgeymqVfiO1pajPXmux7PBQnQto 9WbzJSSa0EK7d3eTEi7ZCcidaqRENLO4b78A3LPUCwqzMHXXOOAyIo5cJ563+Klsr3O/ pv8woNl7ul/pAoqy8e3bSaHcTBLUL3t1hhp7+ZImR6wpFB2loAheB3rzUukTNNr6cWx9 EAiKtzmmEJeXYo7NDwAtD7CZp6PrMNLr+fEAxZ1xJlkToB3KF0BoBdSE2ASfQd4tDLgj HSUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=sender:x-gm-message-state:date:from:to:cc:subject:message-id :references:mime-version:content-disposition :content-transfer-encoding:in-reply-to:x-original-sender :x-original-authentication-results:reply-to:precedence:mailing-list :list-id:x-spam-checked-in-group:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=z1piCmvKXp04Hn7XImMPdUFDwovv47cliCD8Qj0VGdY=; b=ZA3zI1FuohXJXYcDBB/fvEwdRfoKUfvbYRz2ALVAYvZ66TDVBGkMbTZIBOU3FEQorQ ZQ3NaIYDCFGrdu7Ej8RyllXjXdPIRTQBjsQyLE8A13I6Ob9v8qIgPiXcjR5t/2vACUYv I5PmCJFvMUOqpYLmICCfkMsYjblRa4WBjkLdWKxEslJqPGClhuKVC3Tf20n2WMV+jp+p 73TyLvrXTO3a26RUt/JsXynKjIbv6ejo4/sSsfNlvSPizJA24KzcRsjUaunu2gaoAjxn rbqkPM7NJ7EcstEAj/qOHDvwiqa1dTVjRQ5KylV/xbfAwB7zpYRDBwcPVC/xIXllbCTo reZg== Sender: rack-devel@googlegroups.com X-Gm-Message-State: ALyK8tK/tnaxDlbq+Yj38LbZXpW7m6KE+p027zujlA9DLPDRAl/Oz/s5sIxsE0Ggm6DmEQ== X-Received: by 10.157.45.136 with SMTP id g8mr178656otb.3.1466205159703; Fri, 17 Jun 2016 16:12:39 -0700 (PDT) X-BeenThere: rack-devel@googlegroups.com Received: by 10.157.52.137 with SMTP id g9ls1038685otc.41.gmail; Fri, 17 Jun 2016 16:12:39 -0700 (PDT) X-Received: by 10.129.131.68 with SMTP id t65mr10609987ywf.42.1466205159232; Fri, 17 Jun 2016 16:12:39 -0700 (PDT) Received: from dcvr.yhbt.net (dcvr.yhbt.net. [64.71.152.64]) by gmr-mx.google.com with ESMTPS id c11si3934152pfc.2.2016.06.17.16.12.38 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Jun 2016 16:12:39 -0700 (PDT) Received-SPF: pass (google.com: domain of e@80x24.org designates 64.71.152.64 as permitted sender) client-ip=64.71.152.64; Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 658671FEAA; Fri, 17 Jun 2016 23:12:38 +0000 (UTC) Date: Fri, 17 Jun 2016 23:12:39 +0000 From: Eric Wong To: Aaron Patterson Cc: richard schneeman , rack-devel@googlegroups.com Subject: Re: Less allocated objects on each request Message-ID: <20160617231239.GA16185@dcvr.yhbt.net> References: <20141003200836.GA4664@dcvr.yhbt.net> <20141005081525.GA8997@dcvr.yhbt.net> <20160512030454.GA10916@dcvr.yhbt.net> <20160512165456.GA79225@TC.local> <20160513234135.GA24167@dcvr.yhbt.net> <20160617210057.GD67772@TC.local> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable In-Reply-To: <20160617210057.GD67772@TC.local> X-Original-Sender: e@80x24.org X-Original-Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of e@80x24.org designates 64.71.152.64 as permitted sender) smtp.mailfrom=e@80x24.org Reply-To: rack-devel@googlegroups.com Precedence: list Mailing-list: list rack-devel@googlegroups.com; contact rack-devel+owners@googlegroups.com List-ID: X-Google-Group-Id: 486215384060 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , Aaron Patterson wrote: > On Fri, Jun 17, 2016 at 02:43:54PM -0500, richard schneeman wrote: > > I have a PR that removes all those constants in rack.rb in favor of str= ing literals: > > https://github.com/rack/rack/pull/1085 >=20 > Eric, >=20 > Here's a link to the raw diff if you want to take a look: >=20 > https://patch-diff.githubusercontent.com/raw/rack/rack/pull/1085.diff Thanks, I added "fetch =3D +refs/pull/*:refs/remotes/pull/*" to the remote section of my .git/config to fetch it down to get my usual git diff/log views. > Now that I'm reading this patch, removing the constants worries me > because of backwards compatibility issues. I think removing the > `freeze` is fine, and setting the magic comment to use frozen strings > would be good. But removing the constants seems dangerous since other > libraries use those constants. Agreed. In my experience, adding the frozen_string_literal magic comment to existing code needs to be done very carefully. I suggest local "literal".freeze for method args where it's difficult to audit and beefing up test cases to ensure breakage is not introduced. > Could we just remove the freeze and add the magic comment to that file > that defines the constants? >=20 > > I also found some places where we should still use `.freeze` in a few c= ases. There are also some other constants buried around the project, this P= R is only the main constants. > >=20 > > Eric & Aaron: on a separate non-rack note, I could use some of your Rub= y internals expertise on a performance tuning patch to Sprockets involving = `case`. Mostly why is the `case` code not faster than using a hash. Here is= the discussion:=C2=A0 > >=20 > > https://github.com/rails/sprockets/pull/312#issuecomment-226369234 >=20 > I left a comment. Basically the case statement is doing an is_a? where > the hash version is just doing an equality on the class. That's part of it. case/when is a linear scan in this case. case/when gets transparently optimized into a hash lookup into a jump table when all the potential "when" matches are frozen literals. Ruby can't optimize constants for case/when because constants aren't constant like frozen literals are[1] As an experiment, it might be worth it to see how only having literal string matches affects performance: case value.class.to_s when "String", "Symbol" when "Array" something else something_else end This will compile to the opt_case_dispatch instruction and require no extra allocations for the literals. (but too ugly for real code) Throwing a non-literal in there will break the optimization: case value.class.to_s when "String", "Symbol" when Array.to_s # oops! something else something_else end The following also breaks the opt_case_dispatch optimization so it becomes a linear scan, but it still avoids allocating: case value.class.to_s when "String".freeze, "Symbol".freeze when "Array".freeze something else something_else end [1] - perhaps the VM constant cache can be used in the future for optimizing case dispatch with constants, though /me crawls back into his Perl 5 cave :) --=20 ---=20 You received this message because you are subscribed to the Google Groups "= Rack Development" group. To unsubscribe from this group and stop receiving emails from it, send an e= mail to rack-devel+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/d/optout.