From df359d66c9955b54fd999e231e549b2eaf75e717 Mon Sep 17 00:00:00 2001 From: Luis Lavena Date: Mon, 18 Jan 2016 13:48:41 -0300 Subject: [PATCH] Build Route Regex once to reduce allocations Right now every `match?` performed against the routes defined result in a new Regex compiled for the exact same `@path`. Since `@path` of the Route does not change on every request, we pre-build it to avoid repeated allocations. When testing this in release mode (against 0.10.2 [d1e3f0b]): Before: 32408.48 req/s After: 34862.07 req/s --- src/kemal/route.cr | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/kemal/route.cr b/src/kemal/route.cr index 9c3947b..ff34373 100644 --- a/src/kemal/route.cr +++ b/src/kemal/route.cr @@ -6,13 +6,14 @@ class Kemal::Route getter method def initialize(@method, @path, &@handler : Kemal::Context -> _) + @compiled_regex = pattern_to_regex(@path) end def match?(request) check_for_method_override!(request) return nil unless request.override_method == @method 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 return true end