Merge pull request #55 from luislavena/precompile-regex

Build Route Regex once to reduce allocations
This commit is contained in:
Serdar Dogruyol 2016-01-18 19:27:17 +02:00
commit 2cc4c17c80

View file

@ -6,13 +6,14 @@ class Kemal::Route
getter method getter method
def initialize(@method, @path, &@handler : Kemal::Context -> _) def initialize(@method, @path, &@handler : Kemal::Context -> _)
@compiled_regex = pattern_to_regex(@path)
end end
def match?(request) def match?(request)
check_for_method_override!(request) check_for_method_override!(request)
return nil unless request.override_method == @method return nil unless request.override_method == @method
return true if request.path.not_nil!.includes?(':') && request.path.not_nil! == @path return true if request.path.not_nil!.includes?(':') && request.path.not_nil! == @path
request.path.not_nil!.match(pattern_to_regex(@path)) do |url_params| request.path.not_nil!.match(@compiled_regex) do |url_params|
request.url_params = url_params request.url_params = url_params
return true return true
end end