rack-devel archive mirror (unofficial) https://groups.google.com/group/rack-devel
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: rack-devel@googlegroups.com
Cc: richard schneeman <richard.schneeman@gmail.com>
Subject: Re: Less allocated objects on each request
Date: Thu, 12 May 2016 03:04:54 +0000	[thread overview]
Message-ID: <20160512030454.GA10916@dcvr.yhbt.net> (raw)
In-Reply-To: <e81ed0c2-2b98-48b8-84b4-0e55eefe8c78@googlegroups.com>

richard schneeman <richard.schneeman@gmail.com> wrote:
> That first SVN commit is
>  
> *https://github.com/ruby/ruby/commit/779ae78995977305aa5aec9cb5b562dcf54c22e7*
> 
> The second commit is:
> *https://github.com/ruby/ruby/commit/58f800a278b8b70463f4afdbb23a918d8ab441ff*
> 
> Eric's proposal: *https://bugs.ruby-lang.org/issues/9425*
> 
> 
> I agree that the patch should one day be unnecessary,

<snip>  Hi Richard, ressurrecting since I believe the day has come :)

Since rack.git depends on Ruby 2.2.2+ which has the
aforementioned string deduplication, I think we can undo
most (if not all) of these ugly constants and reduce constant
cache overheads while we're at it.

I can followup with patches to the list hopefully in the next
few weeks if nobody beats me to it (occupied with other stuff,
at the moment)

To summarize, the following are optimized in 2.2 (or earlier):

  { "foo" => "bar" } # "foo" is deduped
                     # "bar" is duplicated unless 'frozen_string_literal: true'

  base_hash["foo"] = "bar" # same as above
  base_hash["foo"]         # "foo" is deduped ditto

The Hash#[] and Hash#[]= optimizations unfortunately do not
transfer to subclasses like HeaderHash.

  # string literals in the "when" statement
  # this has been optimized for many years, now.
  case var
  when "foo"
    do_foo
  when "bar"
    do_bar
  end

"string".freeze on any string literal is deduped since 2.1,
which might be useful for performance-critical subclasses of Hash.

"frozen_string_literal: true" magic comment exists since 2.3;
but I'm not sure how much it helps and it's easy to introduce
new bugs, so I'm not enthusiastic about it for old code.

Regardless, constants are expensive in both space and time.  The
inline constant cache takes space, and the cache needs to be
checked every time a constant is accessed.  String literals do
not have that cost.

Constants still have one minor advantage: runtime NameError in
case of typos.  But I don't think that benefit is worth it
as they make code harder-to-read.

I mainly bring this up because I had trouble following some
of the hijack code for webrick:
http://mid.gmane.org/20160511050451.GA23544@dcvr.yhbt.net

(Only speaking for MRI optimizations, I don't know other VMs)

-- 

--- 
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 email to rack-devel+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

  reply	other threads:[~2016-05-12  3:04 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-03 20:08 Less allocated objects on each request Eric Wong
2014-10-05  8:15 ` [PATCH] conditionalget: avoid const lookup in case/when Eric Wong
2014-10-05 21:37   ` James Tucker
2014-10-06 20:00     ` richard schneeman
2016-05-12  3:04       ` Eric Wong [this message]
2016-05-12 16:54         ` Less allocated objects on each request Aaron Patterson
2016-05-12 17:01           ` richard schneeman
2016-05-13 23:41           ` Eric Wong
2016-06-17 19:43             ` richard schneeman
2016-06-17 21:00               ` Aaron Patterson
2016-06-17 23:12                 ` Eric Wong
2016-06-17 23:19                   ` Aaron Patterson
2016-06-17 23:43                     ` Eric Wong
2014-10-05 21:31 ` James Tucker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://groups.google.com/group/rack-devel

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160512030454.GA10916@dcvr.yhbt.net \
    --to=rack-devel@googlegroups.com \
    --cc=richard.schneeman@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).