diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2016-01-28 16:08:03 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2016-01-28 16:08:15 -0800 |
commit | c082a7aae4a41b52e58c7dea43e7e9c6c41d11ad (patch) | |
tree | d763ac67ee2b5e1bfc8c97dff31b92bf5aa66331 /actionpack | |
parent | ee9d479941061acdec6b3a6a3805408624f8cc22 (diff) | |
download | rails-c082a7aae4a41b52e58c7dea43e7e9c6c41d11ad.tar.gz rails-c082a7aae4a41b52e58c7dea43e7e9c6c41d11ad.tar.bz2 rails-c082a7aae4a41b52e58c7dea43e7e9c6c41d11ad.zip |
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
```
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_dispatch/http/mime_type.rb | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/actionpack/lib/action_dispatch/http/mime_type.rb b/actionpack/lib/action_dispatch/http/mime_type.rb index 065d7dd355..4672ea7199 100644 --- a/actionpack/lib/action_dispatch/http/mime_type.rb +++ b/actionpack/lib/action_dispatch/http/mime_type.rb @@ -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,15 +202,16 @@ module Mime list, index = [], 0 accept_header.split(',').each do |header| params, q = header.split(PARAMETER_SEPARATOR_REGEXP) - if params.present? - params.strip! - params = parse_trailing_star(params) || [params] + next unless params + params.strip! + next if params.empty? + + params = parse_trailing_star(params) || [params] - params.each do |m| - list << AcceptItem.new(index, m.to_s, q) - index += 1 - end + params.each do |m| + list << AcceptItem.new(index, m.to_s, q) + index += 1 end end AcceptList.sort! list |