1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

speed up accept header parsing a bit.

Accept header is taken from what Safari on El Capitan sends:

```ruby
require 'benchmark/ips'
require 'action_dispatch/http/mime_type'
require 'active_support/all'

accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'

Benchmark.ips do |x|
  x.report "omg" do
    Mime::Type.parse(accept)
  end
end
```

Before:

```
[aaron@TC actionpack (master)]$ be ruby ../x.rb
Calculating -------------------------------------
                 omg     3.181k i/100ms
-------------------------------------------------
                 omg     35.062k (±12.8%) i/s -    174.955k
[aaron@TC actionpack (master)]$ be ruby ../x.rb
Calculating -------------------------------------
                 omg     3.153k i/100ms
-------------------------------------------------
                 omg     33.724k (±12.4%) i/s -    167.109k
[aaron@TC actionpack (master)]$ be ruby ../x.rb
Calculating -------------------------------------
                 omg     3.575k i/100ms
-------------------------------------------------
                 omg     37.251k (±10.4%) i/s -    185.900k
```

After:

```
[aaron@TC actionpack (master)]$ be ruby ../x.rb
Calculating -------------------------------------
                 omg     3.365k i/100ms
-------------------------------------------------
                 omg     40.069k (±16.1%) i/s -    198.535k
[aaron@TC actionpack (master)]$ be ruby ../x.rb
Calculating -------------------------------------
                 omg     4.168k i/100ms
-------------------------------------------------
                 omg     47.596k (± 7.7%) i/s -    237.576k
[aaron@TC actionpack (master)]$ be ruby ../x.rb
Calculating -------------------------------------
                 omg     4.282k i/100ms
-------------------------------------------------
                 omg     43.626k (±17.7%) i/s -    209.818k
```
This commit is contained in:
Aaron Patterson 2016-01-28 16:08:03 -08:00
parent ee9d479941
commit c082a7aae4

View file

@ -1,3 +1,5 @@
# -*- frozen-string-literal: true -*-
require 'singleton'
require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/core_ext/string/starts_ends_with'
@ -157,7 +159,7 @@ module Mime
end
class << self
TRAILING_STAR_REGEXP = /(text|application)\/\*/
TRAILING_STAR_REGEXP = /^(text|application)\/\*/
PARAMETER_SEPARATOR_REGEXP = /;\s*\w+="?\w+"?/
def register_callback(&block)
@ -200,8 +202,10 @@ module Mime
list, index = [], 0
accept_header.split(',').each do |header|
params, q = header.split(PARAMETER_SEPARATOR_REGEXP)
if params.present?
next unless params
params.strip!
next if params.empty?
params = parse_trailing_star(params) || [params]
@ -210,7 +214,6 @@ module Mime
index += 1
end
end
end
AcceptList.sort! list
end
end