2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2012-12-19 15:54:47 -05:00
|
|
|
module ActionDispatch
|
2016-10-26 13:25:02 -04:00
|
|
|
# :stopdoc:
|
|
|
|
module Journey
|
|
|
|
class Route
|
2015-08-20 18:34:18 -04:00
|
|
|
attr_reader :app, :path, :defaults, :name, :precedence
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2016-02-13 22:18:22 -05:00
|
|
|
attr_reader :constraints, :internal
|
2012-12-19 15:54:47 -05:00
|
|
|
alias :conditions :constraints
|
|
|
|
|
use the strategy pattern to match request verbs
Rather than building a regexp for every route, lets use the strategy
pattern to select among objects that can match HTTP verbs. This commit
introduces strategy objects for each verb that has a predicate method on
the request object like `get?`, `post?`, etc.
When we build the route object, look up the strategy for the verbs the
user specified. If we can't find it, fall back on string matching.
Using a strategy / null object pattern (the `All` VerbMatcher is our
"null" object in this case) we can:
1) Remove conditionals
2) Drop boot time allocations
2) Drop run time allocations
3) Improve runtime performance
Here is our boot time allocation benchmark:
```ruby
require 'action_pack'
require 'action_dispatch'
route_set = ActionDispatch::Routing::RouteSet.new
routes = ActionDispatch::Routing::Mapper.new route_set
result = ObjectSpace::AllocationTracer.trace do
500.times do
routes.resources :foo
end
end
sorted = ObjectSpace::AllocationTracer.allocated_count_table.sort_by(&:last)
sorted.each do |k,v|
next if v == 0
p k => v
end
__END__
Before:
$ be ruby -rallocation_tracer route_test.rb
{:T_SYMBOL=>11}
{:T_REGEXP=>4017}
{:T_STRUCT=>6500}
{:T_MATCH=>12004}
{:T_DATA=>84092}
{:T_OBJECT=>99009}
{:T_HASH=>122015}
{:T_STRING=>216652}
{:T_IMEMO=>355137}
{:T_ARRAY=>441057}
After:
$ be ruby -rallocation_tracer route_test.rb
{:T_SYMBOL=>11}
{:T_REGEXP=>17}
{:T_STRUCT=>6500}
{:T_MATCH=>12004}
{:T_DATA=>84092}
{:T_OBJECT=>99009}
{:T_HASH=>122015}
{:T_STRING=>172647}
{:T_IMEMO=>355136}
{:T_ARRAY=>433056}
```
This benchmark adds 500 resources. Each resource has 8 routes, so it
adds 4000 routes. You can see from the results that this patch
eliminates 4000 Regexp allocations, ~44000 String allocations, and ~8000
Array allocations. With that, we can figure out that the previous code
would allocate 1 regexp, 11 strings, and 2 arrays per route *more* than
this patch in order to handle verb matching.
Next lets look at runtime allocations:
```ruby
require 'action_pack'
require 'action_dispatch'
require 'benchmark/ips'
route_set = ActionDispatch::Routing::RouteSet.new
routes = ActionDispatch::Routing::Mapper.new route_set
routes.resources :foo
route = route_set.routes.first
request = ActionDispatch::Request.new("REQUEST_METHOD" => "GET")
result = ObjectSpace::AllocationTracer.trace do
500.times do
route.matches? request
end
end
sorted = ObjectSpace::AllocationTracer.allocated_count_table.sort_by(&:last)
sorted.each do |k,v|
next if v == 0
p k => v
end
__END__
Before:
$ be ruby -rallocation_tracer route_test.rb
{:T_MATCH=>500}
{:T_STRING=>501}
{:T_IMEMO=>1501}
After:
$ be ruby -rallocation_tracer route_test.rb
{:T_IMEMO=>1001}
```
This benchmark runs 500 calls against the `matches?` method on the route
object. We check this method in the case that there are two methods
that match the same path, but they are differentiated by the verb (or
other conditionals). For example `POST /users` vs `GET /users`, same
path, different action.
Previously, we were using regexps to match against the verb. You can
see that doing the regexp match would allocate 1 match object and 1
string object each time it was called. This patch eliminates those
allocations.
Next lets look at runtime performance.
```ruby
require 'action_pack'
require 'action_dispatch'
require 'benchmark/ips'
route_set = ActionDispatch::Routing::RouteSet.new
routes = ActionDispatch::Routing::Mapper.new route_set
routes.resources :foo
route = route_set.routes.first
match = ActionDispatch::Request.new("REQUEST_METHOD" => "GET")
no_match = ActionDispatch::Request.new("REQUEST_METHOD" => "POST")
Benchmark.ips do |x|
x.report("match") do
route.matches? match
end
x.report("no match") do
route.matches? no_match
end
end
__END__
Before:
$ be ruby -rallocation_tracer runtime.rb
Calculating -------------------------------------
match 17.145k i/100ms
no match 24.244k i/100ms
-------------------------------------------------
match 259.708k (± 4.3%) i/s - 1.303M
no match 453.376k (± 5.9%) i/s - 2.279M
After:
$ be ruby -rallocation_tracer runtime.rb
Calculating -------------------------------------
match 23.958k i/100ms
no match 29.402k i/100ms
-------------------------------------------------
match 465.063k (± 3.8%) i/s - 2.324M
no match 691.956k (± 4.5%) i/s - 3.469M
```
This tests tries to see how many times it can match a request per
second. Switching to method calls and string comparison makes the
successful match case about 79% faster, and the unsuccessful case about
52% faster.
That was fun!
2015-08-17 21:17:55 -04:00
|
|
|
module VerbMatchers
|
|
|
|
VERBS = %w{ DELETE GET HEAD OPTIONS LINK PATCH POST PUT TRACE UNLINK }
|
|
|
|
VERBS.each do |v|
|
2017-05-23 02:08:59 -04:00
|
|
|
class_eval <<-eoc, __FILE__, __LINE__ + 1
|
|
|
|
class #{v}
|
|
|
|
def self.verb; name.split("::").last; end
|
|
|
|
def self.call(req); req.#{v.downcase}?; end
|
|
|
|
end
|
use the strategy pattern to match request verbs
Rather than building a regexp for every route, lets use the strategy
pattern to select among objects that can match HTTP verbs. This commit
introduces strategy objects for each verb that has a predicate method on
the request object like `get?`, `post?`, etc.
When we build the route object, look up the strategy for the verbs the
user specified. If we can't find it, fall back on string matching.
Using a strategy / null object pattern (the `All` VerbMatcher is our
"null" object in this case) we can:
1) Remove conditionals
2) Drop boot time allocations
2) Drop run time allocations
3) Improve runtime performance
Here is our boot time allocation benchmark:
```ruby
require 'action_pack'
require 'action_dispatch'
route_set = ActionDispatch::Routing::RouteSet.new
routes = ActionDispatch::Routing::Mapper.new route_set
result = ObjectSpace::AllocationTracer.trace do
500.times do
routes.resources :foo
end
end
sorted = ObjectSpace::AllocationTracer.allocated_count_table.sort_by(&:last)
sorted.each do |k,v|
next if v == 0
p k => v
end
__END__
Before:
$ be ruby -rallocation_tracer route_test.rb
{:T_SYMBOL=>11}
{:T_REGEXP=>4017}
{:T_STRUCT=>6500}
{:T_MATCH=>12004}
{:T_DATA=>84092}
{:T_OBJECT=>99009}
{:T_HASH=>122015}
{:T_STRING=>216652}
{:T_IMEMO=>355137}
{:T_ARRAY=>441057}
After:
$ be ruby -rallocation_tracer route_test.rb
{:T_SYMBOL=>11}
{:T_REGEXP=>17}
{:T_STRUCT=>6500}
{:T_MATCH=>12004}
{:T_DATA=>84092}
{:T_OBJECT=>99009}
{:T_HASH=>122015}
{:T_STRING=>172647}
{:T_IMEMO=>355136}
{:T_ARRAY=>433056}
```
This benchmark adds 500 resources. Each resource has 8 routes, so it
adds 4000 routes. You can see from the results that this patch
eliminates 4000 Regexp allocations, ~44000 String allocations, and ~8000
Array allocations. With that, we can figure out that the previous code
would allocate 1 regexp, 11 strings, and 2 arrays per route *more* than
this patch in order to handle verb matching.
Next lets look at runtime allocations:
```ruby
require 'action_pack'
require 'action_dispatch'
require 'benchmark/ips'
route_set = ActionDispatch::Routing::RouteSet.new
routes = ActionDispatch::Routing::Mapper.new route_set
routes.resources :foo
route = route_set.routes.first
request = ActionDispatch::Request.new("REQUEST_METHOD" => "GET")
result = ObjectSpace::AllocationTracer.trace do
500.times do
route.matches? request
end
end
sorted = ObjectSpace::AllocationTracer.allocated_count_table.sort_by(&:last)
sorted.each do |k,v|
next if v == 0
p k => v
end
__END__
Before:
$ be ruby -rallocation_tracer route_test.rb
{:T_MATCH=>500}
{:T_STRING=>501}
{:T_IMEMO=>1501}
After:
$ be ruby -rallocation_tracer route_test.rb
{:T_IMEMO=>1001}
```
This benchmark runs 500 calls against the `matches?` method on the route
object. We check this method in the case that there are two methods
that match the same path, but they are differentiated by the verb (or
other conditionals). For example `POST /users` vs `GET /users`, same
path, different action.
Previously, we were using regexps to match against the verb. You can
see that doing the regexp match would allocate 1 match object and 1
string object each time it was called. This patch eliminates those
allocations.
Next lets look at runtime performance.
```ruby
require 'action_pack'
require 'action_dispatch'
require 'benchmark/ips'
route_set = ActionDispatch::Routing::RouteSet.new
routes = ActionDispatch::Routing::Mapper.new route_set
routes.resources :foo
route = route_set.routes.first
match = ActionDispatch::Request.new("REQUEST_METHOD" => "GET")
no_match = ActionDispatch::Request.new("REQUEST_METHOD" => "POST")
Benchmark.ips do |x|
x.report("match") do
route.matches? match
end
x.report("no match") do
route.matches? no_match
end
end
__END__
Before:
$ be ruby -rallocation_tracer runtime.rb
Calculating -------------------------------------
match 17.145k i/100ms
no match 24.244k i/100ms
-------------------------------------------------
match 259.708k (± 4.3%) i/s - 1.303M
no match 453.376k (± 5.9%) i/s - 2.279M
After:
$ be ruby -rallocation_tracer runtime.rb
Calculating -------------------------------------
match 23.958k i/100ms
no match 29.402k i/100ms
-------------------------------------------------
match 465.063k (± 3.8%) i/s - 2.324M
no match 691.956k (± 4.5%) i/s - 3.469M
```
This tests tries to see how many times it can match a request per
second. Switching to method calls and string comparison makes the
successful match case about 79% faster, and the unsuccessful case about
52% faster.
That was fun!
2015-08-17 21:17:55 -04:00
|
|
|
eoc
|
|
|
|
end
|
|
|
|
|
|
|
|
class Unknown
|
|
|
|
attr_reader :verb
|
|
|
|
|
|
|
|
def initialize(verb)
|
|
|
|
@verb = verb
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(request); @verb === request.request_method; end
|
|
|
|
end
|
|
|
|
|
|
|
|
class All
|
|
|
|
def self.call(_); true; end
|
2016-08-06 12:51:43 -04:00
|
|
|
def self.verb; ""; end
|
use the strategy pattern to match request verbs
Rather than building a regexp for every route, lets use the strategy
pattern to select among objects that can match HTTP verbs. This commit
introduces strategy objects for each verb that has a predicate method on
the request object like `get?`, `post?`, etc.
When we build the route object, look up the strategy for the verbs the
user specified. If we can't find it, fall back on string matching.
Using a strategy / null object pattern (the `All` VerbMatcher is our
"null" object in this case) we can:
1) Remove conditionals
2) Drop boot time allocations
2) Drop run time allocations
3) Improve runtime performance
Here is our boot time allocation benchmark:
```ruby
require 'action_pack'
require 'action_dispatch'
route_set = ActionDispatch::Routing::RouteSet.new
routes = ActionDispatch::Routing::Mapper.new route_set
result = ObjectSpace::AllocationTracer.trace do
500.times do
routes.resources :foo
end
end
sorted = ObjectSpace::AllocationTracer.allocated_count_table.sort_by(&:last)
sorted.each do |k,v|
next if v == 0
p k => v
end
__END__
Before:
$ be ruby -rallocation_tracer route_test.rb
{:T_SYMBOL=>11}
{:T_REGEXP=>4017}
{:T_STRUCT=>6500}
{:T_MATCH=>12004}
{:T_DATA=>84092}
{:T_OBJECT=>99009}
{:T_HASH=>122015}
{:T_STRING=>216652}
{:T_IMEMO=>355137}
{:T_ARRAY=>441057}
After:
$ be ruby -rallocation_tracer route_test.rb
{:T_SYMBOL=>11}
{:T_REGEXP=>17}
{:T_STRUCT=>6500}
{:T_MATCH=>12004}
{:T_DATA=>84092}
{:T_OBJECT=>99009}
{:T_HASH=>122015}
{:T_STRING=>172647}
{:T_IMEMO=>355136}
{:T_ARRAY=>433056}
```
This benchmark adds 500 resources. Each resource has 8 routes, so it
adds 4000 routes. You can see from the results that this patch
eliminates 4000 Regexp allocations, ~44000 String allocations, and ~8000
Array allocations. With that, we can figure out that the previous code
would allocate 1 regexp, 11 strings, and 2 arrays per route *more* than
this patch in order to handle verb matching.
Next lets look at runtime allocations:
```ruby
require 'action_pack'
require 'action_dispatch'
require 'benchmark/ips'
route_set = ActionDispatch::Routing::RouteSet.new
routes = ActionDispatch::Routing::Mapper.new route_set
routes.resources :foo
route = route_set.routes.first
request = ActionDispatch::Request.new("REQUEST_METHOD" => "GET")
result = ObjectSpace::AllocationTracer.trace do
500.times do
route.matches? request
end
end
sorted = ObjectSpace::AllocationTracer.allocated_count_table.sort_by(&:last)
sorted.each do |k,v|
next if v == 0
p k => v
end
__END__
Before:
$ be ruby -rallocation_tracer route_test.rb
{:T_MATCH=>500}
{:T_STRING=>501}
{:T_IMEMO=>1501}
After:
$ be ruby -rallocation_tracer route_test.rb
{:T_IMEMO=>1001}
```
This benchmark runs 500 calls against the `matches?` method on the route
object. We check this method in the case that there are two methods
that match the same path, but they are differentiated by the verb (or
other conditionals). For example `POST /users` vs `GET /users`, same
path, different action.
Previously, we were using regexps to match against the verb. You can
see that doing the regexp match would allocate 1 match object and 1
string object each time it was called. This patch eliminates those
allocations.
Next lets look at runtime performance.
```ruby
require 'action_pack'
require 'action_dispatch'
require 'benchmark/ips'
route_set = ActionDispatch::Routing::RouteSet.new
routes = ActionDispatch::Routing::Mapper.new route_set
routes.resources :foo
route = route_set.routes.first
match = ActionDispatch::Request.new("REQUEST_METHOD" => "GET")
no_match = ActionDispatch::Request.new("REQUEST_METHOD" => "POST")
Benchmark.ips do |x|
x.report("match") do
route.matches? match
end
x.report("no match") do
route.matches? no_match
end
end
__END__
Before:
$ be ruby -rallocation_tracer runtime.rb
Calculating -------------------------------------
match 17.145k i/100ms
no match 24.244k i/100ms
-------------------------------------------------
match 259.708k (± 4.3%) i/s - 1.303M
no match 453.376k (± 5.9%) i/s - 2.279M
After:
$ be ruby -rallocation_tracer runtime.rb
Calculating -------------------------------------
match 23.958k i/100ms
no match 29.402k i/100ms
-------------------------------------------------
match 465.063k (± 3.8%) i/s - 2.324M
no match 691.956k (± 4.5%) i/s - 3.469M
```
This tests tries to see how many times it can match a request per
second. Switching to method calls and string comparison makes the
successful match case about 79% faster, and the unsuccessful case about
52% faster.
That was fun!
2015-08-17 21:17:55 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:44:11 -04:00
|
|
|
VERB_TO_CLASS = VERBS.each_with_object(all: All) do |verb, hash|
|
use the strategy pattern to match request verbs
Rather than building a regexp for every route, lets use the strategy
pattern to select among objects that can match HTTP verbs. This commit
introduces strategy objects for each verb that has a predicate method on
the request object like `get?`, `post?`, etc.
When we build the route object, look up the strategy for the verbs the
user specified. If we can't find it, fall back on string matching.
Using a strategy / null object pattern (the `All` VerbMatcher is our
"null" object in this case) we can:
1) Remove conditionals
2) Drop boot time allocations
2) Drop run time allocations
3) Improve runtime performance
Here is our boot time allocation benchmark:
```ruby
require 'action_pack'
require 'action_dispatch'
route_set = ActionDispatch::Routing::RouteSet.new
routes = ActionDispatch::Routing::Mapper.new route_set
result = ObjectSpace::AllocationTracer.trace do
500.times do
routes.resources :foo
end
end
sorted = ObjectSpace::AllocationTracer.allocated_count_table.sort_by(&:last)
sorted.each do |k,v|
next if v == 0
p k => v
end
__END__
Before:
$ be ruby -rallocation_tracer route_test.rb
{:T_SYMBOL=>11}
{:T_REGEXP=>4017}
{:T_STRUCT=>6500}
{:T_MATCH=>12004}
{:T_DATA=>84092}
{:T_OBJECT=>99009}
{:T_HASH=>122015}
{:T_STRING=>216652}
{:T_IMEMO=>355137}
{:T_ARRAY=>441057}
After:
$ be ruby -rallocation_tracer route_test.rb
{:T_SYMBOL=>11}
{:T_REGEXP=>17}
{:T_STRUCT=>6500}
{:T_MATCH=>12004}
{:T_DATA=>84092}
{:T_OBJECT=>99009}
{:T_HASH=>122015}
{:T_STRING=>172647}
{:T_IMEMO=>355136}
{:T_ARRAY=>433056}
```
This benchmark adds 500 resources. Each resource has 8 routes, so it
adds 4000 routes. You can see from the results that this patch
eliminates 4000 Regexp allocations, ~44000 String allocations, and ~8000
Array allocations. With that, we can figure out that the previous code
would allocate 1 regexp, 11 strings, and 2 arrays per route *more* than
this patch in order to handle verb matching.
Next lets look at runtime allocations:
```ruby
require 'action_pack'
require 'action_dispatch'
require 'benchmark/ips'
route_set = ActionDispatch::Routing::RouteSet.new
routes = ActionDispatch::Routing::Mapper.new route_set
routes.resources :foo
route = route_set.routes.first
request = ActionDispatch::Request.new("REQUEST_METHOD" => "GET")
result = ObjectSpace::AllocationTracer.trace do
500.times do
route.matches? request
end
end
sorted = ObjectSpace::AllocationTracer.allocated_count_table.sort_by(&:last)
sorted.each do |k,v|
next if v == 0
p k => v
end
__END__
Before:
$ be ruby -rallocation_tracer route_test.rb
{:T_MATCH=>500}
{:T_STRING=>501}
{:T_IMEMO=>1501}
After:
$ be ruby -rallocation_tracer route_test.rb
{:T_IMEMO=>1001}
```
This benchmark runs 500 calls against the `matches?` method on the route
object. We check this method in the case that there are two methods
that match the same path, but they are differentiated by the verb (or
other conditionals). For example `POST /users` vs `GET /users`, same
path, different action.
Previously, we were using regexps to match against the verb. You can
see that doing the regexp match would allocate 1 match object and 1
string object each time it was called. This patch eliminates those
allocations.
Next lets look at runtime performance.
```ruby
require 'action_pack'
require 'action_dispatch'
require 'benchmark/ips'
route_set = ActionDispatch::Routing::RouteSet.new
routes = ActionDispatch::Routing::Mapper.new route_set
routes.resources :foo
route = route_set.routes.first
match = ActionDispatch::Request.new("REQUEST_METHOD" => "GET")
no_match = ActionDispatch::Request.new("REQUEST_METHOD" => "POST")
Benchmark.ips do |x|
x.report("match") do
route.matches? match
end
x.report("no match") do
route.matches? no_match
end
end
__END__
Before:
$ be ruby -rallocation_tracer runtime.rb
Calculating -------------------------------------
match 17.145k i/100ms
no match 24.244k i/100ms
-------------------------------------------------
match 259.708k (± 4.3%) i/s - 1.303M
no match 453.376k (± 5.9%) i/s - 2.279M
After:
$ be ruby -rallocation_tracer runtime.rb
Calculating -------------------------------------
match 23.958k i/100ms
no match 29.402k i/100ms
-------------------------------------------------
match 465.063k (± 3.8%) i/s - 2.324M
no match 691.956k (± 4.5%) i/s - 3.469M
```
This tests tries to see how many times it can match a request per
second. Switching to method calls and string comparison makes the
successful match case about 79% faster, and the unsuccessful case about
52% faster.
That was fun!
2015-08-17 21:17:55 -04:00
|
|
|
klass = const_get verb
|
|
|
|
hash[verb] = klass
|
|
|
|
hash[verb.downcase] = klass
|
|
|
|
hash[verb.downcase.to_sym] = klass
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.verb_matcher(verb)
|
|
|
|
VerbMatchers::VERB_TO_CLASS.fetch(verb) do
|
|
|
|
VerbMatchers::Unknown.new verb.to_s.dasherize.upcase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-17 19:07:39 -04:00
|
|
|
def self.build(name, app, path, constraints, required_defaults, defaults)
|
2015-08-17 22:22:59 -04:00
|
|
|
request_method_match = verb_matcher(constraints.delete(:request_method))
|
2015-08-20 18:34:18 -04:00
|
|
|
new name, app, path, constraints, required_defaults, defaults, request_method_match, 0
|
2015-08-17 19:07:39 -04:00
|
|
|
end
|
|
|
|
|
2012-12-19 15:54:47 -05:00
|
|
|
##
|
|
|
|
# +path+ is a path constraint.
|
|
|
|
# +constraints+ is a hash of constraints to be applied to this route.
|
2016-02-13 22:18:22 -05:00
|
|
|
def initialize(name, app, path, constraints, required_defaults, defaults, request_method_match, precedence, internal = false)
|
2012-12-19 15:54:47 -05:00
|
|
|
@name = name
|
|
|
|
@app = app
|
|
|
|
@path = path
|
|
|
|
|
2015-08-17 19:39:26 -04:00
|
|
|
@request_method_match = request_method_match
|
2012-12-19 15:54:47 -05:00
|
|
|
@constraints = constraints
|
|
|
|
@defaults = defaults
|
|
|
|
@required_defaults = nil
|
2015-08-17 16:59:17 -04:00
|
|
|
@_required_defaults = required_defaults
|
2012-12-19 15:54:47 -05:00
|
|
|
@required_parts = nil
|
|
|
|
@parts = nil
|
|
|
|
@decorated_ast = nil
|
2015-08-20 18:34:18 -04:00
|
|
|
@precedence = precedence
|
2014-05-21 14:56:12 -04:00
|
|
|
@path_formatter = @path.build_formatter
|
2016-02-13 22:18:22 -05:00
|
|
|
@internal = internal
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
2017-01-11 08:50:42 -05:00
|
|
|
def eager_load!
|
|
|
|
path.eager_load!
|
|
|
|
ast
|
|
|
|
parts
|
|
|
|
required_defaults
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2012-12-19 15:54:47 -05:00
|
|
|
def ast
|
2013-01-06 07:38:00 -05:00
|
|
|
@decorated_ast ||= begin
|
2013-01-06 07:52:36 -05:00
|
|
|
decorated_ast = path.ast
|
2015-08-17 16:36:09 -04:00
|
|
|
decorated_ast.find_all(&:terminal?).each { |n| n.memo = self }
|
2013-01-06 07:52:36 -05:00
|
|
|
decorated_ast
|
2013-01-06 07:38:00 -05:00
|
|
|
end
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
2017-03-12 12:51:26 -04:00
|
|
|
# Needed for `rails routes`. Picks up succinctly defined requirements
|
|
|
|
# for a route, for example route
|
|
|
|
#
|
|
|
|
# get 'photo/:id', :controller => 'photos', :action => 'show',
|
|
|
|
# :id => /[A-Z]\d{5}/
|
|
|
|
#
|
|
|
|
# will have {:controller=>"photos", :action=>"show", :id=>/[A-Z]\d{5}/}
|
|
|
|
# as requirements.
|
2016-10-26 13:25:02 -04:00
|
|
|
def requirements
|
2016-10-28 23:05:58 -04:00
|
|
|
@defaults.merge(path.requirements).delete_if { |_, v|
|
2012-12-19 15:54:47 -05:00
|
|
|
/.+?/ == v
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def segments
|
2013-01-07 08:09:22 -05:00
|
|
|
path.names
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def required_keys
|
2013-01-15 12:18:59 -05:00
|
|
|
required_parts + required_defaults.keys
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
2016-12-28 14:35:43 -05:00
|
|
|
def score(supplied_keys)
|
2012-12-19 15:54:47 -05:00
|
|
|
required_keys = path.required_names
|
|
|
|
|
2016-12-28 14:35:43 -05:00
|
|
|
required_keys.each do |k|
|
|
|
|
return -1 unless supplied_keys.include?(k)
|
|
|
|
end
|
|
|
|
|
|
|
|
score = 0
|
|
|
|
path.names.each do |k|
|
|
|
|
score += 1 if supplied_keys.include?(k)
|
|
|
|
end
|
2012-12-19 15:54:47 -05:00
|
|
|
|
|
|
|
score + (required_defaults.length * 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def parts
|
2014-10-27 12:28:53 -04:00
|
|
|
@parts ||= segments.map(&:to_sym)
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
alias :segment_keys :parts
|
|
|
|
|
2012-12-20 15:42:39 -05:00
|
|
|
def format(path_options)
|
2014-05-21 14:56:12 -04:00
|
|
|
@path_formatter.evaluate path_options
|
2013-03-03 14:18:01 -05:00
|
|
|
end
|
|
|
|
|
2012-12-19 15:54:47 -05:00
|
|
|
def required_parts
|
2014-10-27 12:28:53 -04:00
|
|
|
@required_parts ||= path.required_names.map(&:to_sym)
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
2013-01-15 12:18:59 -05:00
|
|
|
def required_default?(key)
|
2015-06-08 20:18:06 -04:00
|
|
|
@_required_defaults.include?(key)
|
2013-01-15 12:18:59 -05:00
|
|
|
end
|
|
|
|
|
2012-12-19 15:54:47 -05:00
|
|
|
def required_defaults
|
2016-10-28 23:05:58 -04:00
|
|
|
@required_defaults ||= @defaults.dup.delete_if do |k, _|
|
2013-01-15 12:18:59 -05:00
|
|
|
parts.include?(k) || !required_default?(k)
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
end
|
2013-01-15 08:38:10 -05:00
|
|
|
|
2014-04-20 05:08:32 -04:00
|
|
|
def glob?
|
|
|
|
!path.spec.grep(Nodes::Star).empty?
|
|
|
|
end
|
|
|
|
|
2013-07-16 08:27:22 -04:00
|
|
|
def dispatcher?
|
2014-05-24 22:03:12 -04:00
|
|
|
@app.dispatcher?
|
2013-07-16 08:27:22 -04:00
|
|
|
end
|
|
|
|
|
2013-01-15 08:38:10 -05:00
|
|
|
def matches?(request)
|
2015-08-17 19:39:26 -04:00
|
|
|
match_verb(request) &&
|
|
|
|
constraints.all? { |method, value|
|
2013-01-15 08:38:10 -05:00
|
|
|
case value
|
|
|
|
when Regexp, String
|
|
|
|
value === request.send(method).to_s
|
|
|
|
when Array
|
|
|
|
value.include?(request.send(method))
|
2013-04-11 22:12:19 -04:00
|
|
|
when TrueClass
|
|
|
|
request.send(method).present?
|
|
|
|
when FalseClass
|
|
|
|
request.send(method).blank?
|
2013-01-15 08:38:10 -05:00
|
|
|
else
|
|
|
|
value === request.send(method)
|
|
|
|
end
|
2015-08-17 19:39:26 -04:00
|
|
|
}
|
2013-01-15 08:38:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def ip
|
|
|
|
constraints[:ip] || //
|
|
|
|
end
|
|
|
|
|
2015-08-14 13:09:09 -04:00
|
|
|
def requires_matching_verb?
|
use the strategy pattern to match request verbs
Rather than building a regexp for every route, lets use the strategy
pattern to select among objects that can match HTTP verbs. This commit
introduces strategy objects for each verb that has a predicate method on
the request object like `get?`, `post?`, etc.
When we build the route object, look up the strategy for the verbs the
user specified. If we can't find it, fall back on string matching.
Using a strategy / null object pattern (the `All` VerbMatcher is our
"null" object in this case) we can:
1) Remove conditionals
2) Drop boot time allocations
2) Drop run time allocations
3) Improve runtime performance
Here is our boot time allocation benchmark:
```ruby
require 'action_pack'
require 'action_dispatch'
route_set = ActionDispatch::Routing::RouteSet.new
routes = ActionDispatch::Routing::Mapper.new route_set
result = ObjectSpace::AllocationTracer.trace do
500.times do
routes.resources :foo
end
end
sorted = ObjectSpace::AllocationTracer.allocated_count_table.sort_by(&:last)
sorted.each do |k,v|
next if v == 0
p k => v
end
__END__
Before:
$ be ruby -rallocation_tracer route_test.rb
{:T_SYMBOL=>11}
{:T_REGEXP=>4017}
{:T_STRUCT=>6500}
{:T_MATCH=>12004}
{:T_DATA=>84092}
{:T_OBJECT=>99009}
{:T_HASH=>122015}
{:T_STRING=>216652}
{:T_IMEMO=>355137}
{:T_ARRAY=>441057}
After:
$ be ruby -rallocation_tracer route_test.rb
{:T_SYMBOL=>11}
{:T_REGEXP=>17}
{:T_STRUCT=>6500}
{:T_MATCH=>12004}
{:T_DATA=>84092}
{:T_OBJECT=>99009}
{:T_HASH=>122015}
{:T_STRING=>172647}
{:T_IMEMO=>355136}
{:T_ARRAY=>433056}
```
This benchmark adds 500 resources. Each resource has 8 routes, so it
adds 4000 routes. You can see from the results that this patch
eliminates 4000 Regexp allocations, ~44000 String allocations, and ~8000
Array allocations. With that, we can figure out that the previous code
would allocate 1 regexp, 11 strings, and 2 arrays per route *more* than
this patch in order to handle verb matching.
Next lets look at runtime allocations:
```ruby
require 'action_pack'
require 'action_dispatch'
require 'benchmark/ips'
route_set = ActionDispatch::Routing::RouteSet.new
routes = ActionDispatch::Routing::Mapper.new route_set
routes.resources :foo
route = route_set.routes.first
request = ActionDispatch::Request.new("REQUEST_METHOD" => "GET")
result = ObjectSpace::AllocationTracer.trace do
500.times do
route.matches? request
end
end
sorted = ObjectSpace::AllocationTracer.allocated_count_table.sort_by(&:last)
sorted.each do |k,v|
next if v == 0
p k => v
end
__END__
Before:
$ be ruby -rallocation_tracer route_test.rb
{:T_MATCH=>500}
{:T_STRING=>501}
{:T_IMEMO=>1501}
After:
$ be ruby -rallocation_tracer route_test.rb
{:T_IMEMO=>1001}
```
This benchmark runs 500 calls against the `matches?` method on the route
object. We check this method in the case that there are two methods
that match the same path, but they are differentiated by the verb (or
other conditionals). For example `POST /users` vs `GET /users`, same
path, different action.
Previously, we were using regexps to match against the verb. You can
see that doing the regexp match would allocate 1 match object and 1
string object each time it was called. This patch eliminates those
allocations.
Next lets look at runtime performance.
```ruby
require 'action_pack'
require 'action_dispatch'
require 'benchmark/ips'
route_set = ActionDispatch::Routing::RouteSet.new
routes = ActionDispatch::Routing::Mapper.new route_set
routes.resources :foo
route = route_set.routes.first
match = ActionDispatch::Request.new("REQUEST_METHOD" => "GET")
no_match = ActionDispatch::Request.new("REQUEST_METHOD" => "POST")
Benchmark.ips do |x|
x.report("match") do
route.matches? match
end
x.report("no match") do
route.matches? no_match
end
end
__END__
Before:
$ be ruby -rallocation_tracer runtime.rb
Calculating -------------------------------------
match 17.145k i/100ms
no match 24.244k i/100ms
-------------------------------------------------
match 259.708k (± 4.3%) i/s - 1.303M
no match 453.376k (± 5.9%) i/s - 2.279M
After:
$ be ruby -rallocation_tracer runtime.rb
Calculating -------------------------------------
match 23.958k i/100ms
no match 29.402k i/100ms
-------------------------------------------------
match 465.063k (± 3.8%) i/s - 2.324M
no match 691.956k (± 4.5%) i/s - 3.469M
```
This tests tries to see how many times it can match a request per
second. Switching to method calls and string comparison makes the
successful match case about 79% faster, and the unsuccessful case about
52% faster.
That was fun!
2015-08-17 21:17:55 -04:00
|
|
|
!@request_method_match.all? { |x| x == VerbMatchers::All }
|
2015-08-14 13:09:09 -04:00
|
|
|
end
|
|
|
|
|
2013-01-15 08:38:10 -05:00
|
|
|
def verb
|
2016-08-06 12:51:43 -04:00
|
|
|
verbs.join("|")
|
2015-08-17 19:39:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2016-08-06 13:55:02 -04:00
|
|
|
def verbs
|
|
|
|
@request_method_match.map(&:verb)
|
|
|
|
end
|
2015-08-17 19:39:26 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def match_verb(request)
|
|
|
|
@request_method_match.any? { |m| m.call request }
|
|
|
|
end
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
end
|
2016-10-26 13:25:02 -04:00
|
|
|
# :startdoc:
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|