From: Jean Boussier <jean.boussier@shopify.com> To: Eric Wong <e@yhbt.net> Cc: unicorn-public@yhbt.net Subject: Re: [PATCH] Add early hints support Date: Mon, 20 Jul 2020 12:27:14 +0200 [thread overview] Message-ID: <F908D215-8646-450C-BA7F-9704F1E9D738@shopify.com> (raw) In-Reply-To: <20200720100958.GA7043@dcvr> > `match?' is Ruby 2.4+, which is probably too big a jump since > we're still on Ruby 1.9.3 at the moment... That's what I figured. > String comparison as in `==' and `!='? Would be interested to know > where and what improvements can be had. One place that jumped to mind when I saw it is http_response_write. But there are many other places where Regexp are used to do case insensitive comparisons. ``` require 'benchmark/ips' def http_response_write(headers) headers.each do |key, value| case key when %r{\A(?:Date|Connection)\z}i next end end end def http_response_write_upcase(headers) headers.each do |key, value| case key.upcase when 'DATE'.freeze, 'CONNECTION'.freeze next end end end def http_response_write_casecmp(headers) headers.each do |key, value| case key when key.casecmp?('Date'.freeze) || key.casecmp?('Connection'.freeze) next end end end HEADERS = { 'Foo' => 'bar', 'Date' => 'plop', 'User-Agent' => 'blah', } Benchmark.ips do |x| x.report('original') { http_response_write(HEADERS) } x.report('upcase') { http_response_write_upcase(HEADERS) } x.report('casecmp?') { http_response_write_casecmp(HEADERS) } x.compare! end ``` ``` Warming up -------------------------------------- original 82.066k i/100ms upcase 177.429k i/100ms casecmp? 96.288k i/100ms Calculating ------------------------------------- original 831.610k (± 1.6%) i/s - 4.185M in 5.034146s upcase 1.770M (± 1.6%) i/s - 8.871M in 5.013796s casecmp? 979.618k (± 1.3%) i/s - 4.911M in 5.013678s Comparison: upcase: 1769883.2 i/s casecmp?: 979618.3 i/s - 1.81x (± 0.00) slower original: 831610.2 i/s - 2.13x (± 0.00) slower ``` Similarly, that method use `value =~ /\n/` which could be replaced favorably for `value.include?("\n".freeze)` ``` VAL = "foobar" Benchmark.ips do |x| x.report('=~') { VAL =~ /\n/ } x.report('include?') { VAL.include?("\n".freeze) } x.compare! end ``` ``` Warming up -------------------------------------- =~ 409.096k i/100ms include? 1.322M i/100ms Calculating ------------------------------------- =~ 4.083M (± 2.1%) i/s - 20.455M in 5.011539s include? 13.053M (± 1.5%) i/s - 66.125M in 5.067097s Comparison: include?: 13052859.2 i/s =~: 4083401.3 i/s - 3.20x (± 0.00) slower ``` > Ruby just seems hopeless performance-wise Well, the gap between 1.9.3 and 2.5+ is pretty big performance-wise.
next prev parent reply other threads:[~2020-07-20 10:27 UTC|newest] Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-07-16 10:05 Jean Boussier 2020-07-16 10:50 ` Eric Wong 2020-07-16 11:41 ` Jean Boussier 2020-07-16 12:16 ` Eric Wong 2020-07-16 12:24 ` Jean Boussier 2020-07-17 1:19 ` Eric Wong 2020-07-20 9:18 ` Jean Boussier 2020-07-20 10:09 ` Eric Wong 2020-07-20 10:27 ` Jean Boussier [this message] 2020-07-20 10:55 ` Eric Wong 2020-07-20 11:53 ` Jean Boussier 2020-07-20 20:27 ` Eric Wong
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-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style List information: https://yhbt.net/unicorn/ * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=F908D215-8646-450C-BA7F-9704F1E9D738@shopify.com \ --to=jean.boussier@shopify.com \ --cc=e@yhbt.net \ --cc=unicorn-public@yhbt.net \ --subject='Re: [PATCH] Add early hints support' \ /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
Code repositories for project(s) associated with this inbox: ../../unicorn.git 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).