mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
16a80882f9
The FSM used to find matching routes was previously limited to patterns
that contained parameters with the default regexp / no constraints. In
large route sets where many parameters are constrained by custom regexp,
these routes all fall back on a slow linear search over the route list.
These custom regexes were not previously able to be included in the FSM
because it transitioned between nodes using only fragments of the URI,
or path separators [/.?], but a custom regex may cross a path separator
boundary. To work around this, the TransitionTable is improved to
support remembering a point within the matching string that we started,
and continuing to attempt to match from that point up to the end of each
token. Only parameters not on a path separator boundary must still match
with a linear search after this change (e.g. `/foo-:bar/`).
This results in performance for constrainted routes that matches that of
ones using the default regexp.
Benchmark:
https://gist.github.com/theojulienne/e91fc338d180e1710e29c81a5d701fab
Before:
```
Calculating -------------------------------------
without params 6.466k (±12.7%) i/s - 31.648k in 5.009453s
params without constraints
5.867k (±12.9%) i/s - 28.842k in 5.032637s
params with constraints
909.661 (± 7.9%) i/s - 4.536k in 5.023534s
```
After:
```
Calculating -------------------------------------
without params 6.387k (±11.9%) i/s - 31.728k in 5.068939s
params without constraints
5.824k (±13.2%) i/s - 28.650k in 5.043701s
params with constraints
5.406k (±11.7%) i/s - 26.931k in 5.076412s
```
For github.com which has many constrainted parameters, a random sampling
of 10 URL patterns can be matched approximately 2-4x faster than before.
This commit fixes symbols as constrains as tested in
6ab985da28
151 lines
3 KiB
Ruby
151 lines
3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "action_dispatch/journey/visitors"
|
|
|
|
module ActionDispatch
|
|
module Journey # :nodoc:
|
|
module Nodes # :nodoc:
|
|
class Node # :nodoc:
|
|
include Enumerable
|
|
|
|
attr_accessor :left, :memo
|
|
|
|
def initialize(left)
|
|
@left = left
|
|
@memo = nil
|
|
end
|
|
|
|
def each(&block)
|
|
Visitors::Each::INSTANCE.accept(self, block)
|
|
end
|
|
|
|
def to_s
|
|
Visitors::String::INSTANCE.accept(self, "")
|
|
end
|
|
|
|
def to_dot
|
|
Visitors::Dot::INSTANCE.accept(self)
|
|
end
|
|
|
|
def to_sym
|
|
name.to_sym
|
|
end
|
|
|
|
def name
|
|
-left.tr("*:", "")
|
|
end
|
|
|
|
def type
|
|
raise NotImplementedError
|
|
end
|
|
|
|
def symbol?; false; end
|
|
def literal?; false; end
|
|
def terminal?; false; end
|
|
def star?; false; end
|
|
def cat?; false; end
|
|
def group?; false; end
|
|
end
|
|
|
|
class Terminal < Node # :nodoc:
|
|
alias :symbol :left
|
|
def terminal?; true; end
|
|
end
|
|
|
|
class Literal < Terminal # :nodoc:
|
|
def literal?; true; end
|
|
def type; :LITERAL; end
|
|
end
|
|
|
|
class Dummy < Literal # :nodoc:
|
|
def initialize(x = Object.new)
|
|
super
|
|
end
|
|
|
|
def literal?; false; end
|
|
end
|
|
|
|
class Slash < Terminal # :nodoc:
|
|
def type; :SLASH; end
|
|
end
|
|
|
|
class Dot < Terminal # :nodoc:
|
|
def type; :DOT; end
|
|
end
|
|
|
|
class Symbol < Terminal # :nodoc:
|
|
attr_accessor :regexp
|
|
alias :symbol :regexp
|
|
attr_reader :name
|
|
|
|
DEFAULT_EXP = /[^.\/?]+/
|
|
GREEDY_EXP = /(.+)/
|
|
def initialize(left, regexp = DEFAULT_EXP)
|
|
super(left)
|
|
@regexp = regexp
|
|
@name = -left.tr("*:", "")
|
|
end
|
|
|
|
def default_regexp?
|
|
regexp == DEFAULT_EXP
|
|
end
|
|
|
|
def type; :SYMBOL; end
|
|
def symbol?; true; end
|
|
end
|
|
|
|
class Unary < Node # :nodoc:
|
|
def children; [left] end
|
|
end
|
|
|
|
class Group < Unary # :nodoc:
|
|
def type; :GROUP; end
|
|
def group?; true; end
|
|
end
|
|
|
|
class Star < Unary # :nodoc:
|
|
attr_accessor :regexp
|
|
|
|
def initialize(left)
|
|
super(left)
|
|
|
|
# By default wildcard routes are non-greedy and must match something.
|
|
@regexp = /.+?/
|
|
end
|
|
|
|
def star?; true; end
|
|
def type; :STAR; end
|
|
|
|
def name
|
|
left.name.tr "*:", ""
|
|
end
|
|
end
|
|
|
|
class Binary < Node # :nodoc:
|
|
attr_accessor :right
|
|
|
|
def initialize(left, right)
|
|
super(left)
|
|
@right = right
|
|
end
|
|
|
|
def children; [left, right] end
|
|
end
|
|
|
|
class Cat < Binary # :nodoc:
|
|
def cat?; true; end
|
|
def type; :CAT; end
|
|
end
|
|
|
|
class Or < Node # :nodoc:
|
|
attr_reader :children
|
|
|
|
def initialize(children)
|
|
@children = children
|
|
end
|
|
|
|
def type; :OR; end
|
|
end
|
|
end
|
|
end
|
|
end
|