From b128a0f3db1b78e6790ab2bf98068a76124d5857 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sun, 26 Apr 2020 22:04:14 +0300 Subject: [PATCH] Run path-conflicting just once for quarantine router --- modules/reitit-core/src/reitit/core.cljc | 4 ++-- .../clj/reitit/router_creation_perf_test.clj | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/modules/reitit-core/src/reitit/core.cljc b/modules/reitit-core/src/reitit/core.cljc index 25bd3ef8..bcd54ac4 100644 --- a/modules/reitit-core/src/reitit/core.cljc +++ b/modules/reitit-core/src/reitit/core.cljc @@ -301,7 +301,7 @@ ([compiled-routes] (quarantine-router compiled-routes {})) ([compiled-routes opts] - (let [conflicting-paths (-> compiled-routes (impl/path-conflicting-routes opts) impl/conflicting-paths) + (let [conflicting-paths (impl/conflicting-paths (or (::path-conflicting opts) (impl/path-conflicting-routes compiled-routes opts))) conflicting? #(contains? conflicting-paths (first %)) {conflicting true, non-conflicting false} (group-by conflicting? compiled-routes) linear-router (linear-router conflicting opts) @@ -389,7 +389,7 @@ (when-let [validate (:validate opts)] (validate compiled-routes opts)) - (router compiled-routes opts)) + (router compiled-routes (assoc opts ::path-conflicting path-conflicting))) (catch #?(:clj Exception, :cljs js/Error) e (throw ((get opts :exception identity) e))))))) diff --git a/perf-test/clj/reitit/router_creation_perf_test.clj b/perf-test/clj/reitit/router_creation_perf_test.clj index de9f4b23..9349d128 100644 --- a/perf-test/clj/reitit/router_creation_perf_test.clj +++ b/perf-test/clj/reitit/router_creation_perf_test.clj @@ -1,5 +1,5 @@ (ns reitit.router-creation-perf-test - (:require [reitit.perf-utils :refer [bench!]] + (:require [reitit.perf-utils :refer [bench! suite]] [reitit.core :as r] [clojure.string :as str]) (:import (java.util Random))) @@ -32,12 +32,25 @@ (let [rnd (random 1)] (mapv (fn [n] [(route rnd) (keyword (str "route" n))]) (range 100)))) +(conj hundred-routes (last hundred-routes)) + + (defn bench-routers [] + + (suite "non-conflicting") + ;; 104ms (bench! "default" (r/router hundred-routes)) ;; 7ms - (bench! "linear" (r/router hundred-routes {:router r/linear-router, :conflicts nil}))) + (bench! "linear" (r/router hundred-routes {:router r/linear-router, :conflicts nil})) + + (suite "conflicting") + (let [routes (conj hundred-routes [(first (last hundred-routes)) ::route])] + + ;; 205ms + ;; 105ms (cache path-conflicts) + (bench! "default" (r/router routes {:conflicts nil})))) (comment (bench-routers))